使用 IDataErrorInfo 和更新按钮状态在验证期间启用禁用保存按钮
Enable Disable save button during Validation using IDataErrorInfo & Update Button State
我是 WPF MVVM 的新手,想问一个关于本文的后续问题:
Enable Disable save button during Validation using IDataErrorInfo
我正在尝试 enable/disable 按钮 save/update 如果表单验证中的许多控件中的任何一个 failed/passed。
我有 IsValid 方法,它检查模型和 returns True/False 上的验证逻辑,它将作为谓词传递给 DelegateCommand。
问题是:我的按钮有以下 属性 IsEnabled{binding IsValid},这应该检查所有字段以确保它符合模型中的条件,returns true/false 到视图模型,然后在全部为真时启用按钮。问题是:实例化视图模型后,DelegateCommand 对象将在 false 状态下通过验证 (IsValid) 创建,并且在对象的整个生命周期中保持这种状态,即使用户正在文本框中填写数据。满足所有条件后如何打开按钮?换句话说,如何不断验证和更新 IsValid,以便在每个文本框验证为真时打开按钮?
谢谢,
我有以下代码:
模特
public class UserModel : ObservePropertyChanged,IDataErrorInfo
{
private string name;
public string Name
{
get { return name; }
set { name = value; OnPropertyChanged("Name"); }
}
// if there is an error throw an exception
public string Error
{
get { throw new NotImplementedException(); }
}
public string this[string columnName]
{
get
{
string result = null;
if (columnName == "Name")
{
if (string.IsNullOrEmpty(Name))
result = "Please enter a Name";
}
return result;
}
}
// the Josh Way
static readonly string[] ValidatedProperties =
{
"Name"
};
public bool IsValid
{
get
{
foreach (string property in ValidatedProperties)
{
if (GetValidationError(property) != null) // there is an error
return false;
}
return true;
}
}
// a method that checks validation error
private string GetValidationError(string propertyName)
{
string error = null;
switch (propertyName)
{
case "Name":
error = this.ValidateName();
break;
default:
error = null;
throw new Exception("Unexpected property being validated on Service");
}
return error;
}
private string ValidateName()
{
string ErrorMsg = null;
if (string.IsNullOrWhiteSpace(Name))
{
ErrorMsg = "Name can't be empty!";
};
return ErrorMsg;
}
}
** 视图模型 **
public class UserViewModel:ObservePropertyChanged
{
UserModel model;
public UserViewModel()
{
presentCommand = new DelegateCommand(param => PresentDataMethod(), param => CanSave);
model = new UserModel();
}
private string name;
public string Name
{
get { return name; }
set { name = value; OnPropertyChanged("Name"); }
}
private string info;
public string Info
{
get { return info; }
set { info = value; OnPropertyChanged("Info"); }
}
private DelegateCommand presentCommand;
public DelegateCommand PresentCommand
{
get
{
if (presentCommand==null)
{
presentCommand = new DelegateCommand(param => PresentDataMethod(), param => CanSave);
}
return presentCommand;
}
}
private void PresentDataMethod()
{
Info = $"Your Name is: {Name}.";
}
// The ViewModel then contains a CanSave Property that reads the IsValid property on the Model:
protected bool CanSave
{
get
{
return model.IsValid;
}
}
}
** 景色**
<TextBox x:Name="Name" HorizontalAlignment="Left" Height="34" Margin="285,145,0,0" TextWrapping="Wrap"
VerticalAlignment="Top" Width="248">
<Binding Path="Name"
ValidatesOnDataErrors="True"
UpdateSourceTrigger="PropertyChanged"
Mode="TwoWay">
</Binding>
</TextBox>
<Button Content="Present" FontSize="20" HorizontalAlignment="Left"
Margin="285,184,0,0" VerticalAlignment="Top" Width="248" Height="35"
Command="{Binding Path=PresentCommand}"
IsEnabled="{Binding IsValid}"
>
</Button>
如果您只想通过 IsValid
值刷新按钮,您只需监听 ViewModel 中的任何其他 属性 更改,以及何时发生,告诉它也刷新 IsValid
绑定(这实际上是您的 CanSave
属性)。这是一种方法:
** 视图模型 **
// ...
public UserViewModel()
{
// ...
this.PropertyChanged += OnViewModelPropertyChanged;
}
public void OnViewModelPropertyChanged(object sender, PropertyEventArgs e)
{
// On any property that implements "OnPropertyChanged(propname)", refresh the CanSave binding too!
OnPropertyChanged(nameof(this.CanSave));
}
// ...
顺便说一句,避免像 OnPropertyChanged("Name")
或 OnPropertyChanged("Info")
这样的魔法词通常是很好的编码习惯,因为你永远不知道开发人员什么时候必须重命名他们的属性,如果发生这种情况,你就赢了' 在这里出现编译错误,可能很难调试。最佳做法是使用 nameof
,例如 OnPropertyChanged(nameof(Name))
,这样如果您决定将 属性 更改为,例如 FirstName
而不是 Name
.
我是 WPF MVVM 的新手,想问一个关于本文的后续问题: Enable Disable save button during Validation using IDataErrorInfo 我正在尝试 enable/disable 按钮 save/update 如果表单验证中的许多控件中的任何一个 failed/passed。 我有 IsValid 方法,它检查模型和 returns True/False 上的验证逻辑,它将作为谓词传递给 DelegateCommand。 问题是:我的按钮有以下 属性 IsEnabled{binding IsValid},这应该检查所有字段以确保它符合模型中的条件,returns true/false 到视图模型,然后在全部为真时启用按钮。问题是:实例化视图模型后,DelegateCommand 对象将在 false 状态下通过验证 (IsValid) 创建,并且在对象的整个生命周期中保持这种状态,即使用户正在文本框中填写数据。满足所有条件后如何打开按钮?换句话说,如何不断验证和更新 IsValid,以便在每个文本框验证为真时打开按钮?
谢谢,
我有以下代码: 模特
public class UserModel : ObservePropertyChanged,IDataErrorInfo
{
private string name;
public string Name
{
get { return name; }
set { name = value; OnPropertyChanged("Name"); }
}
// if there is an error throw an exception
public string Error
{
get { throw new NotImplementedException(); }
}
public string this[string columnName]
{
get
{
string result = null;
if (columnName == "Name")
{
if (string.IsNullOrEmpty(Name))
result = "Please enter a Name";
}
return result;
}
}
// the Josh Way
static readonly string[] ValidatedProperties =
{
"Name"
};
public bool IsValid
{
get
{
foreach (string property in ValidatedProperties)
{
if (GetValidationError(property) != null) // there is an error
return false;
}
return true;
}
}
// a method that checks validation error
private string GetValidationError(string propertyName)
{
string error = null;
switch (propertyName)
{
case "Name":
error = this.ValidateName();
break;
default:
error = null;
throw new Exception("Unexpected property being validated on Service");
}
return error;
}
private string ValidateName()
{
string ErrorMsg = null;
if (string.IsNullOrWhiteSpace(Name))
{
ErrorMsg = "Name can't be empty!";
};
return ErrorMsg;
}
}
** 视图模型 **
public class UserViewModel:ObservePropertyChanged
{
UserModel model;
public UserViewModel()
{
presentCommand = new DelegateCommand(param => PresentDataMethod(), param => CanSave);
model = new UserModel();
}
private string name;
public string Name
{
get { return name; }
set { name = value; OnPropertyChanged("Name"); }
}
private string info;
public string Info
{
get { return info; }
set { info = value; OnPropertyChanged("Info"); }
}
private DelegateCommand presentCommand;
public DelegateCommand PresentCommand
{
get
{
if (presentCommand==null)
{
presentCommand = new DelegateCommand(param => PresentDataMethod(), param => CanSave);
}
return presentCommand;
}
}
private void PresentDataMethod()
{
Info = $"Your Name is: {Name}.";
}
// The ViewModel then contains a CanSave Property that reads the IsValid property on the Model:
protected bool CanSave
{
get
{
return model.IsValid;
}
}
}
** 景色**
<TextBox x:Name="Name" HorizontalAlignment="Left" Height="34" Margin="285,145,0,0" TextWrapping="Wrap"
VerticalAlignment="Top" Width="248">
<Binding Path="Name"
ValidatesOnDataErrors="True"
UpdateSourceTrigger="PropertyChanged"
Mode="TwoWay">
</Binding>
</TextBox>
<Button Content="Present" FontSize="20" HorizontalAlignment="Left"
Margin="285,184,0,0" VerticalAlignment="Top" Width="248" Height="35"
Command="{Binding Path=PresentCommand}"
IsEnabled="{Binding IsValid}"
>
</Button>
如果您只想通过 IsValid
值刷新按钮,您只需监听 ViewModel 中的任何其他 属性 更改,以及何时发生,告诉它也刷新 IsValid
绑定(这实际上是您的 CanSave
属性)。这是一种方法:
** 视图模型 **
// ...
public UserViewModel()
{
// ...
this.PropertyChanged += OnViewModelPropertyChanged;
}
public void OnViewModelPropertyChanged(object sender, PropertyEventArgs e)
{
// On any property that implements "OnPropertyChanged(propname)", refresh the CanSave binding too!
OnPropertyChanged(nameof(this.CanSave));
}
// ...
顺便说一句,避免像 OnPropertyChanged("Name")
或 OnPropertyChanged("Info")
这样的魔法词通常是很好的编码习惯,因为你永远不知道开发人员什么时候必须重命名他们的属性,如果发生这种情况,你就赢了' 在这里出现编译错误,可能很难调试。最佳做法是使用 nameof
,例如 OnPropertyChanged(nameof(Name))
,这样如果您决定将 属性 更改为,例如 FirstName
而不是 Name
.