I have a problem with binding LeftClick
MouseAction to the Command
. I found in SO loads of examples about command binding to the viewmodel, but not similar like mine. Mine seems to quite unique due to context binding inside of my WindowManager
service.
I already tried approaches:
All of them gave me:
Binding expression path error: ('DataContext' / 'Window' / 'SelectedCountCommand') property not found on 'object' CheckedProject (...)
My window data binding happens in my WindowManager service, where I am binding DataContext as follws:
/// <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;
}
And I am not able to use in my xaml anything like this:
<Window.DataContext>
<local:ViewModelName/>
</Window.DataContext>
And my checkbox, that I try to do a MouseBinding
for him, is inside of 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>
How to binde it to SelectedCountCommand
inside of my viewmodel, instead of DataGrid
item, where CheckedProject
is an type of ObservableCollection item, that DataGrid` is binded to?
UPDATE:
I am getting same exception when using EventTrigger
on CheckBoxes:
<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>
Since this is within the datagrid template column, the context will be the DataGridRow. Your service is setting the datacontext for the window and we have to bind the command to the window datacontext.
Try :
Command="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType= {x:Type Window}}, Path=DataContext.SelectedCountCommand}"
If your command expects command parameters, you may need to supply them as required.