C# 4.0 MVVM模型中的验证
海大家,,C# 4.0 MVVM模型中的验证,c#-4.0,C# 4.0,海大家,, 我正在使用MVVM模型做一个项目,有人能帮我说一下如何验证此模型中的字段吗?验证意味着,如果用户没有添加名称(示例)的文本字段,则会显示一条消息,使用弹出窗口或任何东西。Nollamly我们是通过编写一个函数来实现的,这个函数将在保存或其他操作之前被调用,当时如果任何Mandetory字段是自由的,那么将显示一个错误,只有在填写完该字段后,我们才能继续。在MVVM模型中,我如何才能做到这一点?好的,我不能100%确定您将如何使用该字段,但我将给出一个示例。不确定这将如何与MVVM一起
我正在使用MVVM模型做一个项目,有人能帮我说一下如何验证此模型中的字段吗?验证意味着,如果用户没有添加名称(示例)的文本字段,则会显示一条消息,使用弹出窗口或任何东西。Nollamly我们是通过编写一个函数来实现的,这个函数将在保存或其他操作之前被调用,当时如果任何Mandetory字段是自由的,那么将显示一个错误,只有在填写完该字段后,我们才能继续。在MVVM模型中,我如何才能做到这一点?好的,我不能100%确定您将如何使用该字段,但我将给出一个示例。不确定这将如何与MVVM一起工作,但我想它应该 在您的模型中添加以下内容:
public bool IsValid
{
get
{
return (GetRuleViolations().Count() == 0);
}
}
public override IEnumerable<RuleViolation> GetRuleViolations()
{
if (string.IsNullOrEmpty(Name))
yield return new RuleViolation("Name can't be empty", "Name");
else if (Name.Length >= 30)
yield return new RuleViolation("Name can't be more then 30 letters", "Name");
if (!string.IsNullOrEmpty(Address))
{
if (!Web.Contains("http"))
yield return new RuleViolation("Address must be bla bal", "Address");
}
// and so on
yield break;
}
现在,当您调用IsValid方法时,它将检查每个属性。如果您使用的是INotifyPropertyChanged,则每次更新任何已验证的属性时,我都会将IsValid添加为要更新的属性。如果您有一个基本模型类,您可以将其添加到其中,并在每个派生类中定义规则:
public bool IsValid
{
get
{
return (GetRuleViolations().Count() == 0);
}
}
public abstract IEnumerable<RuleViolation> GetRuleViolations();
然后在XAML代码中:
<TextBox Style="{StaticResource ValidationTextBox}" >
<TextBox.Text>
<Binding Path="Name" UpdateSourceTrigger="PropertyChanged">
<Binding.ValidationRules>
<DataErrorValidationRule />
</Binding.ValidationRules>
</Binding>
</TextBox.Text>
</TextBox>
<Style x:Key="ValidationTextBox" TargetType="TextBox" >
<Setter Property="Validation.ErrorTemplate">
<Setter.Value>
<ControlTemplate>
<Border BorderBrush="Red" BorderThickness="1">
<AdornedElementPlaceholder />
</Border>
</ControlTemplate>
</Setter.Value>
</Setter>
<Style.Triggers>
<Trigger Property="Validation.HasError" Value="true">
<Setter Property="ToolTip"
Value="{Binding RelativeSource={RelativeSource Self},
Path=(Validation.Errors)[0].ErrorContent}"/>
</Trigger>
</Style.Triggers>
</Style>
如果您使用Asp.net作为UI,那么也有一些内置的方法来显示表单中的错误
我可以是一种方式,可能有更好的方式,但我希望这能有所帮助。好的,我不是100%确定你要做什么,但我要举个例子。不确定这将如何与MVVM一起工作,但我想它应该 在您的模型中添加以下内容:
public bool IsValid
{
get
{
return (GetRuleViolations().Count() == 0);
}
}
public override IEnumerable<RuleViolation> GetRuleViolations()
{
if (string.IsNullOrEmpty(Name))
yield return new RuleViolation("Name can't be empty", "Name");
else if (Name.Length >= 30)
yield return new RuleViolation("Name can't be more then 30 letters", "Name");
if (!string.IsNullOrEmpty(Address))
{
if (!Web.Contains("http"))
yield return new RuleViolation("Address must be bla bal", "Address");
}
// and so on
yield break;
}
现在,当您调用IsValid方法时,它将检查每个属性。如果您使用的是INotifyPropertyChanged,则每次更新任何已验证的属性时,我都会将IsValid添加为要更新的属性。如果您有一个基本模型类,您可以将其添加到其中,并在每个派生类中定义规则:
public bool IsValid
{
get
{
return (GetRuleViolations().Count() == 0);
}
}
public abstract IEnumerable<RuleViolation> GetRuleViolations();
然后在XAML代码中:
<TextBox Style="{StaticResource ValidationTextBox}" >
<TextBox.Text>
<Binding Path="Name" UpdateSourceTrigger="PropertyChanged">
<Binding.ValidationRules>
<DataErrorValidationRule />
</Binding.ValidationRules>
</Binding>
</TextBox.Text>
</TextBox>
<Style x:Key="ValidationTextBox" TargetType="TextBox" >
<Setter Property="Validation.ErrorTemplate">
<Setter.Value>
<ControlTemplate>
<Border BorderBrush="Red" BorderThickness="1">
<AdornedElementPlaceholder />
</Border>
</ControlTemplate>
</Setter.Value>
</Setter>
<Style.Triggers>
<Trigger Property="Validation.HasError" Value="true">
<Setter Property="ToolTip"
Value="{Binding RelativeSource={RelativeSource Self},
Path=(Validation.Errors)[0].ErrorContent}"/>
</Trigger>
</Style.Triggers>
</Style>
如果您使用Asp.net作为UI,那么也有一些内置的方法来显示表单中的错误
我可以是一种方式,可能有更好的方式,但我希望这能有所帮助。非常感谢您的回复,无论如何我会尝试实现这一点。那么您使用的是什么UI实现?非常感谢您的回复,无论如何我会尝试实现这一点。那么您使用的是什么UI实现?