p>I've added the commands as follows ... public ICommand Create { get { return buttonCreate.Command; } set { buttonCreate.Command = value; } }我已将它们设置为依赖项属性,因此我可以绑定到它们...I've set these as dependency properties so I can bind to them ... public static readonly DependencyProperty CreateCommandProperty = DependencyProperty.Register( "Create", typeof(ICommand), typeof(StandardButtonStrip), new PropertyMetadata((ICommand)null));然后将用户控件绑定到命令...I'm then binding my usercontrol to a command ...<commoncontrols:StandardButtonStrip HorizontalAlignment="Stretch" Create="{Binding CreateCommand}" />我正在如下设置命令...I'm setting up the command as follows ..._viewModel.CreateCommand = new DelegateCommand<object>(OnCreateCommand, CanCreate);但是尽管事实上我一直在我的CanCreate方法上返回true,但是按钮还是被禁用了。如果我在返回true处设置了一个断点,它将永远不会触发!but despite the fact that i'm always returning true on my CanCreate method the button is disabled ... if I put a break point on return true it never fires! public bool CanCreate(object parm) { return true; }我已经尝试过看看是否会刷新绑定,但是没有I've tried this to see if it will refresh the binding, but no joy!_viewModel.CreateCommand.RaiseCanExecuteChanged();我认为问题出在用户控件上,我如何将命令作为属性,但不确定...I think the problem is down to the user control and how I'm passing the Command up as a property, but not sure ...推荐答案这种外观看起来,您在视图模型上具有依赖项属性。如果您确实在使用MVVM,则绝对不是解决该问题的方法(不是因为宗教信仰某种模式,而是因为它不是最佳方法)。The way this looks, you have a dependency property on a view model. If you are really using MVVM, that is definetly not the way to go about it (not because of religious adherence to a pattern, but because it's not the optimal way).首先,您的视图模型是DependencyObject吗?First of all, is your view model a DependencyObject?如果是,则应将其降级为实现INotifyPropertyChanged的类。为什么?因为Button的Command属性本身就是DependencyProperty(从ButtonBase继承),并且已经支持数据绑定。If it is, you should downgrade it to a class which implements INotifyPropertyChanged. Why? Because the Button's Command property is a DependencyProperty itself (inherited from ButtonBase) and supports databinding already.如果不是,则其依赖属性将不起作用,很好,因为您首先不应该在视图模型上具有依赖项属性。If it isn't, then a dependency property on it will not work, which is fine, because you shouldn't have dependency properties on your view model in the first place.您应该做的是,将视图模型作为DataContext用于您的控件(我想您已经设置好了)。然后将视图模型的CreateCommand更改为普通的ICommand,并像这样(使用默认的StandardButtonStrip样式)绑定createButton的Command属性What you should do, is have the view model as the DataContext for your control (I'm guessing you already have that set up). Then change your view model's CreateCommand to a plain ICommand, and bind the createButton's Command property like this (in your default StandardButtonStrip style)<Button Name="createButton" HorizontalAlignment="Stretch" Command="{Binding CreateCommand}" />这样,它仍然可以重用,您只需要确保与之关联的任何视图模型您的用户控件具有ICommand类型的CreateCommand属性(默认情况下,该视图模型将继承到按钮控件,这是wpf成员想到的最好的事情之一)。This way, it will still be reusable, you just need to ensure that any view model you associate with your user control has a property CreateCommand of type ICommand (and that view model will be inherited down to the button control by default - one of the nicest things the wpf guys thought up).因此,回顾一下,您应该或多或少进行相反的操作。So to recap, you should do it the other way around, more or less.希望这会有所帮助,欢呼。Hope this was helpful, cheers. 这篇关于WPF-从用户控件引发命令时,CanExecute不会触发的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云! 08-12 07:03