您好,任何人都可以帮助我编写代码,
我有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,Listview中的所有复选框都应该检查,我的问题是其IsChecked = true后面的代码,但是在UI中看不到Checkbox被检查,请提前帮助我
答案 0 :(得分: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,}
}