开发者

How to show WPF ValidationResult in a seperate textblock?

I am trying to use in WPF a validating input of databound controls with validation rules. I have a posintValidationRule class:

       public class p开发者_高级运维osintValidationRule : 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);
    }
}

In XAML there is also a style error template:

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

When validation error occurs, the text of ValidationResult from posintValidationRule class appears in a tooltip ("Value must be an integer" and the like).

How I could have this the same text shown in a TextBlock from a Validation.ErrorTemplate which now, in case of an error, says just: "there is an error"?


I have found the solution:

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

It works OK


The DataContext is (Validation.Errors), so you could just do:

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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜