2012-10-03 32 views
2

我在我的WPF應用程序定義了一些自定義的命令:爲什麼我的命令綁定不能解僱?

public class MyCommands { 

    public static RoutedUICommand CopyPlateCommand; 

    public static RoutedUICommand PreviousRecordCommand; 

    public static RoutedUICommand NextRecordCommand; 

    public static RoutedUICommand SearchCommand; 

    public static RoutedUICommand SearchPlateCommand; 

} 

我有一個UserControl具有ContextMenus使用某些命令:

<UserControl x:Class="MyNamespace.UserControl1" 
     . . . > 
    <UserControl.Resources> 
     <ContextMenu x:Key="ContextMenu" HorizontalAlignment="Left"> 
      <MenuItem Header="Copy Plate" Command="{Binding cs:MyCommands.CopyPlateCommand}" /> 
      <MenuItem Header="Search Plate" Command="{Binding cs:MyCommands.SearchPlateCommand}" /> 
     </ContextMenu> 
     <ContextMenu x:Key="TextBoxMenu" HorizontalAlignment="Left"> 
      <MenuItem Header="Copy"   Command="{Binding Copy}" /> 
      <MenuItem Header="Copy Plate" Command="{Binding cs:MyCommands.CopyPlateCommand}" /> 
      <MenuItem Header="Search Plate" Command="{Binding cs:MyCommands.SearchPlateCommand}" /> 
     </ContextMenu> 
    </UserControl.Resources> 
          . . . 
</UserControl> 

有在UserControl1沒有命令綁定。

我有另一個UserControl其中包括一個UserControl1的實例。它還具有CommandBindings,其中包括在UserControl1包括在上下文菜單中的命令:

<UserControl x:Class="MyNamespace.UserControl2" 
     . . . > 
    <UserControl.CommandBindings> 
     <CommandBinding Command="Copy"         CanExecute="CopyCommand_CanExecute"  Executed="CopyCommand_Executed" /> 
     <CommandBinding Command="cs:MyCommands.CopyPlateCommand" CanExecute="CopyPlateCommand_CanExecute" Executed="CopyPlateCommand_Executed" /> 
     <CommandBinding Command="cs:MyCommands.SearchPlateCommand" CanExecute="CopyPlateCommand_CanExecute" Executed="SearchPlateCommand_Executed" /> 
</UserControl.CommandBindings> 
          . . . 
    <c:UserControl1 . . . /> 

我已經放在斷點在不同的處理程序的命令,但這些斷點從不打。我做錯了什麼?我必須將命令綁定到UserControl1嗎?

不,我的程序不使用MVVM。在我聽說過MVVM之前,我開始了這個項目。我打算在未來某個時候將其轉換爲MVVM,但現在我沒有時間。我需要修正一些錯誤,並阻止我。

感謝您的理解。

Tony

回答

0

雖然我在課堂上創建的RoutedUICommand對象是字段而不是專有名稱,但這並不足以讓程序正常工作。我簡直無法弄清楚在XAML中如何將MenuItemsCommandTarget屬性設置爲UserControl1UserControl2

我確實得到它的工作,但我最終在代碼隱藏中做到了。我所做的是我寫的一個輔助方法稱爲FixMenuItems

private void FixMenuItems(FrameworkElement element, Func<MenuItem, bool> condition, FrameworkElement target) { 
    foreach (MenuItem menuItem in element.ContextMenu.Items) { 
     if (condition(menuItem)) { 
      menuItem.CommandTarget = target; 
     } 
    } 
} 

然後我寫了一個公共的方法稱爲SetupCommandTargets

public void SetupCommandTargets(FrameworkElement target, Func<MenuItem, bool> condition) { 
    FixMenuItems(Control1, condition, target); 
    FixMenuItems(Control2, condition, target);  . . . 
} 

然後,在UserControl1UserControl2的建設者,我稱之爲公共職能根據需要,我將一些控件的處理程序移動到UserControl1,因爲這些處理程序需要訪問該類的私有屬性。

我也必須刪除MenuItemsCommand屬性Bindings。我能夠在XAML中執行CommandBindings。這只是CommandTargets我無法工作。

完成所有這些更改後,現在一切正常。 MenuItems都指向正確的控件,並且它們被正確啓用和禁用。他們各自執行適當的行動。我很高興。

3

您的Command類需要公開屬性而不是公共字段。

你必須綁定到一個屬性,而不是一個領域,因爲綁定是基於 ComponentModel PropertyDescriptor的模型。屬性公開了綁定引擎啓用綁定所需的元數據。

綁定到公共語言運行庫(CLR)對象:

您可以綁定到公共屬性,子屬性,以及 索引,任何公共語言運行庫(CLR)對象。綁定 引擎使用CLR反射來獲取屬性的值。 或者,實現ICustomTypeDescriptor或具有註冊的TypeDescriptionProvider的對象也可以與綁定引擎一起使用。

取自binding source specification。下面

實施例:

public class MyCommands { 

    public static RoutedUICommand CopyPlateCommand { get; set; } 

    public static RoutedUICommand PreviousRecordCommand { get; set; } 

    public static RoutedUICommand NextRecordCommand { get; set; } 

    public static RoutedUICommand SearchCommand { get; set; } 

    public static RoutedUICommand SearchPlateCommand { get; set; } 

} 

當運行在調試模式下的應用程序,檢查「輸出」窗口綁定錯誤。

+0

謝謝。我正在查看輸出窗口,並且看到綁定錯誤。我無法弄清楚問題所在。 –

+0

如果您認爲這有助於您解決問題,請不要忘記將其標記爲已接受的答案。謝謝! –

+0

雖然您確實指出了我的代碼存在問題,但這並不是解決所有問題的辦法。請看我的答案,看看實際工作。我已經提出了你的答案,只是一樣。 –

相關問題