Question

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.

Was it helpful?

Solution 2

<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..

OTHER TIPS

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>

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

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top