C# 列表框中的组合框不';t更新检查是否正确
我使用以下内容制作了一个CheckedListBox:C# 列表框中的组合框不';t更新检查是否正确,c#,wpf,data-binding,combobox,C#,Wpf,Data Binding,Combobox,我使用以下内容制作了一个CheckedListBox: <ListBox x:Name="lst_checkBoxList" ItemsSource="{Binding}"> <ListBox.ItemTemplate> <DataTemplate> <CheckBox Content="{Binding Name}" IsChecked="{Binding IsChecked}" />
<ListBox x:Name="lst_checkBoxList" ItemsSource="{Binding}">
<ListBox.ItemTemplate>
<DataTemplate>
<CheckBox Content="{Binding Name}" IsChecked="{Binding IsChecked}" />
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
public List<CheckedListItem> listItems = new List<CheckedListItem>();
public class CheckedListItem
{
public int Id { get; set; }
public string Name { get; set; }
public bool IsChecked { get; set; }
public CheckedListItem(int id, string name, bool ischecked)
{
Id = id;
Name = name;
IsChecked = ischecked;
}
}
private void button1_Click(object sender, RoutedEventArgs e)
{
listItems.Add(new CheckedListItem(0, "item 1", false));
listItems.Add(new CheckedListItem(0, "item 2", false));
listItems.Add(new CheckedListItem(0, "item 3", false));
listItems.Add(new CheckedListItem(0, "item 4", false));
lst_checkBoxList.ItemsSource = listItems;
listItems[0].IsChecked = true; //This correctly sets the first CheckBox as checked.
}
private void button2_Click(object sender, RoutedEventArgs e)
{
listItems[0].IsChecked = true; //This does not affect the CheckBox, despite changing the value it is bound to.
}
我使用以下方法填充它:
<ListBox x:Name="lst_checkBoxList" ItemsSource="{Binding}">
<ListBox.ItemTemplate>
<DataTemplate>
<CheckBox Content="{Binding Name}" IsChecked="{Binding IsChecked}" />
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
public List<CheckedListItem> listItems = new List<CheckedListItem>();
public class CheckedListItem
{
public int Id { get; set; }
public string Name { get; set; }
public bool IsChecked { get; set; }
public CheckedListItem(int id, string name, bool ischecked)
{
Id = id;
Name = name;
IsChecked = ischecked;
}
}
private void button1_Click(object sender, RoutedEventArgs e)
{
listItems.Add(new CheckedListItem(0, "item 1", false));
listItems.Add(new CheckedListItem(0, "item 2", false));
listItems.Add(new CheckedListItem(0, "item 3", false));
listItems.Add(new CheckedListItem(0, "item 4", false));
lst_checkBoxList.ItemsSource = listItems;
listItems[0].IsChecked = true; //This correctly sets the first CheckBox as checked.
}
private void button2_Click(object sender, RoutedEventArgs e)
{
listItems[0].IsChecked = true; //This does not affect the CheckBox, despite changing the value it is bound to.
}
public List List items=new List();
公共类CheckedListItem
{
公共int Id{get;set;}
公共字符串名称{get;set;}
已检查公共布尔值{get;set;}
public CheckedListItem(int-id、字符串名、bool-ischecked)
{
Id=Id;
名称=名称;
IsChecked=IsChecked;
}
}
私有无效按钮1\u单击(对象发送者,路由目标)
{
添加(新的CheckedListItem(0,“item 1”,false));
添加(新的CheckedListItem(0,“item 2”,false));
添加(新的CheckedListItem(0,“item 3”,false));
添加(新的CheckedListItem(0,“item 4”,false));
lst_checkBoxList.ItemsSource=listItems;
listItems[0]。IsChecked=true;//这将正确地将第一个复选框设置为选中状态。
}
私有无效按钮2\u单击(对象发送者,路由目标)
{
listItems[0]。IsChecked=true;//这不会影响复选框,尽管更改了它绑定到的值。
}
如果我在设置ItemsSource后立即更改其中一个复选框后面的数据,则相应地勾选该复选框,但是,如果我尝试在代码中的任何其他位置更改值,复选框将保持未选中状态……有人能帮我找出原因吗?您需要在CheckedListItem类上实现接口,然后在任何属性更改时通知。您需要在CheckedListItem类上实现接口,然后在任何属性上通知更改。您需要通过在
CheckedListItem
中实现INotifyPropertyChanged
接口,通知控件它绑定到的属性已更新:
public class CheckedListItem : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
private bool _IsChecked;
public bool IsChecked
{
get { return _IsChecked; }
set
{
_IsChecked = value;
if (PropertyChanged != null)
PropertyChanged(this, new PropertyChangedEventArgs("IsChecked"));
}
}
}
您需要通过在
CheckedListItem
中实现INotifyPropertyChanged
接口来通知控件它绑定到的属性已更新:
public class CheckedListItem : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
private bool _IsChecked;
public bool IsChecked
{
get { return _IsChecked; }
set
{
_IsChecked = value;
if (PropertyChanged != null)
PropertyChanged(this, new PropertyChangedEventArgs("IsChecked"));
}
}
}
只需将绑定代码(button1_Click的前5行)保留在Window_Loaded(Window.Loaded)事件中即可。我确信button1也会停止工作,我的意思是第一个被检查的元素会在按钮1点击时停止。目前,由于竞争条件,在绑定控件(在运行时)之前就设置了第一个元素。只需将绑定代码(按钮1\u单击的前5行)保留在Window\u Loaded(Window.Loaded)事件中即可。我确信button1也会停止工作,我的意思是第一个被检查的元素会在按钮1点击时停止。目前,由于竞争条件的原因,在绑定控件(在运行时)之前就已经设置了第一个elment。这真是一个好消息!谢谢,虽然我很恼火,因为这件事太简单了,我没有想到……哦,好吧,吸取了教训!真是太棒了!谢谢,虽然我很恼火,因为这件事太简单了,我没有想到……哦,好吧,吸取了教训!