WPF - CanExecute从UserControl提升命令时不会触发 [英] WPF - CanExecute dosn't fire when raising Commands from a UserControl

查看:184
本文介绍了WPF - CanExecute从UserControl提升命令时不会触发的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我已经添加了如下的命令...

p>

  public ICommand创建
{
get
{
return buttonCreate.Command;
}
set
{
buttonCreate.Command = value;
}
}



我已将这些设置为依赖属性,绑定到它们...

  public static readonly DependencyProperty CreateCommandProperty = 
DependencyProperty.Register(
Create ,
typeof(ICommand),
typeof(StandardButtonStrip),
new PropertyMetadata((ICommand)null)

然后我将用户控制绑定到命令...

 < commoncontrols:StandardButtonStrip Horizo​​ntalAlignment =StretchCreate ={Binding CreateCommand}/> 

我设置命令如下...

  _viewModel.CreateCommand = new DelegateCommand< object>(OnCreateCommand,CanCreate); 

但是尽管我总是在我的CanCreate方法返回true,

  public bool CanCreate(object parm)
{
return true;
}



我试过这个,看看是否会刷新绑定,欢乐!

  _viewModel.CreateCommand.RaiseCanExecuteChanged(); 

我认为问题在于用户控制,但不确定...



任何人都可以帮忙吗?




$ b

Andy

解决方案

看起来,你有一个视图模型的依赖属性。如果你真的使用MVVM,这绝对不是去做的方式(不是因为宗教坚持一个模式,而是因为它不是最佳的方式)。



首先,你的视图模型是一个DependencyObject吗?



如果是,您应该将其降级为实现INotifyPropertyChanged的类。为什么?因为Button的Command属性是一个DependencyProperty本身(继承自ButtonBase),并且已经支持数据绑定。



如果不是,那么它的依赖属性将不起作用,这是很好的,因为你不应该有视图模型上的依赖属性在第一位。



你应该做什么,是视图模型作为DataContext你的控制(我猜你已经有那个设置)。然后将您的视图模型的CreateCommand更改为一个平常的ICommand,并绑定createButton的Command属性(在默认的StandardButtonStrip样式)

  < Button Name =createButtonHorizo​​ntalAlignment =StretchCommand ={Binding CreateCommand}/> 

这样,它仍然可以重用,你只需要确保任何视图模型你的用户控件有一个属性CreateCommand类型ICommand(和视图模型将继承下来的按钮控件默认 - wpf家伙最好的事情之一)。



希望这是有用的,干杯。


I've got a button strip usercontrol that I want to have on most of my forms.

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);

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 ...

Can anyone help?

Cheers,

Andy

解决方案

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).

First of all, is your view model a DependencyObject?

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.

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}" />

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从UserControl提升命令时不会触发的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆