Binding from context menu item to parent control
Asked Answered
O

2

14

I have a control, on that control is a command called SaveToClipboardCommand. I want to bind a context menu item command to that command so that when i click it, the copy to clipboard command is executed.

<Control x:Name="Control">
    <Control.ContextMenu>
        <ContextMenu>
            <MenuItem Command={"Bind to SaveToClipboardCommand here"} Header="Some Header" />
        </ContextMenu>
    </Control.ContextMenu/>
</Control>

The control (for argument sake) is defined like this:

partial class Control
{
      private ICommand _saveToClipboard;
      public ICommand SaveToClipboardCommand
      {
          get
          {
              if (_saveToClipboard == null)
              {
                  _saveToClipboard = new RelayCommand(
                         x=> SaveToClipboard());
              }
              return _saveToClipboard;
          }
     }
}

I have tried using RelativeSource and ElementName based bindings but both are failing. Is what i am trying to do even possible?

Thanks!

Ornamented answered 7/10, 2010 at 4:15 Comment(3)
What RelativeSource have you tried? Did you try FindAncestor?Subassembly
{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type Control}}, Path-SaveToClipboardCommand}Ornamented
Are you trying to bind to a control's context menu to a command defined within the control? If so then you should do the binding in that control's XAML and set its DataContext to 'this'. If you are binding to the command member outside the scope of 'Control' class then the DataContext should have it so that it is resolved.Elbertina
T
21

EDIT (after showing how the control is exposed): Well ContextMenu is somewhat tricky, because it's actually not part of the same visual tree. Try doing this:

<MenuItem Command="{Binding Path=PlacementTarget.SaveToClipboardCommand,
    RelativeSource={RelativeSource AncestorType=ContextMenu}}"/>

Original answer

Is this command exposed as a public property of the Control? If the command is actually exposed in a ViewModel hanging of the control's DataContext, to do the following:

Command={Binding ElementName=Control, Path=DataContext.SaveToClipboardCommand}

Can you show how this command is currently exposed?

Tytybald answered 11/10, 2010 at 23:42 Comment(4)
Command is in edit. RelayCommand is just a standard ICommand implementationOrnamented
Well ContextMenu is somewhat tricky, because it's actually not part of the same visual tree. Try doing this: <MenuItem Command="{Binding Path=PlacementTarget.SaveToClipboardCommand, RelativeSource={RelativeSource AncestorType=ContextMenu}}"/>Tytybald
i'll have a look into this tomorow :) thanks mate. Bloody context menusOrnamented
Excellent! it worked. I will have to look more into the placement target binding :) thanks for your assistanceOrnamented
R
-3
Command={Binding ElementName=Control, Path=SaveToClipboardCommand}

HTH

Rolph answered 7/10, 2010 at 7:10 Comment(1)
Tried that, but it isnt working for me :( says 'Binding cannot find source...'Ornamented

© 2022 - 2024 — McMap. All rights reserved.