开发者

Help DataBinding to Checkbox in WPF

I have a CheckBox in a ListBox. I set the ListBox ItemsSource to a List of Agency. Agency has a property

public class Agency 
{ 
  public bool isSelected { get; set;} 
}
<ListBox> <!-- ItemsSource set in codebehind to List<Agency> -->
    <CheckBox IsChecked="{Bindin开发者_如何学JAVAg Path=isSelected, Mode=TwoWay}" />
</ListBox>

I have a function to check all the checkboxes

//SelectAll button

    private void SelectAll_Click(object sender, RoutedEventArgs e)
    {
      List<Agency> list = this.AgencySubListBox.ItemsSource as List<Agency>;
      for (int i = 0; i < list.Count; i++)
      {
        Agency d = list[i];
        d.isSelected = true;
      }
    }

When I hit the select all button I expect the checkboxes to all be checked. But nothing happens.


You have to implement INotifyPropertyChanged for your Agency-class. Then in your isSelected-Property, call PropertyChanged if the value of the property has been changed. Auto-properties as you used in your example do not support INotifiyPropertyChanged, therefore you can not use them for your purpose. If you work with .net, I would also recommend to start the property names with upper case. This is the widely accepted standard.


You should make your model implement INotifyPropertyChanged, and implement it


There is nothing to tell the UI that the your checkbox binding has been invalidated, and needs rechecking. Therefore, implement INotifyPropertyChanged on your Agency type.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜