这个名字挺好 發表於 2025-7-10 23:57:00

WPF开发中的第三方库:ValueConverters的使用及属性验证方式

<h1 id="在wpf开发中你会经常遇到一些需要验证填写内容不能为空或者是其他的一些规则比如正则表达式等以下就是一个示例同时提供了很多种方式">在wpf开发中,你会经常遇到一些需要验证填写内容不能为空,或者是其他的一些规则,比如正则表达式等,以下就是一个示例,同时提供了很多种方式。</h1>
<h2 id="1方式1使用第三方库valueconverters">1.方式1.使用第三方库:ValueConverters</h2>
<h3 id="第一步在项目中nuget引用valueconverters">第一步:在项目中nuget引用ValueConverters</h3>
<p><img src="https://img2024.cnblogs.com/blog/2212230/202506/2212230-20250630222206394-720169609.png"></p>
<h3 id="第二步新建viewvalueconverterview">第二步:新建View:ValueConverterView</h3>
<pre><code>&lt;Window x:Class="WPFDemoMVVM.View.ValueConverterView"
                xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
                xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
                xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
                xmlns:local="clr-namespace:WPFDemoMVVM.View"
                xmlns:conv="clr-namespace:ValueConverters;assembly=ValueConverters"
                xmlns:vm="clr-namespace:WPFDemoMVVM.ViewModel"
                xmlns:hp="clr-namespace:WPFDemoMVVM.Helpers"
                WindowStartupLocation="CenterOwner"
                WindowStyle="ToolWindow"
                ResizeMode="NoResize"
                mc:Ignorable="d"
                Title="ValueConverterView" Height="600" Width="450"&gt;
        &lt;Window.Resources&gt;
                &lt;conv:BoolToVisibilityConverter x:Key="AgreeConvert" IsInverted="True"/&gt;

                &lt;Style TargetType="TextBox"&gt;
                        &lt;Setter Property="Margin" Value="5"/&gt;
                        &lt;Setter Property="Height" Value="28"/&gt;
                        &lt;Setter Property="VerticalContentAlignment" Value="Center"/&gt;
                        &lt;Style.Triggers&gt;
                                &lt;Trigger Property="Validation.HasError" Value="True"&gt;
                                        &lt;Setter Property="ToolTip" Value="{Binding RelativeSource={RelativeSource self},Path=(Validation.Errors).ErrorContent}"/&gt;
                                &lt;/Trigger&gt;
                        &lt;/Style.Triggers&gt;
                &lt;/Style&gt;

                &lt;Style x:Key="TextBlockShowTextStyle" TargetType="TextBlock"&gt;
                        &lt;Setter Property="FontSize" Value="14"/&gt;
                        &lt;Setter Property="VerticalAlignment" Value="Bottom"/&gt;
                &lt;/Style&gt;

                &lt;Style x:Key="ErrorStyle" TargetType="TextBlock"&gt;
                        &lt;Setter Property="FontSize" Value="14"/&gt;
                        &lt;Setter Property="Foreground" Value="Red"/&gt;
                        &lt;Setter Property="Margin" Value="0 2"/&gt;
                &lt;/Style&gt;

                &lt;Style x:Key="StackPanelStyle" TargetType="StackPanel"&gt;
                        &lt;Setter Property="Orientation" Value="Horizontal"/&gt;
                        &lt;Setter Property="HorizontalAlignment" Value="Center"/&gt;
                        &lt;Setter Property="VerticalAlignment" Value="Center"/&gt;
                        &lt;Setter Property="Margin" Value="0 10"/&gt;

                &lt;/Style&gt;

                &lt;Style x:Key="TextBoxStyle" TargetType="TextBox"&gt;
                        &lt;Setter Property="FontSize" Value="14"/&gt;
                        &lt;Setter Property="Margin" Value="5 0"/&gt;
                        &lt;Setter Property="Width" Value="100"/&gt;
                        &lt;Setter Property="Height" Value="24"/&gt;
                        &lt;Setter Property="VerticalAlignment" Value="Bottom"/&gt;
                &lt;/Style&gt;

                &lt;Style x:Key="PasswordBoxStyle" TargetType="PasswordBox"&gt;
                        &lt;Setter Property="FontSize" Value="14"/&gt;
                        &lt;Setter Property="Margin" Value="5 0"/&gt;
                        &lt;Setter Property="Width" Value="100"/&gt;
                        &lt;Setter Property="Height" Value="24"/&gt;
                        &lt;Setter Property="VerticalAlignment" Value="Bottom"/&gt;
                &lt;/Style&gt;

        &lt;/Window.Resources&gt;

        &lt;Grid Margin="5"&gt;
                &lt;Grid.RowDefinitions&gt;
                        &lt;RowDefinition/&gt;
                        &lt;RowDefinition/&gt;
                &lt;/Grid.RowDefinitions&gt;
                &lt;StackPanel Grid.Row="0"&gt;
                        &lt;StackPanel&gt;
                                &lt;StackPanel Style="{StaticResource StackPanelStyle}" &gt;
                                        &lt;TextBlock Text="用户名:" Style="{StaticResource TextBlockShowTextStyle}"/&gt;
                                        &lt;TextBox Name="userName" Style="{StaticResource TextBoxStyle}"/&gt;
                                &lt;/StackPanel&gt;
                                &lt;StackPanel Style="{StaticResource StackPanelStyle}"&gt;
                                        &lt;TextBlock Text="年龄:" Style="{StaticResource TextBlockShowTextStyle}"/&gt;
                                        &lt;TextBox Name="age"Style="{StaticResource TextBoxStyle}"/&gt;
                                &lt;/StackPanel&gt;
                                &lt;StackPanel Style="{StaticResource StackPanelStyle}"&gt;
                                        &lt;TextBlock Text="密码:" Style="{StaticResource TextBlockShowTextStyle}"/&gt;
                                        &lt;TextBox Name="passord" Style="{StaticResource TextBoxStyle}"/&gt;
                                &lt;/StackPanel&gt;
                                &lt;CheckBox Name="agree" Content="我已阅读" VerticalAlignment="Center" HorizontalAlignment="Center" Margin="0 5"/&gt;
                        &lt;/StackPanel&gt;
                        &lt;StackPanel Margin="0 10 0 0" HorizontalAlignment="Center"&gt;

                                &lt;TextBlock Style="{StaticResource ErrorStyle}" Text="用户名不能为空" Visibility="{Binding ElementName=userName,Path=Text,Converter={StaticResource userNameToVisibilityConverter}}"/&gt;
                                &lt;TextBlock Style="{StaticResource ErrorStyle}"Text="年龄需要在18-99" Visibility="{Binding ElementName=age,Path=Text,Converter={StaticResource ageToVisibilityConverter}}"/&gt;
                                &lt;TextBlock Style="{StaticResource ErrorStyle}"Text="密码长度不能小于8位" Visibility="{Binding ElementName=passord,Path=Text.Length,Converter={StaticResource passwordToVisibilityConverter}}"/&gt;
                                &lt;TextBlock Style="{StaticResource ErrorStyle}"Text="需要勾选我已阅读" Visibility="{Binding ElementName=agree,Path=IsChecked,Converter={StaticResource AgreeConvert}}"/&gt;
                        &lt;/StackPanel&gt;
                        &lt;Border BorderThickness="1" BorderBrush="Gray" Margin="0 5"&gt;&lt;/Border&gt;
                &lt;/StackPanel&gt;
        &lt;/Grid&gt;
&lt;/Window&gt;
</code></pre>
<h3 id="第三步引用命名空间xmlnsconvclr-namespacevalueconvertersassemblyvalueconverters并使用valueconvertergroup定义每个不一样的验证并显示如果按照规则填好就取消错误提示">第三步:引用命名空间:xmlns:conv="clr-namespace:ValueConverters;assembly=ValueConverters",并使用ValueConverterGroup定义每个不一样的验证,并显示,如果按照规则填好,就取消错误提示。</h3>
<pre><code>&lt;Window x:Class="WPFDemoMVVM.View.ValueConverterView"
                xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
                xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
                xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
                xmlns:local="clr-namespace:WPFDemoMVVM.View"
                xmlns:conv="clr-namespace:ValueConverters;assembly=ValueConverters"
                xmlns:vm="clr-namespace:WPFDemoMVVM.ViewModel"
                xmlns:hp="clr-namespace:WPFDemoMVVM.Helpers"
                WindowStartupLocation="CenterOwner"
                WindowStyle="ToolWindow"
                ResizeMode="NoResize"
                mc:Ignorable="d"
                Title="ValueConverterView" Height="600" Width="450"&gt;
        &lt;Window.Resources&gt;
                &lt;conv:BoolToVisibilityConverter x:Key="AgreeConvert" IsInverted="True"/&gt;

                &lt;conv:ValueConverterGroup x:Key="userNameToVisibilityConverter"&gt;
                        &lt;conv:StringIsNotNullOrEmptyConverter/&gt;
                        &lt;conv:BoolInverter/&gt;
                        &lt;conv:BoolToVisibilityConverter/&gt;
                &lt;/conv:ValueConverterGroup&gt;

                &lt;conv:ValueConverterGroup x:Key="passwordToVisibilityConverter"&gt;
                        &lt;conv:IsInRangeConverter MaxValue="16" MinValue="6"/&gt;
                        &lt;conv:BoolInverter/&gt;
                        &lt;conv:BoolToVisibilityConverter/&gt;
                &lt;/conv:ValueConverterGroup&gt;

                &lt;conv:ValueConverterGroup x:Key="ageToVisibilityConverter"&gt;
                        &lt;conv:StringToDecimalConverter/&gt;
                        &lt;conv:IsInRangeConverter MaxValue="99" MinValue="18"/&gt;
                        &lt;conv:BoolInverter/&gt;
                        &lt;conv:BoolToVisibilityConverter/&gt;
                &lt;/conv:ValueConverterGroup&gt;

                &lt;conv:StringIsNotNullOrEmptyConverter x:Key="StringIsNotNullOrEmptyConverter"/&gt;

                &lt;Style TargetType="TextBox"&gt;
                        &lt;Setter Property="Margin" Value="5"/&gt;
                        &lt;Setter Property="Height" Value="28"/&gt;
                        &lt;Setter Property="VerticalContentAlignment" Value="Center"/&gt;
                        &lt;Style.Triggers&gt;
                                &lt;Trigger Property="Validation.HasError" Value="True"&gt;
                                        &lt;Setter Property="ToolTip" Value="{Binding RelativeSource={RelativeSource self},Path=(Validation.Errors).ErrorContent}"/&gt;
                                &lt;/Trigger&gt;
                        &lt;/Style.Triggers&gt;
                &lt;/Style&gt;

                &lt;Style x:Key="TextBlockShowTextStyle" TargetType="TextBlock"&gt;
                        &lt;Setter Property="FontSize" Value="14"/&gt;
                        &lt;Setter Property="VerticalAlignment" Value="Bottom"/&gt;
                &lt;/Style&gt;

                &lt;Style x:Key="ErrorStyle" TargetType="TextBlock"&gt;
                        &lt;Setter Property="FontSize" Value="14"/&gt;
                        &lt;Setter Property="Foreground" Value="Red"/&gt;
                        &lt;Setter Property="Margin" Value="0 2"/&gt;
                &lt;/Style&gt;

                &lt;Style x:Key="StackPanelStyle" TargetType="StackPanel"&gt;
                        &lt;Setter Property="Orientation" Value="Horizontal"/&gt;
                        &lt;Setter Property="HorizontalAlignment" Value="Center"/&gt;
                        &lt;Setter Property="VerticalAlignment" Value="Center"/&gt;
                        &lt;Setter Property="Margin" Value="0 10"/&gt;

                &lt;/Style&gt;

                &lt;Style x:Key="TextBoxStyle" TargetType="TextBox"&gt;
                        &lt;Setter Property="FontSize" Value="14"/&gt;
                        &lt;Setter Property="Margin" Value="5 0"/&gt;
                        &lt;Setter Property="Width" Value="100"/&gt;
                        &lt;Setter Property="Height" Value="24"/&gt;
                        &lt;Setter Property="VerticalAlignment" Value="Bottom"/&gt;
                &lt;/Style&gt;

                &lt;Style x:Key="PasswordBoxStyle" TargetType="PasswordBox"&gt;
                        &lt;Setter Property="FontSize" Value="14"/&gt;
                        &lt;Setter Property="Margin" Value="5 0"/&gt;
                        &lt;Setter Property="Width" Value="100"/&gt;
                        &lt;Setter Property="Height" Value="24"/&gt;
                        &lt;Setter Property="VerticalAlignment" Value="Bottom"/&gt;
                &lt;/Style&gt;

        &lt;/Window.Resources&gt;

        &lt;Grid Margin="5"&gt;
                &lt;Grid.RowDefinitions&gt;
                        &lt;RowDefinition/&gt;
                        &lt;RowDefinition/&gt;
                &lt;/Grid.RowDefinitions&gt;
                &lt;StackPanel Grid.Row="0"&gt;
                        &lt;StackPanel&gt;
                                &lt;StackPanel Style="{StaticResource StackPanelStyle}" &gt;
                                        &lt;TextBlock Text="用户名:" Style="{StaticResource TextBlockShowTextStyle}"/&gt;
                                        &lt;TextBox Name="userName" Style="{StaticResource TextBoxStyle}"/&gt;
                                &lt;/StackPanel&gt;
                                &lt;StackPanel Style="{StaticResource StackPanelStyle}"&gt;
                                        &lt;TextBlock Text="年龄:" Style="{StaticResource TextBlockShowTextStyle}"/&gt;
                                        &lt;TextBox Name="age"Style="{StaticResource TextBoxStyle}"/&gt;
                                &lt;/StackPanel&gt;
                                &lt;StackPanel Style="{StaticResource StackPanelStyle}"&gt;
                                        &lt;TextBlock Text="密码:" Style="{StaticResource TextBlockShowTextStyle}"/&gt;
                                        &lt;TextBox Name="passord" Style="{StaticResource TextBoxStyle}"/&gt;
                                &lt;/StackPanel&gt;
                                &lt;CheckBox Name="agree" Content="我已阅读" VerticalAlignment="Center" HorizontalAlignment="Center" Margin="0 5"/&gt;
                        &lt;/StackPanel&gt;
                        &lt;StackPanel Margin="0 10 0 0" HorizontalAlignment="Center"&gt;

                                &lt;TextBlock Style="{StaticResource ErrorStyle}" Text="用户名不能为空" Visibility="{Binding ElementName=userName,Path=Text,Converter={StaticResource userNameToVisibilityConverter}}"/&gt;
                                &lt;TextBlock Style="{StaticResource ErrorStyle}"Text="年龄需要在18-99" Visibility="{Binding ElementName=age,Path=Text,Converter={StaticResource ageToVisibilityConverter}}"/&gt;
                                &lt;TextBlock Style="{StaticResource ErrorStyle}"Text="密码长度不能小于8位" Visibility="{Binding ElementName=passord,Path=Text.Length,Converter={StaticResource passwordToVisibilityConverter}}"/&gt;
                                &lt;TextBlock Style="{StaticResource ErrorStyle}"Text="需要勾选我已阅读" Visibility="{Binding ElementName=agree,Path=IsChecked,Converter={StaticResource AgreeConvert}}"/&gt;
                        &lt;/StackPanel&gt;
                        &lt;Border BorderThickness="1" BorderBrush="Gray" Margin="0 5"&gt;&lt;/Border&gt;
                &lt;/StackPanel&gt;
        &lt;/Grid&gt;
&lt;/Window&gt;
</code></pre>
<h3 id="效果如下">效果如下:</h3>
<p><img src="https://img2024.cnblogs.com/blog/2212230/202506/2212230-20250630223037360-339881398.png"></p>
<p>内容填充完,红色提示消失,</p>
<p><img src="https://img2024.cnblogs.com/blog/2212230/202506/2212230-20250630223252228-228658667.png"></p>
<p>当然,这还不算完善,我们可以使用一些更加完善的方式。如以下的形式</p>
<h2 id="2方式2使用idataerrorinfo直接后台绑定需要提示的内容">2.方式2:使用IDataErrorInfo,直接后台绑定需要提示的内容:</h2>
<h3 id="第一步view页面展示如下">第一步:View页面展示如下:</h3>
<pre><code>&lt;Window x:Class="WPFDemoMVVM.View.ValueConverterView"
                xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
                xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
                xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
                xmlns:local="clr-namespace:WPFDemoMVVM.View"
                xmlns:conv="clr-namespace:ValueConverters;assembly=ValueConverters"
                xmlns:vm="clr-namespace:WPFDemoMVVM.ViewModel"
                xmlns:hp="clr-namespace:WPFDemoMVVM.Helpers"
                WindowStartupLocation="CenterOwner"
                WindowStyle="ToolWindow"
                ResizeMode="NoResize"
                mc:Ignorable="d"
                Title="ValueConverterView" Height="600" Width="450"&gt;
        &lt;Window.Resources&gt;
                &lt;Style TargetType="TextBox"&gt;
                        &lt;Setter Property="Margin" Value="5"/&gt;
                        &lt;Setter Property="Height" Value="28"/&gt;
                        &lt;Setter Property="VerticalContentAlignment" Value="Center"/&gt;
                        &lt;Style.Triggers&gt;
                                &lt;Trigger Property="Validation.HasError" Value="True"&gt;
                                        &lt;Setter Property="ToolTip" Value="{Binding RelativeSource={RelativeSource self},Path=(Validation.Errors).ErrorContent}"/&gt;
                                &lt;/Trigger&gt;
                        &lt;/Style.Triggers&gt;
                &lt;/Style&gt;

                &lt;Style x:Key="TextBlockShowTextStyle" TargetType="TextBlock"&gt;
                        &lt;Setter Property="FontSize" Value="14"/&gt;
                        &lt;Setter Property="VerticalAlignment" Value="Bottom"/&gt;
                &lt;/Style&gt;

                &lt;Style x:Key="ErrorStyle" TargetType="TextBlock"&gt;
                        &lt;Setter Property="FontSize" Value="14"/&gt;
                        &lt;Setter Property="Foreground" Value="Red"/&gt;
                        &lt;Setter Property="Margin" Value="0 2"/&gt;
                &lt;/Style&gt;

                &lt;Style x:Key="StackPanelStyle" TargetType="StackPanel"&gt;
                        &lt;Setter Property="Orientation" Value="Horizontal"/&gt;
                        &lt;Setter Property="HorizontalAlignment" Value="Center"/&gt;
                        &lt;Setter Property="VerticalAlignment" Value="Center"/&gt;
                        &lt;Setter Property="Margin" Value="0 10"/&gt;
                &lt;/Style&gt;

                &lt;Style x:Key="TextBoxStyle" TargetType="TextBox"&gt;
                        &lt;Setter Property="FontSize" Value="14"/&gt;
                        &lt;Setter Property="Margin" Value="5 0"/&gt;
                        &lt;Setter Property="Width" Value="100"/&gt;
                        &lt;Setter Property="Height" Value="24"/&gt;
                        &lt;Setter Property="VerticalAlignment" Value="Bottom"/&gt;
                &lt;/Style&gt;

                &lt;Style x:Key="PasswordBoxStyle" TargetType="PasswordBox"&gt;
                        &lt;Setter Property="FontSize" Value="14"/&gt;
                        &lt;Setter Property="Margin" Value="5 0"/&gt;
                        &lt;Setter Property="Width" Value="100"/&gt;
                        &lt;Setter Property="Height" Value="24"/&gt;
                        &lt;Setter Property="VerticalAlignment" Value="Bottom"/&gt;
                &lt;/Style&gt;

        &lt;/Window.Resources&gt;

        &lt;Grid Margin="5"&gt;
                &lt;Grid.RowDefinitions&gt;
                        &lt;RowDefinition/&gt;
                        &lt;RowDefinition/&gt;
                &lt;/Grid.RowDefinitions&gt;
                &lt;StackPanel Grid.Row="0"&gt;
                &lt;/StackPanel&gt;

                &lt;StackPanel Grid.Row="1"&gt;
                        &lt;StackPanel Orientation="Horizontal" VerticalAlignment="Center" HorizontalAlignment="Center" Margin="0 10"&gt;
                                &lt;TextBlock Text="用户名:" VerticalAlignment="Bottom" FontSize="16"/&gt;
                                &lt;TextBox Text="{Binding UserName,ValidatesOnExceptions=True}" Style="{StaticResource TextBoxStyle}"/&gt;
                        &lt;/StackPanel&gt;

                        &lt;StackPanel Orientation="Horizontal" VerticalAlignment="Center" HorizontalAlignment="Center" Margin="0 10"&gt;
                                &lt;TextBlock Text="密码:" VerticalAlignment="Bottom" FontSize="16"/&gt;
                                &lt;TextBox Text="{Binding Password,ValidatesOnDataErrors=True}" Width="100" Height="24"/&gt;
                                &lt;!--&lt;PasswordBox hp:PasswordBoxHelper.BoundPassword="{Binding Passord,ValidatesOnDataErrors=True}" /&gt;--&gt;
                        &lt;/StackPanel&gt;

                        &lt;StackPanel Orientation="Horizontal" VerticalAlignment="Center" HorizontalAlignment="Center" Margin="0 10"&gt;
                                &lt;TextBlock Text="年龄:" VerticalAlignment="Bottom" FontSize="16"/&gt;
                                &lt;TextBox Text="{Binding Age,ValidatesOnDataErrors=True}" Style="{StaticResource TextBoxStyle}"/&gt;
                        &lt;/StackPanel&gt;
                        &lt;TextBox Margin="5" Text="{Binding Error,Mode=OneWay}" Foreground="red" Height="80"/&gt;      

                        &lt;Button Content="注册" Command="{Binding RegisterCommand}" Margin="0 20"/&gt;
                &lt;/StackPanel&gt;

        &lt;/Grid&gt;
&lt;/Window&gt;
</code></pre>
<h3 id="新建valueconverterviewmodel继承observableobjectidataerrorinfo类">新建ValueConverterViewModel,继承:ObservableObject,IDataErrorInfo类</h3>
<pre><code> public partial class ValueConverterViewModel:ObservableObject,IDataErrorInfo
{
       private string? userName;
       public string? UserName
       {
               get =&gt; userName;
               set
               {
                       SetProperty(ref userName, value);
                       OnPropertyChanged(nameof(Error));
               }
       }

       private int age;
       public int Age
       {
               get =&gt; age;
               set
               {               
                       SetProperty(ref age, value);
                       OnPropertyChanged(nameof(Error));
               }
       }

       public string Error
       {
               get
               {
                       var errors = new List&lt;string&gt;
                       {
                               this,
                               this,
                               this
                       };
                       return string.Join(Environment.NewLine, errors.Where(t =&gt; !string.IsNullOrEmpty(t)));
               }
       }

       private string? password;
       public string? Password
       {
               get =&gt; password;
               set
               {
                       SetProperty(ref password, value);
                       OnPropertyChanged(nameof(Error));
               }
       }

       public string this
       {
               get
               {
                       switch (columnName)
                       {
                               case nameof(UserName) when string.IsNullOrWhiteSpace(UserName):
                                       return "用户名必须不为空";
                               case nameof(UserName) when UserName.Length is &lt; 6 or &gt; 10:
                                       return "用户名长度必须在6和10之间";
                               case nameof(Age) when Ageis &lt; 18 or &gt; 110:
                                       return "年龄必须在18和110之间";
                               case nameof(Password) when string.IsNullOrWhiteSpace(Password):
                                       return "密码必须不为空";
                               case nameof(Password) when Password.Length is &lt; 8 or &gt; 20:
                                       return "密码长度必须在8和20之间";
                               default:
                                       return string.Empty;
                       }
               }
       }

       
       public void Register()
       {
               if (Error.Count() &gt; 0)
               {
                       return;
               }
               MessageBox.Show("注册成功");
       }
}
</code></pre>
<h3 id="效果如下-1">效果如下:</h3>
<p><img src="https://img2024.cnblogs.com/blog/2212230/202506/2212230-20250630231750371-1603413162.png"></p>
<p>验证通过,点击注册,效果如下:</p>
<p><img src="https://img2024.cnblogs.com/blog/2212230/202506/2212230-20250630231917427-760713331.png"></p>
<h2 id="3fluentvalidation--inotifydataerrorinfo-实现验证推荐使用">3.FluentValidation + INotifyDataErrorInfo 实现验证【推荐使用】</h2>
<h3 id="31-界面view如下">3.1 界面View如下:</h3>
<pre><code>&lt;Window x:Class="WPFDemoMVVM.View.ValueConverterView"
                xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
                xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
                xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
                xmlns:local="clr-namespace:WPFDemoMVVM.View"
                xmlns:conv="clr-namespace:ValueConverters;assembly=ValueConverters"
                xmlns:vm="clr-namespace:WPFDemoMVVM.ViewModel"
                xmlns:hp="clr-namespace:WPFDemoMVVM.Helpers"
                WindowStartupLocation="CenterOwner"
                WindowStyle="ToolWindow"
                ResizeMode="NoResize"
                mc:Ignorable="d"
                Title="ValueConverterView" Height="600" Width="450"&gt;
        &lt;Window.Resources&gt;
                &lt;conv:BoolToVisibilityConverter x:Key="AgreeConvert" IsInverted="True"/&gt;

                &lt;conv:ValueConverterGroup x:Key="userNameToVisibilityConverter"&gt;
                        &lt;conv:StringIsNotNullOrEmptyConverter/&gt;
                        &lt;conv:BoolInverter/&gt;
                        &lt;conv:BoolToVisibilityConverter/&gt;
                &lt;/conv:ValueConverterGroup&gt;

                &lt;conv:ValueConverterGroup x:Key="passwordToVisibilityConverter"&gt;
                        &lt;conv:IsInRangeConverter MaxValue="16" MinValue="6"/&gt;
                        &lt;conv:BoolInverter/&gt;
                        &lt;conv:BoolToVisibilityConverter/&gt;
                &lt;/conv:ValueConverterGroup&gt;

                &lt;conv:ValueConverterGroup x:Key="ageToVisibilityConverter"&gt;
                        &lt;conv:StringToDecimalConverter/&gt;
                        &lt;conv:IsInRangeConverter MaxValue="99" MinValue="18"/&gt;
                        &lt;conv:BoolInverter/&gt;
                        &lt;conv:BoolToVisibilityConverter/&gt;
                &lt;/conv:ValueConverterGroup&gt;

                &lt;conv:StringIsNotNullOrEmptyConverter x:Key="StringIsNotNullOrEmptyConverter"/&gt;


                &lt;Style TargetType="TextBox"&gt;
                        &lt;Setter Property="Margin" Value="5"/&gt;
                        &lt;Setter Property="Height" Value="28"/&gt;
                        &lt;Setter Property="VerticalContentAlignment" Value="Center"/&gt;
                        &lt;Style.Triggers&gt;
                                &lt;Trigger Property="Validation.HasError" Value="True"&gt;
                                        &lt;Setter Property="ToolTip" Value="{Binding RelativeSource={RelativeSource self},Path=(Validation.Errors).ErrorContent}"/&gt;
                                &lt;/Trigger&gt;
                        &lt;/Style.Triggers&gt;
                &lt;/Style&gt;

                &lt;Style x:Key="TextBlockShowTextStyle" TargetType="TextBlock"&gt;
                        &lt;Setter Property="FontSize" Value="14"/&gt;
                        &lt;Setter Property="VerticalAlignment" Value="Bottom"/&gt;
                &lt;/Style&gt;

                &lt;Style x:Key="ErrorStyle" TargetType="TextBlock"&gt;
                        &lt;Setter Property="FontSize" Value="14"/&gt;
                        &lt;Setter Property="Foreground" Value="Red"/&gt;
                        &lt;Setter Property="Margin" Value="0 2"/&gt;
                &lt;/Style&gt;

                &lt;Style x:Key="StackPanelStyle" TargetType="StackPanel"&gt;
                        &lt;Setter Property="Orientation" Value="Horizontal"/&gt;
                        &lt;Setter Property="HorizontalAlignment" Value="Center"/&gt;
                        &lt;Setter Property="VerticalAlignment" Value="Center"/&gt;
                        &lt;Setter Property="Margin" Value="0 10"/&gt;

                &lt;/Style&gt;

                &lt;Style x:Key="TextBoxStyle" TargetType="TextBox"&gt;
                        &lt;Setter Property="FontSize" Value="14"/&gt;
                        &lt;Setter Property="Margin" Value="5 0"/&gt;
                        &lt;Setter Property="Width" Value="100"/&gt;
                        &lt;Setter Property="Height" Value="24"/&gt;
                        &lt;Setter Property="VerticalAlignment" Value="Bottom"/&gt;
                &lt;/Style&gt;

                &lt;Style x:Key="PasswordBoxStyle" TargetType="PasswordBox"&gt;
                        &lt;Setter Property="FontSize" Value="14"/&gt;
                        &lt;Setter Property="Margin" Value="5 0"/&gt;
                        &lt;Setter Property="Width" Value="100"/&gt;
                        &lt;Setter Property="Height" Value="24"/&gt;
                        &lt;Setter Property="VerticalAlignment" Value="Bottom"/&gt;
                &lt;/Style&gt;


                &lt;ControlTemplate x:Key="ValidationErrorTemplate"&gt;
                        &lt;DockPanel&gt;
                                &lt;TextBlock Foreground="Red" DockPanel.Dock="Bottom" FontSize="12" Text="{Binding .ErrorContent}" /&gt;
                                &lt;AdornedElementPlaceholder /&gt;
                        &lt;/DockPanel&gt;
                &lt;/ControlTemplate&gt;


        &lt;/Window.Resources&gt;

        &lt;Grid Margin="5"&gt;
                &lt;Grid.RowDefinitions&gt;
                        &lt;RowDefinition/&gt;
                        &lt;RowDefinition/&gt;
                &lt;/Grid.RowDefinitions&gt;
                &lt;StackPanel Grid.Row="0"&gt;
                        &lt;StackPanel&gt;
                                &lt;StackPanel Style="{StaticResource StackPanelStyle}" &gt;
                                        &lt;TextBlock Text="用户名:" Style="{StaticResource TextBlockShowTextStyle}"/&gt;
                                        &lt;TextBox Name="userName" Style="{StaticResource TextBoxStyle}"/&gt;
                                &lt;/StackPanel&gt;
                                &lt;StackPanel Style="{StaticResource StackPanelStyle}"&gt;
                                        &lt;TextBlock Text="年龄:" Style="{StaticResource TextBlockShowTextStyle}"/&gt;
                                        &lt;TextBox Name="age"Style="{StaticResource TextBoxStyle}"/&gt;
                                &lt;/StackPanel&gt;
                                &lt;StackPanel Style="{StaticResource StackPanelStyle}"&gt;
                                        &lt;TextBlock Text="密码:" Style="{StaticResource TextBlockShowTextStyle}"/&gt;
                                        &lt;TextBox Name="passord" Style="{StaticResource TextBoxStyle}"/&gt;
                                &lt;/StackPanel&gt;
                                &lt;CheckBox Name="agree" Content="我已阅读" VerticalAlignment="Center" HorizontalAlignment="Center" Margin="0 5"/&gt;
                        &lt;/StackPanel&gt;
                        &lt;StackPanel Margin="0 10 0 0" HorizontalAlignment="Center"&gt;

                                &lt;TextBlock Style="{StaticResource ErrorStyle}" Text="用户名不能为空" Visibility="{Binding ElementName=userName,Path=Text,Converter={StaticResource userNameToVisibilityConverter}}"/&gt;
                                &lt;TextBlock Style="{StaticResource ErrorStyle}"Text="年龄需要在18-99" Visibility="{Binding ElementName=age,Path=Text,Converter={StaticResource ageToVisibilityConverter}}"/&gt;
                                &lt;TextBlock Style="{StaticResource ErrorStyle}"Text="密码长度不能小于8位" Visibility="{Binding ElementName=passord,Path=Text.Length,Converter={StaticResource passwordToVisibilityConverter}}"/&gt;
                                &lt;TextBlock Style="{StaticResource ErrorStyle}"Text="需要勾选我已阅读" Visibility="{Binding ElementName=agree,Path=IsChecked,Converter={StaticResource AgreeConvert}}"/&gt;
                        &lt;/StackPanel&gt;
                        &lt;Border BorderThickness="1" BorderBrush="Gray" Margin="0 5"&gt;&lt;/Border&gt;
                &lt;/StackPanel&gt;

                &lt;StackPanel Grid.Row="1"&gt;
                        &lt;StackPanel Orientation="Horizontal" VerticalAlignment="Center" HorizontalAlignment="Center" Margin="0 10"&gt;
                                &lt;TextBlock Text="用户名:" VerticalAlignment="Bottom" FontSize="16"/&gt;
                                &lt;TextBox Text="{Binding UserName,ValidatesOnExceptions=True,NotifyOnValidationError=True,UpdateSourceTrigger=PropertyChanged}" Validation.ErrorTemplate="{StaticResource ValidationErrorTemplate}" Style="{StaticResource TextBoxStyle}"/&gt;
                        &lt;/StackPanel&gt;

                        &lt;StackPanel Orientation="Horizontal" VerticalAlignment="Center" HorizontalAlignment="Center" Margin="0 10"&gt;
                                &lt;TextBlock Text="密码:" VerticalAlignment="Bottom" FontSize="16"/&gt;
                                &lt;TextBox Text="{Binding Password,ValidatesOnDataErrors=True,NotifyOnValidationError=True,UpdateSourceTrigger=PropertyChanged}" Validation.ErrorTemplate="{StaticResource ValidationErrorTemplate}" Style="{StaticResource TextBoxStyle}"/&gt;
                        &lt;/StackPanel&gt;

                        &lt;StackPanel Orientation="Horizontal" VerticalAlignment="Center" HorizontalAlignment="Center" Margin="0 10"&gt;
                                &lt;TextBlock Text="年龄:" VerticalAlignment="Bottom" FontSize="16"/&gt;
                                &lt;TextBox Text="{Binding Age,ValidatesOnDataErrors=True,NotifyOnValidationError=True,UpdateSourceTrigger=PropertyChanged}" Validation.ErrorTemplate="{StaticResource ValidationErrorTemplate}" Style="{StaticResource TextBoxStyle}"/&gt;
                        &lt;/StackPanel&gt;
                        &lt;Button Content="注册" Command="{Binding RegisterCommand}" Margin="0 20"/&gt;
                &lt;/StackPanel&gt;
        &lt;/Grid&gt;
&lt;/Window&gt;
</code></pre>
<p>当前的 View 使用了 INotifyDataErrorInfo 搭配 FluentValidation 进行验证,已经是很现代且推荐的方式,但 WPF 的默认控件不直接显示错误提示文本,还需要设置 验证模板 (ValidationTemplate) 或使用 Adorner 来显示错误提示。绑定需要设置 NotifyOnValidationError=True。</p>
<h3 id="32-安装fluentvalidation">3.2 安装FluentValidation</h3>
<p>Install-Package FluentValidation</p>
<h3 id="33-新建viewmodel类valueconverterviewmodel继承observableobject-inotifydataerrorinfo">3.3 新建ViewModel类:ValueConverterViewModel,继承ObservableObject, INotifyDataErrorInfo</h3>
<pre><code>public partial class ValueConverterViewModel : ObservableObject, INotifyDataErrorInfo
{
        private readonly IValidator&lt;ValueConverterViewModel&gt; _validator;
        private readonly Dictionary&lt;string, List&lt;string&gt;&gt; _errors = new();

        public ValueConverterViewModel()
        {
                _validator = new ValueConverterViewModelValidator();
        }

       
        private string? userName;

       
        private int age;

       
        private string? password;

        partial void OnUserNameChanged(string? value)
        {
                ValidateProperty(nameof(UserName));
        }

        partial void OnAgeChanged(int value)
        {
                ValidateProperty(nameof(Age));
        }

        partial void OnPasswordChanged(string? value)
        {
                ValidateProperty(nameof(Password));
        }

        public bool HasErrors =&gt; _errors.Any();

        public event EventHandler&lt;DataErrorsChangedEventArgs&gt;? ErrorsChanged;

        public IEnumerable GetErrors(string? propertyName)
        {
                if (propertyName != null &amp;&amp; _errors.TryGetValue(propertyName, out var errors))
                {
                        return errors;
                }
                return Enumerable.Empty&lt;string&gt;();
        }

        private void ValidateProperty(string propertyName)
        {
                //var context = new ValidationContext&lt;ValueConverterViewModel&gt;(this)
                //    .CloneForMember(propertyName);

                // 创建只验证特定属性的 ValidatorSelector
                var selector = new MemberNameValidatorSelector(new[] { propertyName });

                // 正确创建 ValidationContext 并传入 selector
                var context = new ValidationContext&lt;ValueConverterViewModel&gt;(this, new PropertyChain(), selector);

                var result = _validator.Validate(context);

                // 移除旧错误
                if (_errors.ContainsKey(propertyName))
                {
                        _errors.Remove(propertyName);
                        OnErrorsChanged(propertyName);
                }

                // 添加新错误
                var propertyErrors = result.Errors
                        .Where(e =&gt; e.PropertyName == propertyName)
                        .Select(e =&gt; e.ErrorMessage)
                        .ToList();

                if (propertyErrors.Any())
                {
                        _errors = propertyErrors;
                        OnErrorsChanged(propertyName);
                }
        }

        private void OnErrorsChanged(string propertyName)
        {
                ErrorsChanged?.Invoke(this, new DataErrorsChangedEventArgs(propertyName));
        }

       
        public void Register()
        {
                MessageBox.Show("注册成功");
        }

        public bool CanRegister() =&gt; !HasErrors;

}
</code></pre>
<h3 id="34-新建类valueconverterviewmodelvalidator这是用于属性跪着提示类">3.4 新建类ValueConverterViewModelValidator,这是用于属性跪着提示类</h3>
<pre><code>public class ValueConverterViewModelValidator : AbstractValidator&lt;ValueConverterViewModel&gt;
{
        public ValueConverterViewModelValidator()
        {
                RuleFor(x =&gt; x.UserName)
                        .NotEmpty().WithMessage("用户名必须不为空")
                        .Length(6, 10).WithMessage("用户名长度必须在6和10之间");

                RuleFor(x =&gt; x.Age)
                        .InclusiveBetween(18, 110).WithMessage("年龄必须在18和110之间");

                RuleFor(x =&gt; x.Password)
                        .NotEmpty().WithMessage("密码必须不为空")
                        .Length(8, 20).WithMessage("密码长度必须在8和20之间");
        }
}
</code></pre>
<h3 id="35-效果如下">3.5 效果如下:</h3>
<p><img src="https://img2024.cnblogs.com/blog/2212230/202507/2212230-20250710231955311-771490843.png"></p>
<h2 id="4dataannotationsvalidatorinotifydataerrorinfo-实现验证">4.DataAnnotationsValidator+INotifyDataErrorInfo 实现验证</h2>
<h3 id="41-新建view页如下">4.1 新建View页如下:</h3>
<pre><code>&lt;Window x:Class="WPFDemoMVVM.View.ValueConverterView"
                xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
                xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
                xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
                xmlns:local="clr-namespace:WPFDemoMVVM.View"
                xmlns:conv="clr-namespace:ValueConverters;assembly=ValueConverters"
                xmlns:vm="clr-namespace:WPFDemoMVVM.ViewModel"
                xmlns:hp="clr-namespace:WPFDemoMVVM.Helpers"
                WindowStartupLocation="CenterOwner"
                WindowStyle="ToolWindow"
                ResizeMode="NoResize"
                mc:Ignorable="d"
                Title="ValueConverterView" Height="600" Width="450"&gt;
        &lt;Window.Resources&gt;
                &lt;conv:BoolToVisibilityConverter x:Key="AgreeConvert" IsInverted="True"/&gt;

                &lt;conv:ValueConverterGroup x:Key="userNameToVisibilityConverter"&gt;
                        &lt;conv:StringIsNotNullOrEmptyConverter/&gt;
                        &lt;conv:BoolInverter/&gt;
                        &lt;conv:BoolToVisibilityConverter/&gt;
                &lt;/conv:ValueConverterGroup&gt;

                &lt;conv:ValueConverterGroup x:Key="passwordToVisibilityConverter"&gt;
                        &lt;conv:IsInRangeConverter MaxValue="16" MinValue="6"/&gt;
                        &lt;conv:BoolInverter/&gt;
                        &lt;conv:BoolToVisibilityConverter/&gt;
                &lt;/conv:ValueConverterGroup&gt;

                &lt;conv:ValueConverterGroup x:Key="ageToVisibilityConverter"&gt;
                        &lt;conv:StringToDecimalConverter/&gt;
                        &lt;conv:IsInRangeConverter MaxValue="99" MinValue="18"/&gt;
                        &lt;conv:BoolInverter/&gt;
                        &lt;conv:BoolToVisibilityConverter/&gt;
                &lt;/conv:ValueConverterGroup&gt;
                &lt;conv:StringIsNotNullOrEmptyConverter x:Key="StringIsNotNullOrEmptyConverter"/&gt;

                &lt;Style TargetType="TextBox"&gt;
                        &lt;Setter Property="Margin" Value="5"/&gt;
                        &lt;Setter Property="Height" Value="28"/&gt;
                        &lt;Setter Property="VerticalContentAlignment" Value="Center"/&gt;
                        &lt;Style.Triggers&gt;
                                &lt;Trigger Property="Validation.HasError" Value="True"&gt;
                                        &lt;Setter Property="ToolTip" Value="{Binding RelativeSource={RelativeSource self},Path=(Validation.Errors).ErrorContent}"/&gt;
                                &lt;/Trigger&gt;
                        &lt;/Style.Triggers&gt;
                &lt;/Style&gt;

                &lt;Style x:Key="TextBlockShowTextStyle" TargetType="TextBlock"&gt;
                        &lt;Setter Property="FontSize" Value="14"/&gt;
                        &lt;Setter Property="VerticalAlignment" Value="Bottom"/&gt;
                &lt;/Style&gt;

                &lt;Style x:Key="ErrorStyle" TargetType="TextBlock"&gt;
                        &lt;Setter Property="FontSize" Value="14"/&gt;
                        &lt;Setter Property="Foreground" Value="Red"/&gt;
                        &lt;Setter Property="Margin" Value="0 2"/&gt;
                &lt;/Style&gt;

                &lt;Style x:Key="StackPanelStyle" TargetType="StackPanel"&gt;
                        &lt;Setter Property="Orientation" Value="Horizontal"/&gt;
                        &lt;Setter Property="HorizontalAlignment" Value="Center"/&gt;
                        &lt;Setter Property="VerticalAlignment" Value="Center"/&gt;
                        &lt;Setter Property="Margin" Value="0 10"/&gt;

                &lt;/Style&gt;

                &lt;Style x:Key="TextBoxStyle" TargetType="TextBox"&gt;
                        &lt;Setter Property="FontSize" Value="14"/&gt;
                        &lt;Setter Property="Margin" Value="5 0"/&gt;
                        &lt;Setter Property="Width" Value="100"/&gt;
                        &lt;Setter Property="Height" Value="24"/&gt;
                        &lt;Setter Property="VerticalAlignment" Value="Bottom"/&gt;
                &lt;/Style&gt;

                &lt;Style x:Key="PasswordBoxStyle" TargetType="PasswordBox"&gt;
                        &lt;Setter Property="FontSize" Value="14"/&gt;
                        &lt;Setter Property="Margin" Value="5 0"/&gt;
                        &lt;Setter Property="Width" Value="100"/&gt;
                        &lt;Setter Property="Height" Value="24"/&gt;
                        &lt;Setter Property="VerticalAlignment" Value="Bottom"/&gt;
                &lt;/Style&gt;

                &lt;ControlTemplate x:Key="ValidationErrorTemplate"&gt;
                        &lt;DockPanel&gt;
                                &lt;TextBlock Foreground="Red" DockPanel.Dock="Bottom" FontSize="12" Text="{Binding .ErrorContent}" /&gt;
                                &lt;AdornedElementPlaceholder /&gt;
                        &lt;/DockPanel&gt;
                &lt;/ControlTemplate&gt;

                &lt;Style x:Key="TextBoxCommonStyle" TargetType="TextBox"&gt;
                        &lt;Setter Property="FontSize" Value="14"/&gt;
                        &lt;Setter Property="Margin" Value="5 0"/&gt;
                        &lt;Setter Property="Width" Value="100"/&gt;
                        &lt;Setter Property="Height" Value="24"/&gt;
                        &lt;Setter Property="VerticalAlignment" Value="Bottom"/&gt;
                        &lt;Setter Property="Validation.ErrorTemplate" Value="{StaticResource ValidationErrorTemplate}" /&gt;
                        &lt;Style.Triggers&gt;
                                &lt;Trigger Property="Validation.HasError" Value="True"&gt;
                                        &lt;Setter Property="ToolTip"
                                        Value="{Binding RelativeSource={RelativeSource Self},
                                                                        Path=(Validation.Errors).ErrorContent}" /&gt;
                                &lt;/Trigger&gt;
                        &lt;/Style.Triggers&gt;
                &lt;/Style&gt;

        &lt;/Window.Resources&gt;
        &lt;Grid Margin="5"&gt;
                &lt;Grid.RowDefinitions&gt;
                        &lt;RowDefinition/&gt;
                        &lt;RowDefinition/&gt;
                &lt;/Grid.RowDefinitions&gt;
                &lt;StackPanel Grid.Row="0"&gt;
                        &lt;StackPanel&gt;
                                &lt;StackPanel Style="{StaticResource StackPanelStyle}" &gt;
                                        &lt;TextBlock Text="用户名:" Style="{StaticResource TextBlockShowTextStyle}"/&gt;
                                        &lt;TextBox Name="userName" Style="{StaticResource TextBoxStyle}"/&gt;
                                &lt;/StackPanel&gt;
                                &lt;StackPanel Style="{StaticResource StackPanelStyle}"&gt;
                                        &lt;TextBlock Text="年龄:" Style="{StaticResource TextBlockShowTextStyle}"/&gt;
                                        &lt;TextBox Name="age"Style="{StaticResource TextBoxStyle}"/&gt;
                                &lt;/StackPanel&gt;
                                &lt;StackPanel Style="{StaticResource StackPanelStyle}"&gt;
                                        &lt;TextBlock Text="密码:" Style="{StaticResource TextBlockShowTextStyle}"/&gt;
                                        &lt;TextBox Name="passord" Style="{StaticResource TextBoxStyle}"/&gt;
                                &lt;/StackPanel&gt;
                                &lt;CheckBox Name="agree" Content="我已阅读" VerticalAlignment="Center" HorizontalAlignment="Center" Margin="0 5"/&gt;
                        &lt;/StackPanel&gt;
                        &lt;StackPanel Margin="0 10 0 0" HorizontalAlignment="Center"&gt;

                                &lt;TextBlock Style="{StaticResource ErrorStyle}" Text="用户名不能为空" Visibility="{Binding ElementName=userName,Path=Text,Converter={StaticResource userNameToVisibilityConverter}}"/&gt;
                                &lt;TextBlock Style="{StaticResource ErrorStyle}"Text="年龄需要在18-99" Visibility="{Binding ElementName=age,Path=Text,Converter={StaticResource ageToVisibilityConverter}}"/&gt;
                                &lt;TextBlock Style="{StaticResource ErrorStyle}"Text="密码长度不能小于8位" Visibility="{Binding ElementName=passord,Path=Text.Length,Converter={StaticResource passwordToVisibilityConverter}}"/&gt;
                                &lt;TextBlock Style="{StaticResource ErrorStyle}"Text="需要勾选我已阅读" Visibility="{Binding ElementName=agree,Path=IsChecked,Converter={StaticResource AgreeConvert}}"/&gt;
                        &lt;/StackPanel&gt;
                        &lt;Border BorderThickness="1" BorderBrush="Gray" Margin="0 5"&gt;&lt;/Border&gt;
                &lt;/StackPanel&gt;

                &lt;StackPanel Grid.Row="1"&gt;
                        &lt;StackPanel Orientation="Horizontal" VerticalAlignment="Center" HorizontalAlignment="Center" Margin="0 10"&gt;
                                &lt;TextBlock Text="用户名:" VerticalAlignment="Bottom" FontSize="16"/&gt;
                                &lt;TextBox Text="{Binding UserName, ValidatesOnDataErrors=True, NotifyOnValidationError=True, UpdateSourceTrigger=PropertyChanged}" Style="{StaticResource TextBoxCommonStyle}"/&gt;
                        &lt;/StackPanel&gt;
                        &lt;StackPanel Orientation="Horizontal" VerticalAlignment="Center" HorizontalAlignment="Center" Margin="0 10"&gt;
                                &lt;TextBlock Text="密码:" VerticalAlignment="Bottom" FontSize="16"/&gt;
                                &lt;TextBox Text="{Binding Password, ValidatesOnDataErrors=True, NotifyOnValidationError=True, UpdateSourceTrigger=PropertyChanged}" Style="{StaticResource TextBoxCommonStyle}"/&gt;
                        &lt;/StackPanel&gt;
                        &lt;StackPanel Orientation="Horizontal" VerticalAlignment="Center" HorizontalAlignment="Center" Margin="0 10"&gt;
                                &lt;TextBlock Text="年龄:" VerticalAlignment="Bottom" FontSize="16"/&gt;
                                &lt;TextBox Text="{Binding Age, ValidatesOnDataErrors=True, NotifyOnValidationError=True, UpdateSourceTrigger=PropertyChanged}" Style="{StaticResource TextBoxCommonStyle}"/&gt;
                        &lt;/StackPanel&gt;
                        &lt;Button Content="注册" Command="{Binding RegisterCommand}" Margin="0 20"/&gt;
                &lt;/StackPanel&gt;
        &lt;/Grid&gt;
&lt;/Window&gt;
</code></pre>
<h3 id="42-新建viewmodelvalueconverterviewmodel继承observablevalidator">4.2 新建ViewModel:ValueConverterViewModel,继承ObservableValidator</h3>
<pre><code>using CommunityToolkit.Mvvm.ComponentModel;
using CommunityToolkit.Mvvm.Input;
using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Windows;


namespace WPFDemoMVVM.ViewModel
{
        #region INotifyDataErrorInfo + DataAnnotations 实现属性验证
        public partial class ValueConverterViewModel : ObservableValidator
        {
                private readonly Dictionary&lt;string, List&lt;string&gt;&gt; _errors = new();

               
               
               
               
                private string? userName;

               
               
               
               
                private string? password;

               
               
                private int age;

                public ValueConverterViewModel()
                {
                        PropertyChanged += (s, e) =&gt; ValidateProperty(e.PropertyName!);
                }

                private void ValidateProperty(string propertyName)
                {
                        if (string.IsNullOrEmpty(propertyName))
                                return;

                        var propertyInfo = GetType().GetProperty(propertyName);
                        if (propertyInfo == null)
                                return;

                        var context = new ValidationContext(this)
                        {
                                MemberName = propertyName
                        };

                        var results = new List&lt;System.ComponentModel.DataAnnotations.ValidationResult&gt;();
                        var propertyValue = propertyInfo.GetValue(this);
                        Validator.TryValidateProperty(propertyValue, context, results);

                        if (_errors.ContainsKey(propertyName))
                        {
                                _errors.Remove(propertyName);
                                OnErrorsChanged(propertyName);
                        }

                        if (results.Any())
                        {
                                _errors = results.Select(r =&gt; r.ErrorMessage!).ToList();
                                OnErrorsChanged(propertyName);
                        }
                }

                public bool HasErrors =&gt; _errors.Any();

                public event EventHandler&lt;DataErrorsChangedEventArgs&gt;? ErrorsChanged;

                public IEnumerable GetErrors(string? propertyName)
                {
                        if (string.IsNullOrEmpty(propertyName))
                                return _errors.SelectMany(e =&gt; e.Value);
                        return _errors.TryGetValue(propertyName, out var errors) ? errors : Enumerable.Empty&lt;string&gt;();
                }

                private void OnErrorsChanged(string propertyName) =&gt;
                        ErrorsChanged?.Invoke(this, new DataErrorsChangedEventArgs(propertyName));

               
                public void Register()
                {
                        MessageBox.Show("注册成功");
                }

                public bool CanRegister() =&gt; !HasErrors;
        }
        #endregion
}
</code></pre>
<p>效果如下:<br>
<img src="https://img2024.cnblogs.com/blog/2212230/202507/2212230-20250710235548184-661082059.png"></p>
<p>源代码地址如下:https://gitee.com/chenshibao/wpfdemo.git</p>
<p><strong>如果本文介绍对你有帮助,可以一键四连:点赞+评论+收藏+推荐,谢谢!</strong></p><br><br>
来源:https://www.cnblogs.com/chenshibao/p/18958710
頁: [1]
查看完整版本: WPF开发中的第三方库:ValueConverters的使用及属性验证方式