2014-01-20 39 views
1

我有RibbonApplicationMenu在我的应用程序类似于此示例:不同的行为命令

<RibbonApplicationMenu> 
    <RibbonApplicationMenuItem Header="Open Project..." Command="{Binding OpenProjectCommand}" /> 
    <RibbonApplicationMenuItem Header="Save Project..." Command="{Binding SaveProjectCommand}" /> 
    <RibbonApplicationMenuItem Header="Exit" Command="{Binding CloseWindowCommand}" CommandParameter="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type RibbonWindow}}}" /> 
    <RibbonApplicationMenu.FooterPaneContent> 
     <RibbonButton Label="Exit" Command="{Binding CloseWindowCommand}" CommandParameter="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type RibbonWindow}}}" /> 
    </RibbonApplicationMenu.FooterPaneContent> 
</RibbonApplicationMenu> 

private void CloseWindow (Object parameter) 
{ 
    ((Window) parameter).Close(); 
} 

在该例子中存在具有通过相同的参数绑定到相同的命令和RibbonApplicationMenuItem项RibbonButton和。该命令执行CloseWindow()函数。我感到好奇的是,当单击RibbonApplicationMenuItem时,该函数的参数是指向RibbonWindow的指针。但是,单击RibbonButton时,该函数的参数为​​空。

为什么行为会有所不同?

回答

1

FooterPaneContent设置为另一个控件(RibbonButton)混淆逻辑树,这就是为什么RelativeAncestor不起作用。

在换句话说,即使你绑定到按钮本身,与LogicalTreeHelper遍历是不行的(VisualTreeHelper不会工作,要么因为面板是一个PopUp,住在一个单独的视觉树):

<r:RibbonApplicationMenu.FooterPaneContent> 
    <r:RibbonButton Label="Exit" Command="{Binding CloseWindowCommand}" CommandParameter="{Binding RelativeSource={RelativeSource Self}}" /> 
</r:RibbonApplicationMenu.FooterPaneContent> 
private void CloseWindow(object parameter) 
{ 
    RibbonButton _button = (RibbonButton)parameter; 

    // _appMenu will be null 
    DependencyObject _appMenu = LogicalTreeHelper.GetParent(_button); 
} 

所以,你的选择是:

  1. 绑定到自我和使用_button.Ribbon属性来获取Ribbon并遍历逻辑树以获得RibbonWindow

  2. 设置ContentTemplate而不是Content。但要小心传播DataContext

<r:RibbonApplicationMenu.FooterPaneContentTemplate> 
    <DataTemplate> 
     <r:RibbonButton Label="Exit" DataContext="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type r:RibbonApplicationMenu}}, Path=DataContext}" Command="{Binding Path=CloseWindowCommand}" CommandParameter="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type r:RibbonWindow}}}" /> 
    </DataTemplate> 
</r:RibbonApplicationMenu.FooterPaneContentTemplate> 
private void CloseWindow(object parameter) 
    { 
     ((Window)parameter).Close(); 
    } 
+1

会是什么把一个关闭按钮在页脚的正常方式? –

+1

我想说第二个选项。 (对不起,延迟!) – Dusan

+0

第二个选项解决了我的问题。奇怪的是,即使它找不到我的CloseWindowCommand,我在Output窗口中也没有绑定错误。 –