如何选中所有复选框C#

如何选中所有复选框C#,c#,.net,mvvm,C#,.net,Mvvm,我有几个复选框,我希望Functionality选中或取消选中all复选框。下面是class.cs和xaml代码。如何为“全部选中”或“全部取消选中”添加功能性 public static event PropertyChangedEventHandler IsCheckedChanged; private bool isChecked; public WorkStep(string name) { Name = name; Is

我有几个复选框,我希望Functionality选中或取消选中all复选框。下面是class.cs和xaml代码。如何为“全部选中”或“全部取消选中”添加功能性

    public static event PropertyChangedEventHandler IsCheckedChanged;
    private bool isChecked;

    public WorkStep(string name)
    {
        Name = name;
        IsChecked = true;
    }

    public WorkStep(string name, bool isChecked)
    {
        Name = name;
        IsChecked = isChecked;
    }

    public string Name
    {
        get;
    }

    public bool IsChecked
    {
        get { return isChecked; }
        set
        {
            isChecked = value;
            OnIsCheckedChanged();
        }
    }

    private void OnIsCheckedChanged()
    {
        PropertyChangedEventHandler handler = IsCheckedChanged;
        if (handler != null)
            handler(this, new PropertyChangedEventArgs("IsChecked"));
    }
和xaml:

                                <DataGridTemplateColumn.CellTemplate>
                                    <DataTemplate>
                                        <CheckBox IsChecked="{Binding IsChecked, UpdateSourceTrigger=PropertyChanged}" HorizontalAlignment="Center"/>
                                    </DataTemplate>
                                </DataGridTemplateColumn.CellTemplate>

我将使用
INotifyPropertyChanged
界面,如下面的类所示:

public class myClass : INotifyPropertyChanged
{
    private bool _IsChecked;
    public bool IsChecked
    {
        get { return _IsChecked; }
        set
        {
            _IsChecked = value;
            OnPropertyChanged("IsChecked");
        }
    }

    public event PropertyChangedEventHandler PropertyChanged;
    protected void OnPropertyChanged(string name)
    {
        PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(name));
    }
}
然后我将属性
IsChecked
绑定到我的所有复选框,如xaml中所示:

<Grid>
    <CheckBox x:Name="checkBox1" IsChecked="{Binding IsChecked,UpdateSourceTrigger=PropertyChanged}" Content="CheckBox" HorizontalAlignment="Left" Margin="116,90,0,0" VerticalAlignment="Top"/>
    <CheckBox x:Name="checkBox2" IsChecked="{Binding IsChecked,UpdateSourceTrigger=PropertyChanged}" Content="CheckBox" HorizontalAlignment="Left" Margin="116,126,0,0" VerticalAlignment="Top"/>
    <CheckBox x:Name="checkBox3" IsChecked="{Binding IsChecked,UpdateSourceTrigger=PropertyChanged}" Content="CheckBox" HorizontalAlignment="Left" Margin="116,164,0,0" VerticalAlignment="Top"/>
    <Button x:Name="button" Content="Button" HorizontalAlignment="Left" Margin="380,235,0,0" VerticalAlignment="Top" Width="75" Click="button_Click"/>
</Grid>
我通常会列一个清单:列出我的箱子,并将箱子添加到清单中。然后我可以简单地编写代码来枚举列表。
public MainWindow()
{
    InitializeComponent();
    DataContext = MyClass;
}

myClass MyClass = new myClass();

private void button_Click(object sender, RoutedEventArgs e)
{
    if(MyClass.IsChecked)
        MyClass.IsChecked = false;
    else
        MyClass.IsChecked = true;
}