【问题标题】:How to show WPF ValidationResult in a seperate textblock?如何在单独的文本块中显示 WPF ValidationResult?
【发布时间】:2010-01-19 09:15:07
【问题描述】:

我试图在 WPF 中使用带有验证规则的数据绑定控件的验证输入。我有一个posintValidationRule 类:

       public class posintValidationRule : ValidationRule
{
    public override ValidationResult Validate(object value, System.Globalization.CultureInfo cultureInfo)
    {
        string _strInt = value.ToString();
        int _int = -1;
        if (!Int32.TryParse(_strInt, out _int))
            return new ValidationResult(false, "Value must be an integer");
        if (_int < 0)
            return new ValidationResult(false, "Value must be positive");
        return new ValidationResult(true, null);
    }
}

在 XAML 中还有一个样式错误模板:

   <Setter Property="Validation.ErrorTemplate">
     <Setter.Value>
        <ControlTemplate>
          <StackPanel>
            <Border BorderBrush="Red" BorderThickness="1" >
               <AdornedElementPlaceholder></AdornedElementPlaceholder>
            </Border>
            <TextBlock Text="there is an error"></TextBlock>
           </StackPanel>
         </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>

发生验证错误时,来自posintValidationRule 类的ValidationResult 的文本出现在工具提示中(“值必须是整数”等)。

我如何才能在 Validation.ErrorTemplate 的 TextBlock 中显示相同的文本,如果出现错误,现在只会说:“有错误”?

【问题讨论】:

    标签: wpf validation xaml data-binding textblock


    【解决方案1】:

    我找到了解决办法:

    <Style TargetType="{x:Type TextBox}">
           <Setter Property="Validation.ErrorTemplate">
            <Setter.Value>
                <ControlTemplate>
                  <StackPanel>
                    <Border BorderBrush="Red" BorderThickness="1" Margin="5,0,5,0" >
                        <AdornedElementPlaceholder Name="MyAdorner" ></AdornedElementPlaceholder>
                    </Border>
                    <TextBlock
                          Margin="5,0,0,0"
                          Foreground="Red" 
                          Text="{Binding ElementName=MyAdorner, Path=AdornedElement.(Validation.Errors)[0].ErrorContent}">
                   </TextBlock>
                 </StackPanel>
               </ControlTemplate>
           </Setter.Value>
         </Setter>
    </Style>
    

    一切正常

    【讨论】:

      【解决方案2】:

      DataContext 是 (Validation.Errors),所以你可以这样做:

      <Style TargetType="{x:Type TextBox}">
         <Setter Property="Validation.ErrorTemplate">
          <Setter.Value>
              <ControlTemplate>
                <StackPanel>
                  <Border BorderBrush="Red" BorderThickness="1" Margin="5,0,5,0" >
                      <AdornedElementPlaceholder Name="MyAdorner" ></AdornedElementPlaceholder>
                  </Border>
                  <TextBlock
                        Margin="5,0,0,0"
                        Foreground="Red" 
                        Text="{Binding [0].ErrorContent}">
                 </TextBlock>
               </StackPanel>
             </ControlTemplate>
         </Setter.Value>
       </Setter>
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2017-06-23
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2019-03-14
        • 1970-01-01
        • 2014-10-08
        相关资源
        最近更新 更多