如何制作" CanSave"属性启用/禁用绑定的SaveButton到RelayCommand?

时间:2014-05-18 09:26:54

标签: c# wpf validation mvvm

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

和PersonViweModel.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类如下:

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

0 个答案:

没有答案