如何将 CheckBox.InputBindings MouseBinding 绑定到视图模型中的命令,而不是 DataGrid 对象项?
How to make binding of CheckBox.InputBindings MouseBinding to a command in viewmodel, instead of DataGrid object item?
我在将 LeftClick
MouseAction 绑定到 Command
时遇到问题。我在 SO 中找到了大量关于命令绑定到视图模型的示例,但与我的不相似。由于我的 WindowManager
服务内部的上下文绑定,我的似乎非常独特。
我已经尝试过方法:
- this
- this
{Binding Path=Window.DataContext.SelectedCountCommand}
他们都给了我:
Binding expression path error: ('DataContext' / 'Window' / 'SelectedCountCommand') property not found on
'object' CheckedProject (...)
我的 window 数据绑定发生在我的 WindowManager 服务中,我在其中绑定 DataContext 如下:
/// <summary>
/// Creates instance of new window basing on window type, returns new instance of WindowModel object.
/// </summary>
/// <param name="type">Window type</param>
/// <param name="viewModel">View model related to the window</param>
/// <returns>WindowModel object</returns>
private WindowModel GetWindowModelFromWindowName(Type type, object viewModel)
{
Window window = (Window)Activator.CreateInstance(type);
window.DataContext = viewModel;
//some model instance stuff here
return model;
}
而且我不能在我的 xaml 中使用这样的东西:
<Window.DataContext>
<local:ViewModelName/>
</Window.DataContext>
我的复选框,我试图为他做一个 MouseBinding
,在 DataGrid
:
里面
<DataGridTemplateColumn Header="Check" Width="50">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<CheckBox IsChecked="{Binding Path=Checked, UpdateSourceTrigger=PropertyChanged}" HorizontalAlignment="Center">
<CheckBox.InputBindings>
<MouseBinding Command="{Binding Path=DataContext.SelectedCountCommand}" MouseAction="LeftClick" />
</CheckBox.InputBindings>
</CheckBox>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
如何将它绑定到我的视图模型中的 SelectedCountCommand
,而不是 DataGrid
项目,其中 CheckedProject
是一种 ObservableCollection 项目,DataGrid` 绑定到?
更新:
在 CheckBoxes 上使用 EventTrigger
时出现相同的异常:
<CheckBox IsChecked="{Binding Path=Checked, UpdateSourceTrigger=PropertyChanged}" HorizontalAlignment="Center">
<i:Interaction.Triggers>
<i:EventTrigger EventName="Unchecked">
<i:InvokeCommandAction Command="{Binding SelectedCountCommand}"/>
</i:EventTrigger>
<i:EventTrigger EventName="Checked">
<i:InvokeCommandAction Command="{Binding SelectedCountCommand}"/>
</i:EventTrigger>
</i:Interaction.Triggers>
</CheckBox>
由于这是在数据网格模板列中,上下文将是 DataGridRow。您的服务正在为 window 设置数据上下文,我们必须将命令绑定到 window 数据上下文。
尝试:
Command="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType= {x:Type Window}}, Path=DataContext.SelectedCountCommand}"
如果您的命令需要命令参数,您可能需要按要求提供它们。
我在将 LeftClick
MouseAction 绑定到 Command
时遇到问题。我在 SO 中找到了大量关于命令绑定到视图模型的示例,但与我的不相似。由于我的 WindowManager
服务内部的上下文绑定,我的似乎非常独特。
我已经尝试过方法:
- this
- this
{Binding Path=Window.DataContext.SelectedCountCommand}
他们都给了我:
Binding expression path error: ('DataContext' / 'Window' / 'SelectedCountCommand') property not found on 'object' CheckedProject (...)
我的 window 数据绑定发生在我的 WindowManager 服务中,我在其中绑定 DataContext 如下:
/// <summary>
/// Creates instance of new window basing on window type, returns new instance of WindowModel object.
/// </summary>
/// <param name="type">Window type</param>
/// <param name="viewModel">View model related to the window</param>
/// <returns>WindowModel object</returns>
private WindowModel GetWindowModelFromWindowName(Type type, object viewModel)
{
Window window = (Window)Activator.CreateInstance(type);
window.DataContext = viewModel;
//some model instance stuff here
return model;
}
而且我不能在我的 xaml 中使用这样的东西:
<Window.DataContext>
<local:ViewModelName/>
</Window.DataContext>
我的复选框,我试图为他做一个 MouseBinding
,在 DataGrid
:
<DataGridTemplateColumn Header="Check" Width="50">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<CheckBox IsChecked="{Binding Path=Checked, UpdateSourceTrigger=PropertyChanged}" HorizontalAlignment="Center">
<CheckBox.InputBindings>
<MouseBinding Command="{Binding Path=DataContext.SelectedCountCommand}" MouseAction="LeftClick" />
</CheckBox.InputBindings>
</CheckBox>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
如何将它绑定到我的视图模型中的 SelectedCountCommand
,而不是 DataGrid
项目,其中 CheckedProject
是一种 ObservableCollection 项目,DataGrid` 绑定到?
更新:
在 CheckBoxes 上使用 EventTrigger
时出现相同的异常:
<CheckBox IsChecked="{Binding Path=Checked, UpdateSourceTrigger=PropertyChanged}" HorizontalAlignment="Center">
<i:Interaction.Triggers>
<i:EventTrigger EventName="Unchecked">
<i:InvokeCommandAction Command="{Binding SelectedCountCommand}"/>
</i:EventTrigger>
<i:EventTrigger EventName="Checked">
<i:InvokeCommandAction Command="{Binding SelectedCountCommand}"/>
</i:EventTrigger>
</i:Interaction.Triggers>
</CheckBox>
由于这是在数据网格模板列中,上下文将是 DataGridRow。您的服务正在为 window 设置数据上下文,我们必须将命令绑定到 window 数据上下文。
尝试:
Command="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType= {x:Type Window}}, Path=DataContext.SelectedCountCommand}"
如果您的命令需要命令参数,您可能需要按要求提供它们。