开发者

wpf listbox checkbox combination, really stuck

开发者 https://www.devze.com 2023-02-09 16:08 出处:网络
I am trying to link the checkbox and listview together, and then use the binding method to set the path to the object in order to set the check-box IsChecked status in the listbox.

I am trying to link the checkbox and listview together, and then use the binding method to set the path to the object in order to set the check-box IsChecked status in the listbox.

List<Test1> datas = new List<Test1>();

var data = new Test1 {Key = 1, Value = "Hello", IsSelected= true};
    datas.Add(data);
    data = new Test1 {Key = 2, Value = "Hello2", IsSelected= false};
    datas.Add(data);

What I need to happen is that if the checkbox is checked ( IsSelected is true ) then I need to populate with those values and then when I click and unclick the checkbox in the GUI I need it to also select the proper listeview item so I 开发者_如何学Ccan get to the tag property.

This code below does not set the checkbox IsChecked.

<ListBox ItemsSource="{Binding}" Name="lstSwimLane" Width="225" Height="125" SelectionChanged="lstSwimLane_SelectionChanged">
    <ListBox.ItemTemplate>
        <DataTemplate>
            <StackPanel Orientation="Horizontal">
                <CheckBox IsChecked="{Binding RelativeSource={RelativeSource AncestorType=ListBoxItem}, Path=IsSelected, Mode=TwoWay}" 
                                          Checked="CheckBox_Checked" 
                                          Unchecked="CheckBox_Unchecked" />
                <TextBlock Text="{Binding Path=Value}"></TextBlock>
            </StackPanel>
        </DataTemplate>
    </ListBox.ItemTemplate>
</ListBox>

What do I need to change in order to set the check box to the value in the object? I even tried INotifyChange etc...

Here is the object as well.

public class Test1 : INotifyPropertyChanged
{
    public event PropertyChangedEventHandler PropertyChanged;

    private bool _isChecked;

    public int Key { get; set; }
    public string Value { get; set; }

    public bool IsSelected
    {
        get { return _isChecked; } 
        set
        {
            if(_isChecked != value)
            {
                _isChecked = value;
                OnPropertyChanged("IsSelected");
            }
        }
    }

    protected void OnPropertyChanged(string name)
    {
        if (PropertyChanged != null)
            PropertyChanged(this, new PropertyChangedEventArgs(name));
    }
}

I am new to all this wpf, and I am stumped.

Thanks.


Do you need this to work "three-way"? So setting any of the three properties

  • ListBoxItem.IsSelected
  • CheckBox.IsChecked
  • Item1.IsSelected

will affect both of the other properties? Unfortunately, there is no such Binding in WPF so you're gonna have to do a little extra work.

Update
As pointed out by Robert Rossney, a much better solution for this is to bind

  • ListBoxItem.IsSelected to Item1.IsSelected
  • CheckBox.IsChecked to ListBoxItem.IsSelected

Updated Xaml

<ListBox ItemsSource="{Binding}"
         Name="lstSwimLane"
         SelectionMode="Multiple"
         Width="225" Height="125" SelectionChanged="lstSwimLane_SelectionChanged">
    <ListBox.ItemContainerStyle>
        <Style TargetType="ListBoxItem">
            <Setter Property="IsSelected"
                Value="{Binding Path=IsSelected,
                                Mode=TwoWay}"/>
        </Style>
    </ListBox.ItemContainerStyle>
    <ListBox.ItemTemplate>
        <DataTemplate>
            <StackPanel Orientation="Horizontal">
                <CheckBox Checked="CheckBox_Checked"
                          Unchecked="CheckBox_Unchecked"
                          IsChecked="{Binding RelativeSource={RelativeSource AncestorType={x:Type ListBoxItem}},
                                              Path=IsSelected}">
                </CheckBox>
                <TextBlock Text="{Binding Path=Value}"/>
            </StackPanel>
        </DataTemplate>
    </ListBox.ItemTemplate>
</ListBox>


In case anyone is interested, here is the markup for the listbox / combox. It will display horizontal.

Thank you all again for all you help as I greatly appreciated it.

<ListBox ScrollViewer.HorizontalScrollBarVisibility="Disabled" 
         ItemsSource="{Binding}" 
         Name="lstSwimLane" 
         SelectionMode="Multiple"
         Width="auto" 
         Height="auto"
         Background="Transparent"
         BorderThickness="0" 
         SelectionChanged="lstSwimLane_SelectionChanged" >
    <ListBox.ItemsPanel>
        <ItemsPanelTemplate>
            <WrapPanel IsItemsHost="True" />
        </ItemsPanelTemplate>
    </ListBox.ItemsPanel>

    <ListBox.ItemContainerStyle>
        <Style TargetType="ListBoxItem">
            <Setter Property="IsSelected" Value="{Binding Path=IsChecked, Mode=TwoWay}"/>
        </Style>
    </ListBox.ItemContainerStyle>

    <ListBox.ItemTemplate>
        <DataTemplate>
            <StackPanel Orientation="Horizontal" Margin="3,3,3,3">
                <CheckBox IsChecked="{Binding RelativeSource={RelativeSource AncestorType={x:Type ListBoxItem}}, Path=IsSelected}"
                          Checked="CheckBox_Checked" 
                          Unchecked="CheckBox_Unchecked" 
                          VerticalAlignment="Center" 
                          Margin="0,0,4,0" />
                <TextBlock Text="{Binding Value}" VerticalAlignment="Center"/>
            </StackPanel>
        </DataTemplate>
    </ListBox.ItemTemplate>
</ListBox>
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号