【问题标题】:Command Binding Failure in MenuItemMenuItem 中的命令绑定失败
【发布时间】:2013-09-17 12:55:26
【问题描述】:

我在资源字典中有以下MenuItem 模板

<Style x:Key="RecentMenuItem" 
       TargetType="{x:Type MenuItem}" 
       BasedOn="{StaticResource {x:Type MenuItem}}">
    <Setter Property="Command" Value="{Binding RelativeSource={RelativeSource 
        Mode=FindAncestor, AncestorType=MenuItem}, Path=DataContext.LoadRecentItemCommand}" />
    <Setter Property="CommandParameter" Value="{Binding RelativeSource={RelativeSource Mode=Self}, Path=Header}"/>
    <Setter Property="HeaderTemplate" >
        <Setter.Value>
            <DataTemplate>
                <TextBlock>
                    <TextBlock.Text>
                        <MultiBinding StringFormat="{}{0}. {1}">
                            <Binding Path="(ItemsControl.AlternationIndex)" 
                                     RelativeSource="{RelativeSource FindAncestor, AncestorType={x:Type MenuItem}}"/>
                            <Binding Path="FullFileName"/>
                        </MultiBinding>
                    </TextBlock.Text>
                </TextBlock>
            </DataTemplate>
        </Setter.Value>
    </Setter>
</Style>

这样使用

<Menu>
    <MenuItem Header="FILE">
        ...
        <MenuItem Header="_Recent Studies" 
                  ItemsSource="{Binding RecentFiles}"
                  AlternationCount="{Binding Path=Items.Count, 
                                             Mode=OneWay, 
                                             RelativeSource={RelativeSource Self}}" 
                  ItemContainerStyle="{StaticResource RecentMenuItem}"/>
    <MenuItem/>

Command 上的绑定不起作用(我可以通过 Snoop[dog] 看到这一点)。

上述命令绑定有什么问题,如何解决?

感谢您的宝贵时间。

【问题讨论】:

  • 也许您需要删除Header="_Recent Studies" 或者您需要绑定到HeaderTemplate EDITFullFileName
  • 如果你想点击子MenuItems,那么你肯定需要将CommandBehaviour对象添加到它们,而不是它们的父项?
  • @Sheridan 我在问题的 PS 中尝试过这种方法。由于某种原因,这甚至不会触发!?
  • @Killercam 孩子需要绑定到父母的 DataContext 才能访问 LoadRecentItemCommandHeader... 你在这样做吗?
  • @nmclean 我想我和&lt;Setter Property="Command" Value="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=MenuItem}, Path=DataContext.LoadRecentFileCommand}" /&gt; &lt;Setter Property="CommandParameter" Value="{Binding}" /&gt;

标签: wpf binding command menuitem


【解决方案1】:

我试过了,它工作得很好......我在命令参数中得到了完整的文件名:这里命令是在我的窗口的虚拟机中定义的,所以你必须相应地更新它(如果你使用了用户控件)。

     <Menu>
          <MenuItem Header="_Recent Studies" 
           ItemsSource="{Binding Files}"
           ItemContainerStyle="{StaticResource RecentMenuItem}"/>
    </Menu>


    <Style x:Key="RecentMenuItem" 
       TargetType="{x:Type MenuItem}" 
       BasedOn="{StaticResource {x:Type MenuItem}}">
            <Setter Property="Command" Value="{Binding DataContext.MyCommand, RelativeSource={RelativeSource AncestorType={x:Type Window}}}" />
            <Setter Property="CommandParameter" Value="{Binding FullName}"/>
            <Setter Property="HeaderTemplate" >
                <Setter.Value>
                    <DataTemplate>
                        <TextBlock>
                            <TextBlock.Text>
                            <Binding Path="FullName"/>
                    </TextBlock.Text>
                        </TextBlock>
                    </DataTemplate>
                </Setter.Value>
            </Setter>
        </Style>

【讨论】:

  • 你还有什么想法吗?
  • 非常感谢您的宝贵时间。这仍然行不通。一方面是 Windo 是 MahAppsControls:MetroWindow 类型,我显然已经替换了它,但它仍然无法正常工作。 S对不起……
  • 您的菜单是否使用这种方法填充?你在输出窗口有什么错误吗?因为它对我有用
  • 层次结构是&lt;Menu&gt;&lt;MenuItem&gt; 然后这个&lt;MenuItem&gt; 如我在问题中所示。我正在填充绑定的MenuItem 的父级本身就是MenuItem...
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-07-15
  • 1970-01-01
  • 2013-07-23
  • 2016-11-17
  • 1970-01-01
相关资源
最近更新 更多