2009-07-14 78 views

回答

6

几分钟前我正在看同一个样本。你的猜测是正确的。在这个代码示例中,他们从TextBox控件中删除了默认的ErrorTemplate,所以它不会显示红色的矩形。他们不使用ErrorTemplate而是使用绑定到特定文本框验证错误的内容创建ContentProvider。

0

从我猜测那是Validation.ErrorTemplate =“{x:Null}”在出现错误时删除红色矩形。这可能被设置为能够在表单启动时在文本框周围没有红色矩形。

对于显示在表单中的错误,我在代码中看到了某处:Content="{Binding ElementName=lastNameTxt, Path=(Validation.Errors).CurrentItem}",所以我仍然猜测它是绑定到文本框(而不是数据模型)并检查它是否有静态验证错误。可能从数据模型连接到IDataErroInfo的错误?

我猜对了吗?

16

当您绑定到支持IDataErrorInfo的对象,也有WPF绑定类的几个特点来考虑:

  1. ValidatesOnDataErrors必须是真实的。这指示WPF查找并使用底层对象上的IDataError接口。

  2. 如果源对象的IDataError接口报告验证问题,则附加属性Validation.HasError将在目标对象上设置为true。然后,您可以使用此属性与触发器来更改控件的工具提示以显示验证错误消息(我在我当前的项目中执行此操作,并且最终用户喜欢它)。

  3. Validation.Errors附加属性将包含上次验证尝试导致的任何ValidationResult错误的枚举。如果您要使用工具提示方法,请使用IValueConverter仅检索第一个项目,否则会遇到绑定错误以显示错误消息本身。

  4. 绑定类公开NotifyOnValidationError,当True时,每当验证规则的状态发生更改时,都会导致路由事件从绑定控件中冒出来。如果要在绑定控件的容器中实现事件处理程序,然后将验证消息添加到列表框或从列表框中移除验证消息,这很有用。

有这样做的反馈两个样式(工具提示以及列表框)在MSDN上的样品,但我会贴上我乐雷来实现我的DataGridCells和文本框工具提示反馈的代码如下.. 。

的DataGridCell样式:

<Style TargetType="{x:Type dg:DataGridCell}" 
      x:Key="DataGridCellStyle"> 

     <Setter Property="ToolTip" 
       Value="{Binding Path=Column.(ToolTipService.ToolTip),RelativeSource={RelativeSource Self}}" /> 

     <Style.Triggers> 
     <Trigger Property="Validation.HasError" 
       Value="True"> 
      <Setter Property="ToolTip" 
        Value="{Binding RelativeSource={RelativeSource Self},Path=(Validation.Errors), Converter={StaticResource ErrorContentConverter}}" /> 
     </Trigger> 
     </Style.Triggers> 

    </Style> 

文本框样式:

 <Style x:Key="ValidatableTextBoxStyle" TargetType="TextBox"> 
    <!--When the control is not in error, set the tooltip to match the AutomationProperties.HelpText attached property--> 
    <Setter Property="ToolTip" 
      Value="{Binding RelativeSource={RelativeSource Mode=Self},Path=(AutomationProperties.HelpText)}" /> 

      <Style.Triggers> 
      <Trigger Property="Validation.HasError" Value="true"> 
       <Setter Property="ToolTip" 
         Value="{Binding RelativeSource={x:Static RelativeSource.Self},Path=(Validation.Errors)[0].ErrorContent}" /> 
      </Trigger> 
      </Style.Triggers> 
     </Style> 

的ErrorContentConverter(用于检索工具提示所述第一验证错误消息):

Imports System.Collections.ObjectModel 

Namespace Converters 

    <ValueConversion(GetType(ReadOnlyObservableCollection(Of ValidationError)), GetType(String))> _ 
    Public Class ErrorContentConverter 
     Implements IValueConverter 

     Public Function Convert(ByVal value As Object, ByVal targetType As System.Type, ByVal parameter As Object, ByVal culture As System.Globalization.CultureInfo) As Object Implements System.Windows.Data.IValueConverter.Convert 
      Dim errors As ReadOnlyObservableCollection(Of ValidationError) = TryCast(value, ReadOnlyObservableCollection(Of ValidationError)) 
      If errors IsNot Nothing Then 
       If errors.Count > 0 Then 
        Return errors(0).ErrorContent 
       End If 
      End If 
      Return String.Empty 
     End Function 

     Public Function ConvertBack(ByVal value As Object, ByVal targetType As System.Type, ByVal parameter As Object, ByVal culture As System.Globalization.CultureInfo) As Object Implements System.Windows.Data.IValueConverter.ConvertBack 
      Throw New NotImplementedException() 
     End Function 

    End Class 

End Namespace 

.. 。最后是在文本框中使用样式的示例:

<TextBox Text="{Binding Path=EstimatedUnits,ValidatesOnDataErrors=True,NotifyOnValidationError=True}" 
      Style="{StaticResource ValidatableTextBoxStyle}" 
      AutomationProperties.HelpText="The number of units which are likely to sell in 1 year." /> 
0

这里是我用于在工具提示中显示错误的代码或控件旁边的小气泡。

  1. 定义样式。

    <Style x:Key="TextBoxValidationStyle" TargetType="{x:Type TextBox}"> 
        <Style.Triggers> 
         <Trigger Property="Validation.HasError" Value="true"> 
          <Setter Property="ToolTip" Value="{Binding RelativeSource={RelativeSource Self}, Path=(Validation.Errors)[0].ErrorContent}"/> 
         </Trigger> 
        </Style.Triggers> 
    </Style> 
    
    <Style x:Key="TextboxErrorBubbleStyle" TargetType="{x:Type TextBox}" BasedOn="{StaticResource ResourceKey=TextBoxValidationStyle}"> 
        <Setter Property="Validation.ErrorTemplate"> 
         <Setter.Value> 
          <ControlTemplate> 
           <DockPanel LastChildFill="true"> 
    
            <Border Background="Red" DockPanel.Dock="right" Margin="5,0,0,0" Width="10" Height="10" CornerRadius="10" 
             ToolTip="{Binding ElementName=customAdorner, Path=AdornedElement.(Validation.Errors)[0].ErrorContent}"> 
    
             <TextBlock Text="!" VerticalAlignment="center" HorizontalAlignment="center" FontWeight="Bold" Foreground="white"/> 
            </Border> 
    
            <AdornedElementPlaceholder Name="customAdorner" VerticalAlignment="Center" > 
             <Border BorderBrush="red" BorderThickness="1" /> 
            </AdornedElementPlaceholder> 
    
           </DockPanel> 
          </ControlTemplate> 
         </Setter.Value> 
        </Setter> 
    </Style> 
    

  2. 与控制使用。

    <TextBox Text="{Binding Path=FirstName, Mode=TwoWay, ValidatesOnDataErrors=True,NotifyOnValidationError=True, UpdateSourceTrigger=PropertyChanged}" 
          Style="{StaticResource TextBoxValidationStyle}" Width="100" Margin="3 5 3 5"/> 
    <TextBox Text="{Binding Path=LastName, Mode=TwoWay, ValidatesOnDataErrors=True,NotifyOnValidationError=True, UpdateSourceTrigger=PropertyChanged}" 
          Style="{StaticResource TextboxErrorBubbleStyle}" Width="100" Margin="0 5 3 5"/> 
    

  3. 样品模型类。

    class Customer:INotifyPropertyChanged,IDataErrorInfo { private string firstName; 私人字符串姓氏;

    public string FirstName 
    { 
        get { return firstName; } 
        set 
        { 
         if (firstName != value) 
         { 
          firstName = value; 
          RaisePropertyChanged("FirstName"); 
         } 
        } 
    } 
    
    public string LastName 
    { 
        get { return lastName; } 
        set 
        { 
         if (lastName != value) 
         { 
          lastName = value; 
          RaisePropertyChanged("LastName"); 
         } 
        } 
    } 
    public string Error 
    { 
        get { throw new System.NotImplementedException(); } 
    } 
    
    public string this[string columnName] 
    { 
        get 
        { 
         string message = null; 
         if (columnName == "FirstName" && string.IsNullOrEmpty(FirstName)) 
         { 
          message = "Please enter FirstName"; 
         } 
         if (columnName == "LastName" && string.IsNullOrEmpty(LastName)) 
         { 
          message = "Please enter LastName"; 
         } 
         return message; 
        } 
    } 
    

    }

  4. UI的外观和感觉。

enter image description here