我在我的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
謝謝。我正在查看輸出窗口,並且看到綁定錯誤。我無法弄清楚問題所在。 –
如果您認爲這有助於您解決問題,請不要忘記將其標記爲已接受的答案。謝謝! –
雖然您確實指出了我的代碼存在問題,但這並不是解決所有問題的辦法。請看我的答案,看看實際工作。我已經提出了你的答案,只是一樣。 –