2014-01-20 169 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窗口中也沒有綁定錯誤。 –

相關問題