2013-05-13 50 views
0

您好任何一个可以请帮我在我的代码,复选框没有表现出作为一个在UI检查

我有XAML:

<ListView Name="__Listview" ItemsSource="{Binding Path=DisplayItems}"> 
    <CheckBox Content="{Binding Items}" IsChecked="{Binding Path=IsChecked, Mode=TwoWay, NotifyOnTargetUpdated=True}" Checked="CheckBox_Checked" Unchecked="CheckBox_Checked"/> 
</ListView> 
<CheckBox Name="_checkBoxSelectAll" Checked="CheckBoxToCheckAll" Unchecked="CheckBoxToCheckAll"/> 

代码C#:

public partial class DisplayItems 
{  
    private ObservableCollection<Records> _displayItems = new ObservableCollection< Records>(); 

    public DisplayItems() 
    { 
     InitializeComponent(); 
     _ Listview.DataContext = this; 
    } 

    public ObservableCollection<Records> DisplayItems 
    { 
     get { return _displayItems; } 
     set { _displayItems = value; } 
    } 

    private void CheckBoxToCheckAll(object sender, RoutedEventArgs e) 
    {   
     if (_checking || !(sender is CheckBox)) 
      return; 

     CheckBox checkBox = (CheckBox)sender; 
     _checking = true; 

     foreach (Records swElement in DisplayItems) 
     { 
      bool val; 
      if (checkBox.IsChecked != null) 
       val = checkBox.IsChecked.Value; 
      else 
       val = false; 

      swElement.IsChecked = val; 
     } 
     _checking = false;   
    } 

    private void CheckBox_Checked(object sender, RoutedEventArgs e) 
    { 
     if ((sender as CheckBox) == null || ((sender as CheckBox).DataContext as Records) == null || _checking) 
      return; 

     _checking = true; 

     if (_checkBoxSelectAll.IsChecked != null && _listTo.All(select => select.IsChecked) && !_checkBoxSelectAll.IsChecked.Value) 
      _checkBoxSelectAll.IsChecked = true; 
     else if (_checkBoxSelectAll.IsChecked == null || _checkBoxSelectAll.IsChecked.Value) 
      _checkBoxSelectAll.IsChecked = false; 

     _checking = false; 
    } 
} 

Public class Records 
{ 
    public event PropertyChangedEventHandler PropertyChanged; 
    private bool _isChecked = false; 

    Public Records(){} 
    public bool IsChecked 
    { 
     get { return _isChecked; } 
     set { _isChecked = value; OnPropertyChanged("IsChecked"); } 
    } 

    protected void OnPropertyChanged(string name) 
    { 
     if ((PropertyChanged != null) && (_notification)) 
      PropertyChanged(this, new PropertyChangedEventArgs(name)); 
    } 
    public enum Items{One,Two,three,} 
} 

如果我检查_checkBoxSelectAll,在列表视图所有checkBoxs应该检查,我的问题是它的背后=器isChecked真正的代码,但在UI中不可见该复选框被选中,pleae帮我提前

+0

我不知道,你的XAML不抛出异常*的项目必须集合在使用ItemsSource之前*是空的。 – LPL 2013-05-13 16:34:19

+0

定义了_notification? – Steve 2013-05-13 18:39:41

+0

其定义的私有布尔_notification公共BOOL通知{获得;设置;},我忘了提 – Kumar 2013-05-13 20:07:21

回答

0

对记录实施INotifyPropertyChanged?

Public class Records**: INotifyPropertyChanged** 

更换你的记录类下面有请

Public class Records : INotifyPropertyChanged 
{ 
public event PropertyChangedEventHandler PropertyChanged; 
private bool _isChecked = false; 

Public Records(){} 
public `bool IsChecked` 
     { 
      get { return _isChecked; } 
      set { _isChecked = value; OnPropertyChanged("IsChecked"); } 
     } 
protected void OnPropertyChanged(string name) 
     { 
      if ((PropertyChanged != null) && (_notification)) 
       PropertyChanged(this, new PropertyChangedEventArgs(name)); 
     } 
public enum Items{One,Two,three,} 
} 
+0

我实现INotifyPropertyChanged的,但没有更新UI ... – Kumar 2013-05-13 16:11:48

+0

你必须明确指定你的类实现INotifypropertychanged。你已经实现了它,但没有在类定义中声明它。 – cheedep 2013-05-13 16:14:11

+0

事件PropertyChangedEventHandler INotifyPropertyChanged.PropertyChanged { 加{抛出新NotImplementedException(); } remove {throw new NotImplementedException();} }} 可以 – Kumar 2013-05-13 16:16:39