Hello,
I'm following an MVVM pattern, where I have a command in my view model called "GoCommand". I'm displaying an ellipse in my XamDataGrid, and when the user clicks on the ellipse, my GoCommand is called. I have all of this working fine, but I'm struggling with getting the current record passed to the view model. In my view model command code, the parameter is always null, but I'm sure that's just because I can't get the XAML binding right.
I have this style in my XAML:
<Style x:Key="ellipseCVP" TargetType="{x:Type igDP:CellValuePresenter}"> <Setter Property="ContentTemplate"> <Setter.Value> <DataTemplate> <Ellipse x:Name="GoIcon" Width="12" Height="12"> <Ellipse.InputBindings> <MouseBinding MouseAction="LeftClick" Command="{Binding Path=DataContext.GoCommand, RelativeSource={RelativeSource AncestorType={x:Type igDP:XamDataGrid}}}" CommandParameter="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=Record.DataItem}"/> </Ellipse.InputBindings> </Ellipse> </DataTemplate> </Setter.Value> </Setter></Style>
I have this field in my XamDataGrid:
<igDP:Field Name="IsReady" Label="Ready?"> <igDP:Field.Settings> <igDP:FieldSettings CellValuePresenterStyle="{StaticResource ellipseCVP}"/> </igDP:Field.Settings> </igDP:Field>
As I said, everything works fine except that the CommandParameter is always null when it reaches my view model. I've tried hard-coding a value just to make sure the "plumbing is correct" and that worked fine. I know the problem is that I don't have the binding quite right on the CommandParameter of the MouseBinding. Do you have any suggestions for the correct binding?
Thanks
Steve
Thanks Tacho. Unfortunately, that's one of the things I tried as well, but it didn't work either. After a few hours yesterday, I finally got this to work. Hopefully it might help someone else...
<MouseBinding MouseAction="LeftClick" Command="{Binding Path=DataContext.GoCommand, RelativeSource={RelativeSource AncestorType={x:Type igDP:XamDataGrid}}}" CommandParameter="{Binding RelativeSource={RelativeSource AncestorType={x:Type igDP:CellValuePresenter}}, Path=Record.DataItem}"/>
Thanks.
Hello Steve,
Thank you for the code-snippet you have provided.
In order to pass the DataItem of the current CellValuePresenter's Record as a CommandParameter inside the MouseBinding, an approach I can suggest you is to set the RelativeSource binding for the CommandParameter to Self.This way you will bind for the CellValuePresenter itself, which also has a Record property, which you can use for accessing the DataItem.
<MouseBinding MouseAction="LeftClick" Command="{Binding Path=DataContext.GoCommand, RelativeSource={RelativeSource AncestorType={x:Type igDP:XamDataGrid}}}" CommandParameter="{Binding RelativeSource={RelativeSource Self}, Path=Record.DataItem}"/>
If you have any further questions, please let me know.