伊夫注意到这个几次使用带有命令菜单时,它们不是非常动态的,检查了这一点。我创建从颜色集合的菜单,我用它在DataGrid中以颜色为列。反正当我第一次调出菜单(它的上下文菜单)的命令参数绑定情况,并将其绑定到列的上下文菜单被打开了。然而接下来的时间我把它似乎WPF缓存菜单,它不重新绑定命令参数。这样我就可以对初始列只设置颜色的上下文菜单上出现。

我身边有这样的情况在过去通过使菜单完全动态和销毁集合时菜单关闭,迫使在下一次开启重建了,我不喜欢这个技巧。任何人有一个更好的办法?

    <MenuItem
       Header="Colour"
       ItemsSource="{Binding RelativeSource={RelativeSource AncestorType={x:Type local:ResultEditorGrid}}, Path=ColumnColourCollection}"
       ItemTemplate="{StaticResource colourHeader}" >
       <MenuItem.Icon>
          <Image
             Source="{StaticResource ColumnShowIcon16}" />
       </MenuItem.Icon>
       <MenuItem.ItemContainerStyle>
          <Style
             TargetType="MenuItem"
             BasedOn="{StaticResource systemMenuItemStyle}">
             <!--Warning dont change the order of the following two setters
                                otherwise the command parameter gets set after the command fires,
                                not mush use eh?-->
             <Setter
                Property="CommandParameter">
                <Setter.Value>
                   <MultiBinding>
                      <MultiBinding.Converter>
                         <local:ColumnAndColourMultiConverter/>
                      </MultiBinding.Converter>
                      <Binding RelativeSource="{RelativeSource AncestorType={x:Type DataGridColumnHeader}}" Path="Column"/>
                      <Binding Path="."/>
                   </MultiBinding>
                </Setter.Value>
             </Setter>
             <Setter
                Property="Command"
                Value="{Binding RelativeSource={RelativeSource AncestorType={x:Type local:ResultEditorGrid}}, Path=ColourColumnCommand}" />
          </Style>
       </MenuItem.ItemContainerStyle>
    </MenuItem>
有帮助吗?

解决方案

问题是,文本菜单的是显然属于自己的视觉树的根我读的地方,它需要在DataContext其父母,但只有一次上载,因此,如果DataContext的改变的菜单项的父母没有。 (不幸的是我不能找到一个链接进行右不)

我以前也遇到过这个问题,我所做的就是使用的约什·史密斯的虚拟分支模式。这是相当的技术,但文章帮助我理解得很好了事情的原委与该可视树无稽之谈。

基本上你创建这个桥结合到视图的DataContext的。这座桥是创建作为一个静态资源,允许您从哪怕是视觉树之外的上下文菜单绑定到它。

添加到您的XAML:

<Window.Resources>
   <!-- This is the "root node" in the virtual branch
   attached to the logical tree. It has its
   DataContext set by the Binding applied to the
   Window's DataContext property. -->
   <FrameworkElement x:Key="DataContextBridge" />
</Window.Resources>

<Window.DataContext>
   <!-- This Binding sets the DataContext on the "root node"
   of the virtual logical tree branch.  This Binding
   must be applied to the DataContext of the element
   which is actually assigned the data context value. -->
   <Binding
    Mode="OneWayToSource"
    Path="DataContext"
    Source="{StaticResource DataContextBridge}"
   />
</Window.DataContext>

这是我发言的桥梁。它采用的datacontext和__pushes it_向桥的datacontext,它是(前面说过)静态资源。

然后你只需这样的文本菜单的DataContext的:

 DataContext="{Binding
               Source={StaticResource DataContextBridge},
               Path=DataContext}"

现在扔掉所有的相对路径功能等,并使用常规的菜单项里面绑定的,你应该罚款。在DataContext将更新照常进行。

只需一个音符:

您显然必须有在DataContext的一些属性,以辨别哪些命令来使用,但我敢肯定,你自己看着办吧。这种解决方案只是与文本菜单的方式更新不要涉及

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top