【问题标题】:C# WPF IDataErrorInfo indexer is never calledC# WPF IDataErrorInfo 索引器永远不会被调用
【发布时间】:2017-09-22 16:13:03
【问题描述】:

我正在尝试使用 IDataErrorInfo 实现简单的验证,但遇到了问题 - 从未调用过 IDataErrorInfo 索引器。我正在验证 TextBox 是否具有指定范围的数值,类型验证正确,但范围验证不起作用。 C# 代码:

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 代码:

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

你看到问题的原因了吗?

【问题讨论】:

    标签: wpf c#-4.0 idataerrorinfo


    【解决方案1】:

    您是否正在实现 内置 System.ComponentModel.IDataErrorInfo 接口?

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

    如果您实现自己的自定义接口(恰好名为“IDataErrorInfo”),则它不起作用。

    【讨论】:

      猜你喜欢
      • 2023-03-13
      • 2012-10-27
      • 2013-10-12
      • 2012-03-27
      • 2013-08-29
      • 2013-11-03
      • 1970-01-01
      • 1970-01-01
      • 2015-05-06
      相关资源
      最近更新 更多