2014-02-05 156 views
0

我已经为基于TextBoxBase的控件修改了默认的Validation.ErrorTemplate。 为了让我有工具提示和标签弹出,显示输入字段获得焦点时的错误。基于控件状态设置Validation.ErrorTemplate

我想删除默认的红色边框,当控件没有文字,但需要输入,并用控件左侧的星号*代替红色边框。

我认为,如果你有很多必填字段,那么使用会遇到一个充满了可怕的红色框的表格。只有当你输入一个非有效的值,如年龄等于223时,才会显示红色边框。

所以我想我想根据触发器切换模板或模板的一部分。

回答

0

恕我直言,你不能在TextBox控件上使用StyleSelector。这是我的错。相反,您可以使用简单的转换器来切换TextBox样式。

下面是它的示例代码:

型号:

public class User : ModelBase 
{ 
    private string _login; 

    [Required(ErrorMessage = "Login can not be empty")] 
    [MaxLength(20, ErrorMessage = "Login max lenght is 20")] 
    public string Login 
    { 
     get 
     { 
      return _login; 
     } 

     set 
     { 
      _login = value; 
      OnPropertyChanged("Login"); 
     } 
    } 
} 

创建在App.xaml中自定义错误模板文本框的样式。

的App.xaml

<Application.Resources> 
    <Style x:Key="AsteriskErrorStyle" 
      TargetType="{x:Type TextBox}"> 

     <Setter Property="Validation.ErrorTemplate"> 
      <Setter.Value> 
       <ControlTemplate> 
        <StackPanel Orientation="Horizontal"> 
         <AdornedElementPlaceholder x:Name="AdornedElementPlaceholder" /> 

         <TextBlock Foreground="Red" 
            Margin="10,0,0,0" 
            VerticalAlignment="Top" 
            FontSize="20" 
            Text="*" 
            ToolTip="{Binding ElementName=AdornedElementPlaceholder, Path=AdornedElement.(Validation.Errors)[0].ErrorContent}"> 
         </TextBlock> 
        </StackPanel> 
       </ControlTemplate> 
      </Setter.Value> 
     </Setter> 
    </Style> 

    <Style x:Key="RedBorderErrorStyle" 
      TargetType="{x:Type TextBox}"> 

     <Setter Property="Validation.ErrorTemplate"> 
      <Setter.Value> 
       <ControlTemplate> 
        <Border BorderThickness="1.5" 
          BorderBrush="Red"> 
         <AdornedElementPlaceholder x:Name="AdornedElementPlaceholder" /> 
        </Border> 
       </ControlTemplate> 
      </Setter.Value> 
     </Setter> 
    </Style> 
</Application.Resources> 

转换器:

public class TextBoxStyleConverter : IValueConverter 
{ 
    public object Convert(object value, Type targetType, object parameter, CultureInfo culture) 
    { 
     string textBoxTex = value.ToString(); 

     var asteriskErrorStyle = Application.Current.FindResource("AsteriskErrorStyle") as Style; 
     var redBorderErrorStyle = Application.Current.FindResource("RedBorderErrorStyle") as Style; 

     if (string.IsNullOrEmpty(textBoxTex)) 
     { 
      return asteriskErrorStyle; 
     } 
     else 
     { 
      return redBorderErrorStyle; 
     } 
    } 

    public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture) 
    { 
     throw new NotImplementedException(); 
    } 
} 

用途:

<Window.Resources> 
    <styleSelector:TextBoxStyleConverter x:Key="TextBoxStyleConverter"/> 
</Window.Resources> 

<TextBox Grid.Column="1" 
     Grid.Row="0" 
     Margin="5,5,80,5" 
     Style="{Binding RelativeSource={RelativeSource Self}, Path=Text,Converter={StaticResource TextBoxStyleConverter}}" 
     Text="{Binding Path=User.Login, ValidatesOnNotifyDataErrors=True}" > 

样的项目,你可以下载here

+0

感谢一个代码示例会很好。 –

0

您可以将Binding.ValidatesOnExceptionsBinding.ValidatesOnDataErrors属性设置为False以停止显示默认红色Border。你可以使用Validation.ErrorTemplate属性应用于你的星号,你似乎已经发现:

<ControlTemplate x:Key="RedAsteriskValidationTemplate"> 
    <StackPanel Orientation="Horizontal"> 
     <TextBlock Text="*" Foreground="Red" FontWeight="Bold" Margin="0,0,5,0" /> 
     <AdornedElementPlaceholder /> 
    </StackPanel> 
</ControlTemplate> 

你可以找到更多的例子,包括Validation.ErrorTemplate property页面上MSDN上使用Trigger之一。

+0

当然,我可以这样做,但我想用我的验证规则来驱动UI。对于同一个属性,我可以有一个不空的规则和一个范围规则。非空应显示星号,范围规则应显示边框和带有错误消息的小弹出窗口。 –