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

3
如果我点击“创建”按钮,它会检查所有字段是否为空并进行验证。如果有任何信息缺失,它将显示一个验证消息。
视图:
<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 接口在模型类中。

模型:

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

它只在按钮点击之前进行验证。但我希望在按钮命令或单击事件之后进行验证。


你的代码中没有叫做“创建按钮”的按钮。 - BSG
这是我的按钮命令 public void CreateMember(object Sender) { Task.Run(() => AddNewMember()).Wait(); } - pavithra s
1个回答

1
你可以在AddNewMember()方法中将需要验证的属性设置为默认值,例如空字符串。这将触发验证。或者在实现了IDataErrorInfo接口的对象上创建一个公共的Validate()方法,在该方法中进行全部验证。因此,在创建新成员后,调用Validate()方法即可完成验证。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接