带有ValidatesOnDataErrors = true的DataGridTextColumn需要添加两次值

时间:2014-01-20 09:54:42

标签: wpf wpfdatagrid idataerrorinfo

我在WPF DataGrid中有一个列绑定到具有以下成员的对象:

/// <summary>
/// Gets or sets the Value property. This observable property 
/// indicates ....
/// </summary>
public double Value
{
    get { return _scalarData.Value; }
    set
    {
        if (_scalarData.Value.EqualsWithPrecision(value)) return;

        _scalarData.Value = value;
        RaisePropertyChanged(() => Value);
    }
}

public string this[string columnName]
{
    get
    {
        if (columnName == "Value")
        {
            if (Value < _scalarElement.MinValue || Value > _scalarElement.MaxValue)
            {
                return "Value must be between the min and max value";
            }
        }

        return null;
    }
}

该列是在Codebehind中动态生成的(出于与应用程序细节相关的其他原因):

else if (e.PropertyType == typeof(ScalarDataEntryViewModel))
{
    column = new DataGridTextColumn
    {
        Binding = new Binding(e.PropertyName + "." + "Value")
        {
            Mode = BindingMode.TwoWay,
            UpdateSourceTrigger = UpdateSourceTrigger.PropertyChanged,
            ValidatesOnDataErrors = true,
            Converter = new DoubleToPersistantStringConverter()
        }
    };
}

我很确定只要你使用DataGridTextColumn且ValidatesOnDataErrors设置为true,就会出现问题。

如果以无效值开头(可能是因为它是新行或其他原因),网格会正确突出显示包含错误的单元格。问题是,当您输入单元格并更新该值时,当焦点离开该行时,该值将恢复为其原始(无效)值。如果您重新进入单元格并重新更改该值,它可以正常工作。但是对于每个无效单元格,您必须输入两次新值。

如果我注释掉“this [string columnName]”的错误检查或设置ValidatesOnErrors = false,问题就会消失,但我没有得到突出显示的单元格。

我可以使用自定义模板(TextBlock和TextBox)作为解决方法,但这带来了它自己的怪癖,所以我宁愿让内置模板正常工作。

如何按预期生成此功能,以便我只需输入一次值?

0 个答案:

没有答案