gpt4 book ai didi

c# - 如何使用 WPF MVVM 验证所有字段?

转载 作者:太空宇宙 更新时间:2023-11-03 13:07:38 24 4
gpt4 key购买 nike

如果我单击“创建”按钮,它会检查所有字段是否为空并验证是否有效。如果缺少任何内容,它会显示验证消息。

查看:

<ControlTemplate x:Key="validationErrorTemplate">
<ControlTemplate.Resources>
<Style x:Key="textblockErrorTooltip" TargetType="TextBlock">
<Setter Property="HorizontalAlignment" Value="Center" />
<Setter Property="VerticalAlignment" Value="Center" />
<Setter Property="FontWeight" Value="Bold" />
<Setter Property="Foreground" Value="White" />
<Setter Property="Margin" Value="10 0 10 0" />
</Style>
</ControlTemplate.Resources>
<DockPanel>
<Border Height="Auto"
Margin="5,0,0,0"
Background="#DC000C"
CornerRadius="3"
DockPanel.Dock="right">
<TextBlock Style="{StaticResource textblockErrorTooltip}" DockPanel.Dock="Top"
Text="{Binding ElementName=customAdorner, Path=AdornedElement.(Validation.Errors)[0].ErrorContent}" />
</Border>
<AdornedElementPlaceholder Name="customAdorner">
<Border BorderBrush="#DC000C" BorderThickness="1.3" />
</AdornedElementPlaceholder>
</DockPanel>
</ControlTemplate>

<TextBlock FontSize="20" Text="UserName" Name="txtuserName" HorizontalAlignment="Left" VerticalAlignment="Top" Height="30" Width="163" Canvas.Left="426" Canvas.Top="172"/>
<TextBox MaxLength="10" Name="txtUserName" Validation.ErrorTemplate="{StaticResource validationErrorTemplate}" Text="{Binding MemberModelObj.MemberName, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged, ValidatesOnDataErrors=True, NotifyOnValidationError=True}" FontSize="18" Style="{StaticResource textblockErrorTooltip}" GotFocus="TextUserName_GotFocus" Background="Transparent" BorderThickness="0" HorizontalAlignment="Left" VerticalAlignment="Center" Height="24" Width="370" ></TextBox>
<TextBlock FontSize="20" Text="FullName" HorizontalAlignment="Left" VerticalAlignment="Top" Height="30" Width="102" Canvas.Left="426" Canvas.Top="241" RenderTransformOrigin="-1.275,0.967"/>
<TextBox FontSize="18" Validation.ErrorTemplate="{StaticResource validationErrorTemplate}" GotFocus="TextFull_GotFocus" Background="Transparent" BorderThickness="0" Text="{Binding MemberModelObj.MemberFullName,Mode=TwoWay, UpdateSourceTrigger=PropertyChanged, ValidatesOnDataErrors=True, NotifyOnValidationError=True}" HorizontalAlignment="Left" VerticalAlignment="Center" Height="24" Width="367" Name="txtFirstName" />

我在模型类中使用 IDataErrorInfo 接口(interface)。

型号:

public string this[string columnName]
{
set { columnName = value; }
get
{
switch (columnName)
{
case "MemberName":
if (String.IsNullOrEmpty(MemberName))
{
result = "! Please Enter a User Name";
}
else if (MemberName.Length >= 30)
{
result = "! Name can not be longer than 30 characters.";
}
break;
}
return result;
}
}

它仅在单击按钮之前验证。但是我在按钮命令或点击事件之后排除了它。

最佳答案

您可以将需要验证的属性设置为默认值,例如 AddNewMember() 方法中的空字符串。这将触发验证。或者在实现 IDataErrorInfo 接口(interface)的对象上创建公共(public) Validate() 方法,该方法将验证所有内容。因此,当您创建一个新成员时,请在构建完成后调用 Validate() 方法。

关于c# - 如何使用 WPF MVVM 验证所有字段?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30212954/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com