Reputation: 1
i have a few checkboxes an I want funcionality to check or uncheck all checkbox. Here is a class .cs and xaml code. How to add funcionality for check or uncheck all?
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"));
}
and xaml:
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<CheckBox IsChecked="{Binding IsChecked, UpdateSourceTrigger=PropertyChanged}" HorizontalAlignment="Center"/>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
Upvotes: 0
Views: 203
Reputation: 3014
I would use the INotifyPropertyChanged
interface like shown in the class below:
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));
}
}
Then I would bind the property IsChecked
to all of my checkboxes like shown below in the 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>
In the MainWindow
class make a new instance of the class where the IsChecked
property is located (in my case myClass MyClass = new myClass()
). Then add the newly created instance MyClass
to the DataContext of your MainWindow (DataContext = MyClass;
). I use a button control in this example to check and uncheck all of my checkboxes. If the value of the property IsChecked
is true all of the checkboxes are checked and if it's false all of the checkboxes are unchecked. The MainWindow
class is shown below :
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;
}
Upvotes: 1