0

I'm trying to send property thats set by dp to my viewmodel (bound via datacontext) via command parameter a Dependency Property in the code behind of my view. The property (ParentUserControl) seems to be initialized correctly when breaking into it, however I can't seem to be able to send it. I've tried the two bindings below

<DataGrid.ContextMenu>
    <ContextMenu>
        <MenuItem Command="{Binding CommandTest}"
                  CommandParameter="{Binding ParentUserControl, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type local:MyView}}}" />
    </ContextMenu>
</DataGrid.ContextMenu>

and

<ContextMenu>
    <MenuItem Command="{Binding CommandTest}"
              CommandParameter="{Binding ParentUserControl, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type UserControl}}}" />
</ContextMenu>

I'm using mvvmlight relay commands as shown below however the parameter pased in the method test() is always null

CommandTest = new RelayCommand<object>(x => test(x));

This is the dependency property attached in the code behind of the view:

public static readonly DependencyProperty ParentUserControlProperty = DependencyProperty.Register(
    "ParentUserControl", typeof(UserControl), typeof(MyView), new PropertyMetadata(default(UserControl)));

public UserControl ParentUserControl
{
    get { return (UserControl) GetValue(ParentUserControlProperty); }
    set { SetValue(ParentUserControlProperty, value); }
}
A.A
  • 743
  • 1
  • 8
  • 20
  • The `ContextMenu` breaks the Visual Tree, you can bridge the gap using `PlacementTarget` as explained [here](http://stackoverflow.com/questions/3668654/relativesource-binding-from-a-tooltip-or-contextmenu). – bab7lon Feb 26 '17 at 21:59
  • I'm not sure if this is what I'm looking for, the command is already bound to the viewmodel and works fine, it's command parameter I'm having difficulties with – A.A Feb 26 '17 at 23:41
  • Although I did try it on CommandParameter, but had no luck. – A.A Feb 26 '17 at 23:57

2 Answers2

0

Use the name of the View to find the parameter

CommandParameter="{Binding ElementName=MyViewName, Path=ParentUserControl}"

Also add a dummy binding of ParentUserControl on the MyView with its ViewModel (and check if it is working at that level). I mean, try create a UserControl Parent property on the View Model, binding the dependency of MyView and then try

CommandParameter="{Binding ElementName=MyViewName, Path=DataContext.Parent}"

In the latter case, since it's already in the View Model, maybe you don't even need the parameter. By the way, from a MVVM design pattern perspective, you should not pass a Control as a parameter to the ViewModel...

0

You should use something like this:

<DataGrid
    Tag="{Binding RelativeSource={RelativeSource AncestorType={x:Type MyView}}}">
    <DataGrid.ContextMenu>
        <ContextMenu>
            <MenuItem Command="{Binding CommandTest}"
              CommandParameter="{Binding PlacementTarget.Tag.ParentUserControl, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type local:ContextMenu}}}" />
        </ContextMenu>
    </DataGrid.ContextMenu>
</DataGrid>

You bind the Tag of the DataGrid to MyView. In MenuItem you seatch for the ContextMenu, use its PlacementTarget (which is the DataGrid) and its Tag (which is the MyView).

rmojab63
  • 3,513
  • 1
  • 15
  • 28