2013-05-13 52 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