C# 绑定到复选框的check命令时出现BindingExpression路径错误
我有一个带有一列复选框的C# 绑定到复选框的check命令时出现BindingExpression路径错误,c#,wpf,xaml,data-binding,C#,Wpf,Xaml,Data Binding,我有一个带有一列复选框的DataGrid,我在DataGrid标题中有一个复选框,选中该复选框时,会选中所有复选框。基于,我有一个绑定到“checked”事件的命令和另一个绑定到“unchecked”事件的命令 所有相关文件如下(当然是简化文件) 我的XAML: <DataGridTemplateColumn Width="40"> <DataGridTemplateColumn.Header> <CheckBox>
DataGrid
,我在DataGrid
标题中有一个复选框,选中该复选框时,会选中所有复选框。基于,我有一个绑定到“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>
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; }
}
检查所有行命令
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路径错误:
在“对象”“复选框”上找不到“CheckAllRowsCommand”属性
(名称=“”)。BindingExpression:Path=CheckAllRowsCommand;
DataItem='CheckBox'(名称='');目标元素是“InvokeCommandAction”
(HashCode=47015983);目标属性为“Command”(类型为“ICommand”)
非常感谢您的帮助。CheckAllRowsCommand是ViewModel中的属性,而不是CheckBox中的属性。尝试通过DataGrid绑定到viewModel(它应该继承了DataContext):
这是一个比ASh发布的更详细的解决方案,但在我看来更灵活
<CheckBox DataContext="{Binding Path=DataContext, RelativeSource={RelativeSource AncestorType={x:Type DataGrid}}}">
...
</CheckBox>
...
问题是,模板中的绑定似乎使用了复选框
作为数据上下文,而您可能认为它将使用包含控件的数据上下文。WPF有时会很奇怪,每当我看到这样的错误时(即BindingExpression路径错误:'blah'属性未在'object''blah'
),我总是假设数据上下文推断不正确
我建议的解决方法是使用相对源强制设置
复选框
上的数据上下文。例如,可以使用相对源声明性地说“使用此类型的第一个父级”。此绑定表示“将我的DataContext属性设置为DataGrid
类型的我上方第一个父级的DataContext
”。从逻辑上讲,DataGrid
类型的复选框的第一个父项将是您想要的。然后,您的内部命令绑定不需要更改,一切都应该按预期工作。您可以将复选框的DataContext
绑定到父UserControl
的DataContext
,在父UserControl
中使用{RelativeSource}
定义命令属性:
<CheckBox DataContext="{Binding DataContext, RelativeSource={RelativeSource AncestorType=UserControl}}">
<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>
<CheckBox DataContext="{Binding Path=DataContext, RelativeSource={RelativeSource AncestorType={x:Type DataGrid}}}">
...
</CheckBox>
<CheckBox DataContext="{Binding DataContext, RelativeSource={RelativeSource AncestorType=UserControl}}">
<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>