2013-07-05 50 views
1

現狀:的RoutedCommand在文本菜單的用戶控件

我有這樣定義的靜態RoutedCommand

public static class Commands 
{ 
    public static readonly RoutedCommand GrowingOperation = new RoutedCommand("GrowingOperation", typeof(GrowingDisplay)); 
} 

在我MyUserControl.xaml我這樣定義命令:

<UserControl.CommandBindings> 
    <CommandBinding Command="{x:Static local:Commands.GrowingOperation}" 
        Executed="GrowingOperationExecuted" 
        CanExecute="GrowingOperationCanExecute"/> 
</UserControl.CommandBindings> 

而且然後在我的MyUserControlContextMenu中像這樣使用它:

<UserControl.ContextMenu> 
    <ContextMenu x:Name="GrowingContextMenu"> 
     <MenuItem Header="Grow" 
         Command="{x:Static local:Commands.GrowingOperation}" 
         CommandParameter="grow"/> 
    </ContextMenu> 
</UserControl.ContextMenu> 

問題:

ContextMenu出現,但無論是GrowingOperationExecuted也不GrowingOperationCanExecute被調用。打開ContextMenu時,我也沒有得到任何異常。

開放ContextMenu看起來是這樣的: enter image description here

它似乎被啓用,但絕對沒有互動,甚至沒有一個懸停動畫。 這裏的錯誤在哪裏?

編輯:

這裏的指揮方法的實現:

private void GrowingOperationExecuted(object sender, ExecutedRoutedEventArgs e) 
    { 
     if (e.Parameter == null) 
      throw new ArgumentException("ExecutedRoutedEventArgs must contain parameter."); 
     var task = e.Parameter.ToString().ToLower(); 
     switch (task) 
     { 
      case "grow": 
       Growing.SpeedUpGrowing(); 
       break; 
      default: 
       throw new ArgumentOutOfRangeException(); 
     } 
    } 

    private void GrowingOperationCanExecute(object sender, CanExecuteRoutedEventArgs e) 
    { 
     if (e.Parameter == null) 
      throw new ArgumentException("ExecutedRoutedEventArgs must contain parameter."); 
     var task = e.Parameter.ToString().ToLower(); 
     switch (task) 
     { 
      case "grow": 
       e.CanExecute = Growing.CanSpeedUpGrowing(); 
       break; 
      default: 
       throw new ArgumentOutOfRangeException(); 
     } 
    } 

編輯2:

MyUserControl的構造:

public GrowingDisplay() 
    { 
     InitializeComponent(); 

     HeightProperty.AddOwner(typeof (GrowingDisplay), 
           new FrameworkPropertyMetadata(OnHeightPropertyChanged)); 
     WidthProperty.AddOwner(typeof (GrowingDisplay), 
           new FrameworkPropertyMetadata(OnWidthPropertyChanged)); 

     CommandManager.InvalidateRequerySuggested(); 
    } 

回答

0

我想你的RoutedCommand的定義修改爲:

private static RoutedUICommand _GrowingOperation; 
public static RoutedCommand GrowingOperation 
{ 
    get 
    { 
     if(_GrowingOperation == null) 
     { 
      _GrowingOperation = new RoutedUICommand("GrowingOperation", 
           "GrowingOperation", typeof(WINDOWNAME)); 
     } 
     return _GrowingOperation; 
} 

然後,您可以通過將命令類與清理XAML:

xmlns:commands="clr-namespace:NAMESPACE.Commands" 

在打開的窗口標籤將這個。 (假設這是一個窗口) 然後,當你設置你的命令,你可以使用:

<UserControl.CommandBindings> 
<CommandBinding Command="commands:Commands.GrowingOperation" 
       Executed="GrowingOperationExecuted" 
       CanExecute="GrowingOperationCanExecute"/> 

我唯一的問題是:你是如何實現GrowingOperationExecutedGrowingOperationCanExecute

+0

我在我的MyUserControl後面的代碼中實現了這兩個方法,然後在我的MainWindow中使用這個usercontrol。 嘗試你的解決方案... – Herdo

+0

如果它不起作用,你會介意發佈'GrowingOperationCanExecute'只是爲了確保它正確實施? – Th3BFG

+0

好的。改用你的UICommands並清理XAML代碼。 仍然無法正常工作。 就像之前說過的,方法中的斷點甚至沒有被執行,但我會在上面發佈這兩種方法。 – Herdo