wpf列表框复选框组合,真的卡住了

nit*_*rog 1 wpf binding combobox listbox

我试图将checkbox和listview链接在一起,然后使用绑定方法设置对象的路径,以便在列表框中设置复选框IsChecked状态.

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);
Run Code Online (Sandbox Code Playgroud)

我需要发生的是,如果选中复选框(IsSelected为true),那么我需要填充这些值,然后当我单击并取消选中GUI中的复选框时,我需要它也选择正确的侦听项目,这样我就可以转到标签属性.

下面的代码未设置复选框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>
Run Code Online (Sandbox Code Playgroud)

为了将复选框设置为对象中的值,我需要更改什么?我甚至尝试过INotifyChange等......

这也是对象.

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));
    }
}
Run Code Online (Sandbox Code Playgroud)

我是所有这些wpf的新手,我很难过.

谢谢.

Fre*_*lad 6

你需要这个"三路"工作吗?所以设置三个属性中的任何一个

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

会影响其他两个属性吗?不幸的是,在WPF中没有这样的绑定,所以你将不得不做一些额外的工作.

更新
正如罗伯特罗斯尼所指出的,更好的解决方案是绑定

  • ListBoxItem.IsSelectedItem1.IsSelected
  • CheckBox.IsCheckedListBoxItem.IsSelected

更新了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>
Run Code Online (Sandbox Code Playgroud)