如何使“CanSave”属性启用/禁用绑定的SaveButton到RelayCommand? [英] How to make “CanSave” property to Enable/disable binded SaveButton to RelayCommand?

查看:111
本文介绍了如何使“CanSave”属性启用/禁用绑定的SaveButton到RelayCommand?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用MVVM模式处理WPF应用程序。问题是:如何检查文本框内容是否有效,并顺便使保存按钮禁用,直到所有文本框都填入有效数据。我搜索越来越多,但在所有情况下,答案是在模型上实现IDataerrorInfo但我使用实体框架实体作为模型,因此无法执行。请给我一些建议或链接。提前致谢。在我的项目描述下面:XAML:

I working on WPF application using MVVM pattern. the question is : How to Check wether the Textboxes content are valid or not and by the way make a Save Button Disable until all textboxes fill by valid data. I googled more and more but in all scenario the answer was implementing IDataerrorInfo on the model but i using entity frameworks entity as model so can not do the implementation. please give me some advice or links . thanks in advance. Below my project description: the XAML:

<TextBox Name="txtName"    Height="23" HorizontalAlignment="Left" Margin="56,42,0,0"  VerticalAlignment="Top" Width="120"  >
                           <TextBox.Style>
                               <Style TargetType="TextBox">
                                   <Style.Triggers>
                                       <Trigger Property="Validation.HasError" Value="true">
                                           <Setter Property="ToolTip" Value="{Binding RelativeSource={x:Static RelativeSource.Self}, Path=(Validation.Errors).CurrentItem.ErrorContent}" />
                                       </Trigger>
                                   </Style.Triggers>
                               </Style>
                           </TextBox.Style>

                           <Binding   Path="CurrentItem.Name" UpdateSourceTrigger="PropertyChanged">
                               <Binding.ValidationRules>
                                   <vm:NotNullValidationRule  ValidatesOnTargetUpdated="True"/>
                               </Binding.ValidationRules>
                           </Binding>
                       </TextBox>





和PersonViewModel.cs:



And the PersonViewModel.cs:

public class PersonViewModel : WorkspaceViewModel
{

    SDPS.Business.Base.BaseBusiness<Person> bz = new Business.Base.BaseBusiness<Person>();

    #region Constructor

    public PersonViewModel(string displayTitle)
    {
        Items = bz.GetAll();
        DisplayName = displayTitle;
        NewCommand = new RelayCommand(p => NewItem());
        SaveCommand = new RelayCommand(p => SaveItem());
        UpdateCommand = new RelayCommand(p => UpdateItem(), p => CanUpdateOrDelete);
        DeleteCommand = new RelayCommand(p => DeleteItem(), p => CanUpdateOrDelete);

    }



    #endregion

    #region Methods
    private void NewItem()
    {
        CurrentItem = new Person();
    }

    private void SaveItem()
    {

            bz.Add(CurrentItem);
            Items.Add(CurrentItem);

    }
    private void DeleteItem()
    {
        bz.Delete(CurrentItem);
        Items.Remove(CurrentItem);

        OnPropertyChanged("Items");

        OnPropertyChanged("CurrentItem");

    }
    public void UpdateItem()
    {

        bz.Update(CurrentItem);
        OnPropertyChanged("Items");
    }
    #endregion

    #region Properties

    public Person CurrentItem
    {
        get { return _CurrentItem; }
        set
        {
            _CurrentItem = value;

            OnPropertyChanged("CurrentItem");
        }
    }

    public ObservableCollection<Person> Items
    {
        get { return _items; }
        set
        {
            _items = value;
            OnPropertyChanged("Items");
        }
    }
    public bool CanUpdateOrDelete { get { return CurrentItem != null; } }

    #endregion

    #region Variables
    private ObservableCollection<Person> _items;
    private Person _CurrentItem;

    #endregion


}



和ValidationRule类如下:


and the ValidationRule class is as below:

 public class NotNullValidationRule : ValidationRule
{

    public virtual bool HasError
    {
        get
        {
            return   null;
        }
    }
    public override ValidationResult Validate(object value, CultureInfo cultureInfo)
    {
        var result = new ValidationResult(true, null);

        if(value!=null)
        if (string.IsNullOrWhiteSpace( value.ToString()) )
        {
            result = new ValidationResult(false, "null string not allowed");
        }

        return result;
    }
}

推荐答案

这篇关于如何使“CanSave”属性启用/禁用绑定的SaveButton到RelayCommand?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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