【发布时间】:2020-03-15 02:26:31
【问题描述】:
我有一个带有一列复选框的DataGrid,并且我在DataGrid 标头中有一个复选框,选中时会选中所有复选框。基于this answer,我有一个绑定到“checked”事件的命令和另一个绑定到“unchecked”事件的命令。
所有相关文件都在下面(当然是简化的)
我的 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>
我的 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
}
}
运行时出现以下错误:
System.Windows.Data 错误:40:BindingExpression 路径错误: 在“对象”“CheckBox”上找不到“CheckAllRowsCommand”属性 (名称='')'。绑定表达式:路径=CheckAllRowsCommand; DataItem='CheckBox' (Name='');目标元素是“InvokeCommandAction” (哈希码=47015983);目标属性是“命令”(输入“ICommand”)
任何帮助将不胜感激。
【问题讨论】:
标签: c# wpf xaml data-binding