【发布时间】:2021-09-22 11:13:00
【问题描述】:
给出下一个例子:
型号
public class ProjecteModel : ObservableObject, IProjecteModel
{
private string _patientID
public string IdPacient
{
get => _idPacient;
set
{
_idPacient = value;
OnPropertyChanged(nameof(IdPacient));
}
}
/* More fields here... */
}
其中 ObservableObject 是实现 INotifyPropertyChanged 接口的基类。
视图模型
public class ProjecteViewModel : BaseViewModel, IDataErrorInfo
{
private IProjecteModel _projecte = new ProjecteModel();
private string _codiClient;
public IProjecteModel Projecte
{
get => _projecte;
set
{
_projecte = value;
OnPropertyChanged(nameof(Projecte));
}
}
public string CodiClient
{
get => _codiClient;
set
{
_codiClient = value;
OnPropertyChanged(nameof(CodiClient));
}
}
public string this[string columnName]
{
get
{
string msg = String.Empty;
switch (columnName)
{
case "CodiClient":
if (CodiClient.Length <= 0)
{
msg = "ID Client is required.";
}
break;
case "IdPacient":
//case "Projecte.IdPacient":
if (Projecte.IdPacient.Length <= 0)
{
msg = "Id Pacient is required.";
}
break;
};
return msg;
}
}
}
查看
<!-- Id Pacient -->
<StackPanel Orientation="Vertical"
Width="{Binding ElementName=capComandes, Path=ItemsWidth}">
<TextBlock Text="ID Pacient:"
Style="{StaticResource FieldNameTextBlock}"/>
<TextBox x:Name="IdPacient"
Width="150"
HorizontalAlignment="Left"
Margin="0, 0, 0, 15"
Foreground="{StaticResource ForegroundFieldValuesBrush}"
Text="{Binding Projecte.IdPacient,
Mode=TwoWay,
UpdateSourceTrigger=PropertyChanged,
ValidatesOnDataErrors=True}"/>
</StackPanel>
<!-- CodiClient -->
<StackPanel Orientation="Vertical"
Width="{Binding ElementName=capComandes, Path=ItemsWidth}">
<TextBlock Text="ID Client:"
Style="{StaticResource FieldNameTextBlock}"/>
<TextBox x:Name="IdClient"
Width="150"
HorizontalAlignment="Left"
Foreground="{StaticResource ForegroundFieldValuesBrush}"
Text="{Binding CodiClient,
Mode=TwoWay,
UpdateSourceTrigger=PropertyChanged,
ValidatesOnDataErrors=True}">
</TextBox>
</StackPanel>
验证适用于 CodiClient,但不适用于 IdPacient。
验证方法:从不为 IdPacient 调用 public string this[string columnName]。
如何验证子类属性?
【问题讨论】:
-
请注意,自 .NET 4.5 起实现属性验证的推荐方法是实现
INotifyDataErrorInfo接口。 -
@BionicCode 是的,我已经阅读过,感谢您的建议..
标签: wpf validation