WPF中TextBox输入框利用Validation进行数据验证

//继承一个ValidationRule,重写Validate方法
public class NumberValidationRule : ValidationRule
    {
        public override ValidationResult Validate(object value, CultureInfo cultureInfo)
        {
            double myValue = 0;
            if (double.TryParse(value.ToString(), out myValue))
            {
                if (myValue >= 0 && myValue <= 100)
                {
                    return new ValidationResult(true, null);
                }
            }

            return new ValidationResult(false, "请输入 0 至 100的数字");
        }
    }

前端XAML代码调用上面的类,并自定义错误提示模板

 <TextBox x:Name="_textbox" Width="191" Height="30" Canvas.Left="41" Canvas.Top="25">
     <TextBox.Text>
         <Binding Path="Number" UpdateSourceTrigger="PropertyChanged">
             <Binding.ValidationRules>
                 <validation:NumberValidationRule ValidatesOnTargetUpdated="True" />
             </Binding.ValidationRules>
         </Binding>
     </TextBox.Text>
     <Validation.ErrorTemplate>
         <ControlTemplate>
             <StackPanel Orientation="Horizontal" x:Name="_stack">
                 <Border>
                     <Grid>
                         <AdornedElementPlaceholder x:Name="adorner"/>
                     </Grid>
                 </Border>
                 <Grid Width="10"/>
                 <Popup x:Name="popup" AllowsTransparency="True" Placement="Left" >
                     <Border x:Name="errorBorder" Background="#ffdc000c" Opacity="0" MinHeight="30" >
                         <TextBlock Margin="5,0" Text="{Binding ElementName=adorner, Path=AdornedElement.(Validation.Errors).CurrentItem.ErrorContent}" Foreground="White" TextWrapping="Wrap" VerticalAlignment="Center"/>
                     </Border>
                 </Popup>
             </StackPanel>
             <ControlTemplate.Triggers>
                 <DataTrigger Value="True" Binding="{Binding ElementName=adorner,Path=AdornedElement.IsKeyboardFocused}">
                     <!--<DataTrigger.Binding>
                         <Binding ElementName="adorner" Path="AdornedElement.IsKeyboardFocused" />
                     </DataTrigger.Binding>-->

                     <DataTrigger.Setters>
                         <Setter Property="IsOpen" TargetName="popup" Value="true"/>
                     </DataTrigger.Setters>
                     <DataTrigger.EnterActions>
                         <BeginStoryboard x:Name="fadeInStoryboard">
                             <Storyboard>
                                 <DoubleAnimation Duration="00:00:00.15" Storyboard.TargetName="errorBorder" Storyboard.TargetProperty="Opacity" To="1"/>
                             </Storyboard>
                         </BeginStoryboard>
                     </DataTrigger.EnterActions>
                     <DataTrigger.ExitActions>
                         <StopStoryboard BeginStoryboardName="fadeInStoryboard"/>
                         <BeginStoryboard x:Name="fadeOutStoryBoard">
                             <Storyboard>
                                 <DoubleAnimation Duration="00:00:00" Storyboard.TargetName="errorBorder" Storyboard.TargetProperty="Opacity" To="0"/>
                             </Storyboard>
                         </BeginStoryboard>
                     </DataTrigger.ExitActions>
                 </DataTrigger>
             </ControlTemplate.Triggers>
         </ControlTemplate>
     </Validation.ErrorTemplate>
 </TextBox>

Binding Path="Number"中的Number是当前UI的DataContext中的一个属性,DataContext来源于一个ViewModel

WPF中TextBox输入框利用Validation进行数据验证_第1张图片

你可能感兴趣的:(WPF)