使用 DelegateCommand 的 CanExecute 操作 [英] Working with DelegateCommand's CanExecute action

查看:66
本文介绍了使用 DelegateCommand 的 CanExecute 操作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 Prism/WPF 项目中有一个这样的 ViewModel 类.

I've a ViewModel class like this in a Prism / WPF project.

public class ContentViewModel : ViewModelBase, IContentViewModel
{
    public ContentViewModel(IPersonService personService)
    {
        Person = personService.GetPerson();
        SaveCommand = new DelegateCommand(Save, CanSave);
    }

    public Person Person { get; set; }

    public DelegateCommand SaveCommand { get; set; }

    private void Save()
    {
        // Save actions here...
    }

    private bool CanSave()
    {
        return Person.Error == null;
    }
}

上面ViewModel中使用的person类型定义如下:

The person type used in the above ViewModel is defined as follows:

public class Person : INotifyPropertyChanged, IDataErrorInfo
{
    private string _firstName;
    public string FirstName
    {
        get { return _firstName; }
        set
        {
            _firstName = value;
            OnPropertyChanged("FirstName");
        }
    }

    // other properties are implemented in the same way as above...

    public event PropertyChangedEventHandler PropertyChanged;

    protected void OnPropertyChanged(string propertyName)
    {
        if (PropertyChanged != null)
            PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
    }

    private string _error;
    public string Error
    {
        get
        {
            return _error;
        }
    }

    public string this[string columnName]
    {
        get
        {
            _error = null;
            switch (columnName)
            {
                // logic here to validate columns...
            }
            return _error;
        }
    }
}

ContentViewModel 的一个实例被设置为一个 View 的 DataContext.在视图中,我使用绑定到 Person 如下:

An instance of ContentViewModel is set as DataContext of a View. Inside the View I've used binding to Person as follows:

<TextBox Text="{Binding Person.FirstName, ValidatesOnDataErrors=True}" />
<Button Content="Save" Command="{Binding SaveCommand}" />

当我对绑定到人的属性(如名字)的 TextBox 进行更改并单击保存"时,我可以看到 ViewModel 命令处理程序中的更改.但是,如果这些属性中的任何一个在验证中失败,则 CanSave 永远不会执行并且按钮永远不会被禁用.

When I make changes to TextBox which is binded to Person's properties like FirstName and click Save I could see the changes in ViewModel command handler. But if any of these properties fail in validation CanSave is never executed and button never gets disabled.

如何在上述场景中禁用基于 DelegateCommand 的 CanExecute 操作处理程序的按钮?

How do I disable a button based on DelegateCommand's CanExecute action handler in the above scenario?

推荐答案

尝试使用所有可以更改错误的属性:

try this with all the properties that can change error:

 public string FirstName
{
    get { return _firstName; }
    set
    {
        _firstName = value;
        OnPropertyChanged("FirstName");

        OnPropertyChanged("Error");
    }
}

替代

        switch (columnName)
        {
            // logic here to validate columns...

            OnPropertyChanged("Error");
        }

您遇到的问题是当错误更改时没有调用 OnPropertyChanged.

The problem you are having is that the OnPropertyChanged is not being called when the error changes.

下一步是在创建人的 propertychanged 事件时订阅它,并创建一个处理程序来检查 propertychanged,然后更改命令使用的布尔变量.

The next step is to subscribe to the person's propertychanged event when its created, and create a handler that checks for the propertychanged and then changes the boolean variable that the command uses.

 public ContentViewModel(IPersonService personService)
{
    Person = personService.GetPerson();
    Person.PropertyChanged+= PersonPropertyChangedHandler;
    SaveCommand = new DelegateCommand(Save, personHasError);
}

bool personHasError = false;
void PersonPropertyChangedHandler(object sender, System.ComponentModel.PropertyChangedEventArgs e)
{
    if (e.PropertyName == "Error")
    {
        if(Person.Error == null)
           personHasError = true;
        else
           personHasError = false;
    } 
}

希望这有效.我是手工制作的,没有检查它,所以让我知道它是否有问题或其他什么问题,并纠正它

Hope this works. I built this by hand and didn't check it so let me know if its buggy or whatever and ill correct it

这篇关于使用 DelegateCommand 的 CanExecute 操作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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