ASP
ASP

Reputation: 854

Apply IDataErrorInfo validation errors to a control Errors inside a usercontrol child element

I have a usercontrol that contains simply a label and a TextBox. I would like to validate the textbox entry inside my viewmodel using the IDataErrorInfo interface. The validation kicks in, BUT the error is not shown on the TextBox inside my usercontrol but on the usercontrol itself.

This is the usercontrol (the other dependency properties are in the base class ):

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

This is the code behind:

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

And this is how it's (supposed) to be used:

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

My question is: How can I "forward" the validation error to the TextBox? Currently, validation errors are shown like this, but and I don't want the whole usercontrol to be framed.

Only the TextBox should be marked

Thank you!

Upvotes: 0

Views: 951

Answers (2)

ASP
ASP

Reputation: 854

Thanks to dotsvens linked thread, I was able to solve my problem, but a little differently.

  1. I set a global style for my usercontrol to disable the default Validation.ErrorTemplate

    <Style TargetType="{x:Type cc:LabelableTextBox}">
        <Setter Property="Validation.ErrorTemplate" Value="{x:Null}" />
    </Style>
    
  2. I implemented the IDataErrorInfo interface in my usercontrols code-behind:

    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. Finally, setting ValidatesOnDataErrors on my TextBoxinside my usercontrol to true.

Thats it! Simple, and it's working. If anybody sees some drawbacks, please let me know.

Upvotes: 0

dotsven
dotsven

Reputation: 326

Have a look at this thread: Validation Error Templates For UserControl

They solve it by using ErrorTemplates and triggers.

Upvotes: 1

Related Questions