【问题标题】:Apply IDataErrorInfo validation errors to a control Errors inside a usercontrol child element将 IDataErrorInfo 验证错误应用于用户控件子元素内的控件错误
【发布时间】:2015-10-12 14:02:36
【问题描述】:

我有一个只包含一个标签和一个文本框的用户控件。我想使用 IDataErrorInfo 接口验证我的视图模型中的文本框条目。验证开始,但错误未显示在我的用户控件内的 TextBox 上,而是显示在用户控件本身上。

这是用户控件(其他依赖属性在基类中):

<cc:LabelableInputControl>

<Grid x:Name="LayoutRoot" >
    <Grid.ColumnDefinitions>
        <ColumnDefinition Width="Auto" />
        <ColumnDefinition Width="*" />
    </Grid.ColumnDefinitions>
    <Label Content="{Binding Path=LabelText, FallbackValue=Empty Label}" 
        Grid.Column="0"
        HorizontalAlignment="Left" 
        Name="BaseLabel" 
        VerticalAlignment="Center" 
        Width="{Binding Path=LabelWidth}"/>

    <TextBox Grid.Column="1" 
        x:Name="BaseTextBox"
        Text="{Binding Text" />
</Grid>
</cc:LabelableInputControl>

这是后面的代码:

public partial class LabelableTextBox : LabelableInputControl
{       
    [Bindable(true)]
    public string Text
    {
        get { return (string)GetValue(TextProperty); }
        set
        {
            SetValue(TextProperty, value);
        }
    }
    public static readonly DependencyProperty TextProperty = TextBox.TextProperty.AddOwner(typeof(LabelableTextBox));

    public LabelableTextBox()
    {
        InitializeComponent();

        LayoutRoot.DataContext = this;
    }
}

这就是它的(假定)使用方式:

<cc:LabelableTextBox LabelText="{x:Static text:Resources.Label_Username}"
        Text="{Binding Username, Mode=TwoWay,
        ValidatesOnDataErrors=True, NotifyOnValidationError=True}" />

我的问题是:如何将验证错误“转发”到 TextBox?目前,验证错误是这样显示的,但我不希望整个用户控件都被框起来。

谢谢!

【问题讨论】:

    标签: .net wpf wpf-controls


    【解决方案1】:

    看看这个帖子:Validation Error Templates For UserControl

    他们通过使用 ErrorTemplates 和触发器来解决它。

    【讨论】:

    • 太棒了!谢谢,这将我推向了正确的方向。
    【解决方案2】:

    感谢 dotsvens 链接线程,我能够解决我的问题,但有点不同。

    1. 我为我的用户控件设置了一个全局样式来禁用默认的Validation.ErrorTemplate

      <Style TargetType="{x:Type cc:LabelableTextBox}">
          <Setter Property="Validation.ErrorTemplate" Value="{x:Null}" />
      </Style>
      
    2. 我在我的用户控件代码隐藏中实现了IDataErrorInfo 接口:

      public partial class LabelableTextBox : LabelableInputControl, IDataErrorInfo
      {       
          [Bindable(true)]
          public string Text
          {
              get { return (string)GetValue(TextProperty); }
              set
              {
                   SetValue(TextProperty, value);
              }
          }
          public static readonly DependencyProperty TextProperty = TextBox.TextProperty.AddOwner(typeof(LabelableTextBox));
      
          public LabelableTextBox()
          {
              InitializeComponent();
              LayoutRoot.DataContext = this;
          }
      }
      public string Error
      {
          get { throw new System.NotImplementedException(); }
      }
      
      public string this[string columnName]
      {
          get
          {
              string result = null;
      
              var errors = Validation.GetErrors(this);
      
              if (errors.Count > 0)
              {
                  result = errors[0].ErrorContent.ToString();
              }
      
              return result;
          }
      }
      
    3. 最后,将我的TextBoxinside 我的用户控件中的ValidatesOnDataErrors 设置为true

    就是这样!很简单,而且很有效。如果有人看到一些缺点,请告诉我。

    【讨论】:

      猜你喜欢
      • 2020-06-27
      • 1970-01-01
      • 1970-01-01
      • 2015-12-01
      • 1970-01-01
      • 2019-08-22
      • 2020-03-08
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多