如何在单独的文本块中显示WPF ValidationResult?

如何在单独的文本块中显示WPF ValidationResult?,wpf,validation,xaml,data-binding,textblock,Wpf,Validation,Xaml,Data Binding,Textblock,我试图在WPF中使用带有验证规则的数据绑定控件的验证输入。我有一个posintValidationRule类: public class posintValidationRule : ValidationRule { public override ValidationResult Validate(object value, System.Globalization.CultureInfo cultureInfo) { string _strInt

我试图在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);
    }
}
公共类posintValidationRule:ValidationRule
{
公共覆盖验证结果验证(对象值,System.Globalization.CultureInfo CultureInfo)
{
字符串_strInt=value.ToString();
int _int=-1;
如果(!Int32.TryParse(\u strInt,out\u int))
返回新的ValidationResult(false,“值必须是整数”);
如果(_int<0)
返回新的ValidationResult(false,“值必须为正值”);
返回新的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中的文本块显示相同的文本呢?如果出现错误,它现在只会说:“有错误”

我找到了解决方案:

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


它工作正常

我找到了解决方案:

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

它工作正常

数据上下文是(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>

数据上下文是(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>