我在ListBox
中有一个DataTemplate
包含复选框,我有两个按钮“全选”和“取消全选”。我想要选中所有选项并通过单击选择取消选中全部取消选择按钮,我想实现INotifyPropertyChanged
到课堂。我该怎么办呢?
提前感谢您的回答..
XAML代码
<StackPanel>
<ListBox Name="lstUserDefination"
ScrollViewer.VerticalScrollBarVisibility="Auto" SelectionMode="Multiple">
<ListBox.ItemTemplate>
<DataTemplate>
<ListBoxItem>
<CheckBox Name="chkUser" Content="{Binding AuthorityName}"/>
</ListBoxItem>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
</StackPanel>
C#CODE
public partial class UserDefinationEdit : Window
{
public ObservableCollection<Authority> authorityList { get; set; }
public UserDefinationEdit()
{
InitializeComponent();
CreateCheckBoxList();
lstUserDefination.ItemsSource = authorityList;
}
private void Window_MouseDown(object sender, MouseButtonEventArgs e)
{
if (e.LeftButton == MouseButtonState.Pressed)
{
DragMove();
}
}
public void CreateCheckBoxList()
{
authorityList = new ObservableCollection<Authority>();
authorityList.Add(new Authority() {AuthorityValue = 0, AuthorityName = "0 - " });
authorityList.Add(new Authority() { AuthorityValue = 1, AuthorityName = "1 - " });
authorityList.Add(new Authority() { AuthorityValue = 2, AuthorityName = "2 - " });
authorityList.Add(new Authority() { AuthorityValue = 3, AuthorityName = "3 - " });
authorityList.Add(new Authority() { AuthorityValue = 4, AuthorityName = "4 - " });
authorityList.Add(new Authority() { AuthorityValue = 5, AuthorityName = "5 - " });
authorityList.Add(new Authority() { AuthorityValue = 6, AuthorityName = "6 - " });
this.DataContext = this;
}
private void btnUnselectall_Click(object sender, RoutedEventArgs e)
{
lstUserDefination.UnselectAll();
}
private void btnSelectAll_Click(object sender, RoutedEventArgs e)
{
lstUserDefination.SelectAll();
}
}
public class Authority
{
public string AuthorityName { get; set; }
public int AuthorityValue { get; set; }
public bool IsChecked { get; set; }
}
}
答案 0 :(得分:3)
在(&x).m()
模板
&
属性添加绑定
IsChecked
将按钮处理程序更改为
ListBoxItem
请注意,您的<CheckBox Name="chkUser" Content="{Binding AuthorityName}" IsChecked="{Binding IsChecked}"/>
课程应实施private void btnUnselectall_Click(object sender, RoutedEventArgs e)
{
foreach (var a in authorityList)
{
a.IsChecked = false;
}
}
private void btnSelectAll_Click(object sender, RoutedEventArgs e)
{
foreach (var a in authorityList)
{
a.IsChecked = true;
}
}
以使其发挥作用。
Authority
答案 1 :(得分:1)
实施INotifyPropertyChanged
:
public class Authority : INotifyPropertyChanged
{
private string _authorityName;
public string AuthorityName
{
get { return _authorityName; }
set { _authorityName = value; NotifyPropertyChanged(); }
}
private string _authorityValue;
public string AuthorityValue
{
get { return _authorityValue; }
set { _authorityValue = value; NotifyPropertyChanged(); }
}
private bool _isChecked;
public bool IsChecked
{
get { return _isChecked; }
set { _isChecked = value; NotifyPropertyChanged(); }
}
public event PropertyChangedEventHandler PropertyChanged;
private void NotifyPropertyChanged([CallerMemberName] String propertyName = "")
{
if (PropertyChanged != null)
PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
}
设置所有IsChecked
个对象的Authority
属性:
private void btnUnselectall_Click(object sender, RoutedEventArgs e)
{
Select(false);
}
private void btnSelectAll_Click(object sender, RoutedEventArgs e)
{
Select(true);
}
private void Select(bool select)
{
foreach (Authority authority in authorityList)
authority.IsChecked = select;
}
绑定XAML中的IsChecked
属性:
<CheckBox Name="chkUser" IsChecked="{Binding IsChecked}" Content="{Binding AuthorityName}"/>