I have a DataGrid
with a column of checkboxes and I have a checkbox in the DataGrid
header that, when checked, checks all the checkboxes. Based on this answer, I have a command bound to the "checked" event and another one that binds to the "unchecked" event.
All the relevant files are below (simplified, of course)
My XAML:
<DataGridTemplateColumn Width="40">
<DataGridTemplateColumn.Header>
<CheckBox>
<i:Interaction.Triggers>
<i:EventTrigger EventName="Checked">
<i:InvokeCommandAction Command="{Binding CheckAllRowsCommand}"/>
</i:EventTrigger>
<i:EventTrigger EventName="Unchecked">
<i:InvokeCommandAction Command="{Binding UncheckAllRowsCommand}"/>
</i:EventTrigger>
</i:Interaction.Triggers>
</CheckBox>
</DataGridTemplateColumn.Header>
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<CheckBox IsChecked="{Binding Selected, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" />
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
My xaml.cs
public partial class MyTableView: UserControl
{
public MyTableView()
{
InitializeComponent();
DataContext = new MyTableViewModel();
}
}
MyTableViewModel.cs
public class MyTableViewModel: BaseViewModel
{
public MyTableViewModel() : base()
{
CheckAllRowsCommand= new CheckAllRowsCommand(this);
UncheckAllRowsCommand = new UncheckAllRowsCommand(this);
}
public ICommand CheckAllRowsCommand{ get; }
public ICommand UncheckAllRowsCommand{ get; }
}
CheckAllRowsCommand
public class CheckAllRowsCommand: BaseCommand
{
public CheckAllRowsCommand(MyTableViewModel parent) : base(parent)
{
}
public override bool CanExecute(object parameter)
{
return true;
}
public override void Execute(object parameter)
{
// Set the Selected property of each data row
}
}
When running this, I get the following error:
System.Windows.Data Error: 40 : BindingExpression path error: 'CheckAllRowsCommand' property not found on 'object' ''CheckBox' (Name='')'. BindingExpression:Path=CheckAllRowsCommand; DataItem='CheckBox' (Name=''); target element is 'InvokeCommandAction' (HashCode=47015983); target property is 'Command' (type 'ICommand')
Any help would be greatly appreciated.