wpfc#-4.0idataerrorinfo

C# WPF IDataErrorInfo indexer is never called


I'm trying to implement simple validation with IDataErrorInfo and got problem - IDataErrorInfo indexer is never called. I m validating TextBox for numeric value with specified range, Type validation works correct but range validation doesn't work. C# code:

public interface IDataErrorInfo
{
    string Error { get; }
    string this[string columnName] { get; }
}

public class Bounds:IDataErrorInfo
{
    int lbw = 0;

    public int LBW
    {
        get { return lbw; }
        set { lbw = value; }
    }


    public string this[string columnname]
    {
        get
        {
            string error = String.Empty;
            switch(columnname)
            {
                case "LBW":
                    if (0 >= lbw || 500 < lbw)
                    {
                        error = "out of range";
                    }
                    break;
            }
            return error;
        }
    }
    public string Error
    {
        get { return null; }
    }
}

public partial class MainWindow : Window
{
    public MainWindow()
    {
        InitializeComponent();
        Bounds bounds = new Bounds(rolletBoundLbwBox);
        rolletBoundGrid.DataContext = bounds;

    }
}

xaml code:

<Grid x:Name="rolletBoundGrid" Background="#FFE5E5E5">
        <TextBox x:Name="rolletBoundLbwBox" HorizontalAlignment="Left" Height="26" TextWrapping="Wrap" VerticalAlignment="Top" Width="120" Margin="164,10,0,0"  Text="{Binding LBW, NotifyOnValidationError=True, ValidatesOnDataErrors=True, UpdateSourceTrigger=PropertyChanged}"/>
 </Grid>

Do you see the reason for the problem?


Solution

  • Are you implementing the built-in System.ComponentModel.IDataErrorInfo interface?

    public class Bounds: System.ComponentModel.IDataErrorInfo
    ...
    

    It doesn't work if you implement your own custom interface that happens to be named "IDataErrorInfo".