zoukankan      html  css  js  c++  java
  • How to bind a Command on a ContextMenu within a DataTemplate using MVVM

    Since the Popuup control has it's separate visual tree, you cannot use find ancestor to find the Grid. The trick here is to use the PlacementTarget property, that contains the element, the ContextMenu is aligned to, what is the Grid in our case.

    But this is only half of the solution. Because of the data template, the DataContext is set to a dataitem, and not the view model. So you need another relative source lookup, to find the view model. Trick Nr. 2 is to use the Tag property to bind the view model from outside to the grid, which is the PlacementTargetused above. And there we are.

    <DataTemplate>
       <Grid Tag="{Binding DataContext, RelativeSource={RelativeSource AncestorType={x:Type ListBox}}}">
          <Grid.ContextMenu>
              <ContextMenu DataContext="{Binding Path=PlacementTarget.Tag, RelativeSource={RelativeSource Self}}">
                 <MenuItem Content="Cut" Command="{Binding CutCommand}" />
                 <MenuItem Content="Copy" Command="{Binding CopyCommand}" />
                 <MenuItem Content="Paste" Command="{Binding PasteCommand}" />
              </ContextMenu>
          </Grid.ContextMenu>
       </Grid>
    </DataTemplate>
  • 相关阅读:
    AJAX---跨域相关概念
    AJAX---jQuery全局事件处理函数
    AJAX---load方法
    AJAX---jQuery 中的ajax回调事件
    AJAX---jQuery 中的ajax方法的基本使用
    AJAX---基本的封装
    AJAX---模板引擎的使用
    AJAX---扩展点
    AJAX---如何处理服务端响应的数据
    AJAX---响应数据格式
  • 原文地址:https://www.cnblogs.com/3Tai/p/4778402.html
Copyright © 2011-2022 走看看