0

I have a button on click on that button opening a context menu, now clicking on the context menus is to be binded to viewModel. But its not happening.

<Button Content="Copy" Tag="{Binding LinkViewModel, RelativeSource={RelativeSource Mode=Self}}" Command="{Binding LinkCopyCommand, UpdateSourceTrigger=PropertyChanged}" >
    <Button.ContextMenu>
        <ContextMenu>
           <MenuItem Header="Copy Download link " Command="{Binding Path=Parent.PlacementTarget.Tag.CopyViewCommand, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=ContextMenu}}" />
           <MenuItem ... />
        </ContextMenu>
    </Button.ContextMenu> 
</Button>

I have tried the tag property but it seems to me that its not working. The viewmodel is working fine if I bind to the button itself, but the contextMenu dataBinding is not working.

EDIT:

Now as the code is working after discussion, I think to post it here.

What the changes I made is I put UpdateSourceTrigger="Propertychanged" here is the code

<Button Content="Copy" Tag="{Binding LinkViewModel, RelativeSource={RelativeSource Mode=Self}}" Command="{Binding LinkCopyCommand, UpdateSourceTrigger=PropertyChanged}" >
    <Button.ContextMenu>
       <ContextMenu Width="{Binding RelativeSource={RelativeSource Self}}">
          <MenuItem Header="Copy View link " Command="{Binding CopyViewCommand, UpdateSourceTrigger=PropertyChanged}" />
          <MenuItem ... />
       </ContextMenu>
    </Button.ContextMenu> 
</Button>

However I don't know how come suddenly it works, it has to work with tag property in case of Button Context menu. If anybody put some light into this I think many people like me who are new WPF and data binding will be benefited.

Cœur
  • 37,241
  • 25
  • 195
  • 267
Debhere
  • 1,055
  • 2
  • 19
  • 41

3 Answers3

2

I'm assuming you are using this Button inside the UserControl. Please try below code

<Button Content="Copy" Tag="{Binding LinkViewModel, RelativeSource={RelativeSource Mode=Self}}" Command="{Binding LinkCopyCommand, UpdateSourceTrigger=PropertyChanged}" >
        <Button.ContextMenu>
            <ContextMenu>
               <MenuItem Header="Copy Download link " Command="{Binding RelativeSource={RelativeSource FindAncestor,  AncestorType={x:Type UserControl}}, Path=DataContext.CopyViewCommand}" />
               <MenuItem ... />
            </ContextMenu>
        </Button.ContextMenu> 
    </Button>
Mukesh Rawat
  • 2,047
  • 16
  • 30
1
<Button Content="Copy" Command="{Binding LinkCopyCommand, UpdateSourceTrigger=PropertyChanged}" >
<Button.ContextMenu>
    <ContextMenu>
       <MenuItem Header="Copy Download link " Command="{Binding Path=CopyViewCommand}" />
       <MenuItem ... />
    </ContextMenu>
</Button.ContextMenu> 

CopyViewCommand Bound directly from your DataContext... which is your ViewModel..

Sankarann
  • 2,625
  • 4
  • 22
  • 59
  • Let me understand the code, in Button Tag="{Binding LinkViewModel, RelativeSource={RelativeSource Mode=Self}}" so tag will be binded to the viewModel right? Then this is binded to viewmodel CopyViewCommand, but its not working after the updated code. – Debhere Feb 06 '14 at 07:02
  • I think the Problem is here.. Is `LinkViewModel` is present in the Button.. Since your source is Button(Control) – Sankarann Feb 06 '14 at 07:07
  • where does `LinkViewModel` property available? – Sankarann Feb 06 '14 at 07:08
  • Please see my updated code, can you brief why it's working now. I tried to use tag Property but without that it's working fine. Yes LinkViewModel is attached to Button. – Debhere Feb 06 '14 at 07:29
0

You have to setyour ContextMenu's DataContext to your ViewModel. One way to do this is by having an Opened eventhandler for the context menu.

Take a look at my answer in the below link -

Context Menu items command binding WPF using MVVM

Community
  • 1
  • 1
dev27
  • 605
  • 1
  • 7
  • 15