Validation with IDataErrorInfo through DependencyProperty

338 Views Asked by At

I've created a small user control to extend my fun with validation! The problem is that the validation does not work: IDataErrorInfo.this[] is never called.

Thank you in advance for your help. Stephan

Here is my code:

<UserControl x:Class="WpfApplication5.TextBoxWithValidation"
         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
         x:Name="TextBoxWithValidationControl">
<Grid>
    <StackPanel Orientation="Horizontal">
        <TextBox Height="23" Width="120"
                 Text="{Binding Path=Text,ElementName=TextBoxWithValidationControl, ValidatesOnDataErrors=True, UpdateSourceTrigger=PropertyChanged}">
        </TextBox>
    </StackPanel>
</Grid>

And the code behind is:

public partial class TextBoxWithValidation
{
    public static readonly DependencyProperty TextProperty = DependencyProperty.Register("Text",
                                                                                         typeof (object),
                                                                                         typeof (
                                                                                                 TextBoxWithValidation
                                                                                                 ),
                                                                                         new FrameworkPropertyMetadata
                                                                                                 (default(object),
                                                                                                  FrameworkPropertyMetadataOptions
                                                                                                          .BindsTwoWayByDefault));

    public TextBoxWithValidation()
    {
        InitializeComponent();
    }

    public object Text
    {
        get { return GetValue(TextProperty); }
        set { SetValue(TextProperty, value); }
    }
}

Now I'm using this user control as such:

<local:TextBoxWithValidation Text="{Binding Path=Dummy.MyProperty}" />

And my Dummy entity is defined by:

public class Dummy : INotifyPropertyChanged, IDataErrorInfo
{
    private string _myProperty;

    public string MyProperty
    {
        get { return _myProperty; }
        set
        {
            _myProperty = value;
            OnPropertyChanged("MyProperty");
        }
    }

    #region IDataErrorInfo Members

    public string this[string columnName]
    {
        get { return "This is an error"; }
    }

    public string Error { get; private set; }

    #endregion

    #region INotifyPropertyChanged Members

    public event PropertyChangedEventHandler PropertyChanged;

    #endregion

    protected virtual void OnPropertyChanged(string propertyName)
    {
        PropertyChangedEventHandler handler = PropertyChanged;
        if (handler != null)
            handler(this, new PropertyChangedEventArgs(propertyName));
    }
}
0

There are 0 best solutions below