Wpf 为什么不';我的命令绑定没有开火吗?
我在WPF应用程序中定义了许多自定义命令:Wpf 为什么不';我的命令绑定没有开火吗?,wpf,wpf-controls,Wpf,Wpf Controls,我在WPF应用程序中定义了许多自定义命令: public class MyCommands { public static RoutedUICommand CopyPlateCommand; public static RoutedUICommand PreviousRecordCommand; public static RoutedUICommand NextRecordCommand; public static RoutedUICommand Se
public class MyCommands {
public static RoutedUICommand CopyPlateCommand;
public static RoutedUICommand PreviousRecordCommand;
public static RoutedUICommand NextRecordCommand;
public static RoutedUICommand SearchCommand;
public static RoutedUICommand SearchPlateCommand;
}
我有一个UserControl
,它有contextmenu
,使用以下一些命令:
<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
不,我的程序不使用MVVM。我在听说MVVM之前就开始了这个项目。我打算在将来的某个时候将其转换为MVVM,但我现在没有时间。我需要修理一下门上的虫子,这让我受不了了
谢谢你的理解
Tony您的命令类需要公开公共属性,而不是公共字段 您必须绑定到属性而不是字段,因为大多数绑定都基于 ComponentModel属性描述器模型。属性公开绑定引擎启用绑定所需的元数据 绑定到公共语言运行库(CLR)对象:
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; }
}
您可以绑定到公共属性、子属性以及
任何公共语言运行库(CLR)对象的索引器。装订
引擎使用CLR反射来获取属性的值。
或者,实现ICustomTypeDescriptor或具有
注册的TypeDescriptionProvider也可以使用绑定引擎
摘自《圣经》
下面的示例:
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; }
}
在调试模式下运行应用程序时,请检查“输出”窗口中的绑定错误。虽然我在类中创建的
RoutedUICommand
对象是字段而不是属性,但这不足以使程序正常工作。在XAML中,我根本不知道如何将UserControl1
中MenuItems
的CommandTarget
属性设置为UserControl2
我确实让它工作了,但我最终还是在代码隐藏中完成了。我所做的是编写了一个名为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;
}
}
}
然后,在UserControl1
和UserControl2
的构造函数中,我根据需要调用公共函数,因为我将一些控件的处理程序移动到UserControl1
中,因为这些处理程序需要访问该类的私有属性
我还必须删除菜单项
的命令
属性上的绑定。我能够在XAML中执行命令绑定
。那只是因为我不能去上班
在做了所有这些更改之后,现在一切都正常了。MenuItems
所有控件都指向右侧控件,它们将正确启用和禁用。他们各司其职。我很高兴。谢谢。我在查看输出窗口时确实看到绑定错误。我只是不知道问题出在哪里。如果你觉得这对你的问题有帮助,别忘了把它标记为公认的答案。谢谢虽然您确实指出了我的代码中的一个问题,但并不是这个问题让一切都无法正常工作。请看我的答案,看看什么是真正有效的。我对你的答案投了赞成票,一模一样。它回答了“为什么我的命令绑定没有启动”的问题。你发布的答案与命令没有执行的原因无关。即使有你指出的更改,当命令绑定启动时应该执行的逻辑也没有运行。直到我在答案中做了更改,程序才执行该逻辑。因此,如果我的答案与为什么要执行绑定无关,那么为什么只有在我进行这些更改时才会执行绑定呢?
public void SetupCommandTargets( FrameworkElement target, Func<MenuItem, bool> condition ) {
FixMenuItems( Control1, condition, target );
FixMenuItems( Control2, condition, target ); . . .
}