2012-08-15 86 views
1

我正在使用Prism和使用功能區庫構建複合WPF應用程序。有一件事情我很難與跨區域的命令。棱鏡和功能區:使用命令跨越區域

例如,我在我的'RibbonRegion'中使用了我的功能區,在'MainRegion'中使用了一個網格視圖。比方說,我希望我的功能區中的一個按鈕彈出一個消息框,在網格視圖中顯示當前選定的項目,我該怎麼做?

簡單的方法是使用EventAggregator,但我擔心,如果我有一大堆訂閱者只是點擊按鈕,我只是要求內存泄漏問題。

是否有一種方法可以獲得跨區域命令,以便單擊我的「RibbonRegion」中的按鈕將在網格視圖中獲取所選項目並彈出具有該值的消息框?

回答

1

您可以使用System.Windows.Input.RoutedUICommand

首先,你需要如下聲明你的命令:

public static class Commands 
{ 
    public static readonly RoutedUICommand TestCommand = new RoutedUICommand("Test Command", 
     "Test Command", typeof(Commands)); 
} 

然後在您的RibbonRegion XAML:

<my:RibbonButton Command="{x:Static cmd:Commands.TestCommand}" ... 
在MainRegion XAML

然後:

<UserControl.CommandBindings> 
    <CommandBinding CanExecute="OnTestCanExecute" 
        Command="{x:Static cmd:Commands.TestCommand}" 
        Executed="OnTestExecute" /> 
</UserControl.CommandBindings> 

然後在你的xaml.cs中:

public void OnTestRouteCanExecute(object sender, System.Windows.Input.CanExecuteRoutedEventArgs e) 
    { 
     e.CanExecute = true; 
    } 

    public void OnTestRouteExecute(object sender, System.Windows.Input.ExecutedRoutedEventArgs e) 
    { 
     // do some stuff here 
    } 
+0

我可以看看這是怎麼回事,但它不起作用。 MainRegion.xaml命令綁定不與RibbonRegion命令進行通信。 我的意思是MainRegion.xaml.cs'OnTestExecute'從來沒有從RibbonRegion的角度來評估,因此RibbonButton總是被禁用。 – TrialAndError 2012-08-27 14:54:58

+0

@TrialAndError:你有沒有找到更好的解決方案?如果是這樣,請回答你的問題讓我們知道:) – Jalal 2013-02-01 13:01:11