【问题标题】:why is textedit validation handler called twice?为什么 textedit 验证处理程序被调用两次?
【发布时间】:2011-08-08 09:38:45
【问题描述】:

这是一个示例代码:

ButtonEdit be = new ButtonEdit()
{
    DisplayFormatString = MyDisplayFrm,
    MaskType = MaskType.RegEx,
    Mask = "[-+]?([0-9]*[,.])?[0-9]+([eE][-+]?[0-9]+)?",
    ValidateOnTextInput = false
};
Binding bindingValue = new Binding() { Source = PropItem, Path = new PropertyPath("Value"), Mode = BindingMode.TwoWay };
BindingOperations.SetBinding(be, ButtonEdit.EditValueProperty, bindingValue);
be.SetValue(Grid.ColumnProperty, 0);
be.Validate += be_Validate;

void be_Validate(object sender, ValidationEventArgs e)
{
    if ((Convert.ToDouble(e.Value) <= MaxVal) && (Convert.ToDouble(e.Value) >= MinVal)) return;
    MessageBoxResult mbr = MessageBox.Show("The value in not in the suggested range, do you want to continue?", "Min/Max Range validation", MessageBoxButton.YesNo);
    if (mbr == MessageBoxResult.Yes)
    {
        return;
    }
    else
    {
        e.IsValid = false;
        e.ErrorType = DevExpress.XtraEditors.DXErrorProvider.ErrorType.Warning;
        e.ErrorContent = "Value is not in the suggested range. Please correct.";
    }
}

当我将值更改为超出范围并更改焦点时,我会收到两次消息框;一个用于更改值,一个用于更改显示,因为编辑器将值(双倍)显示为适当的科学显示。

如何使 TextEdit(或上例中的 ButtonEdit)在更改显示时不检查验证?我的意思是它不应该放在首位,不是吗?由于 EditValue 属性没有改变,而只是显示(Text 属性)。

提前致谢:)

【问题讨论】:

    标签: c# wpf validation devexpress textedit


    【解决方案1】:

    请查看此帮助文章 BaseEdit.InvalidValue event

    AFAIK 您需要处理 Validating 事件并在该事件中设置 e.Cancel 如果值不正确,则为 true,然后在 InvalidValue 事件中您可以为无效条目提供视觉提示。

    希望对你有帮助

    【讨论】:

    • 请原谅这个答案,刚刚意识到您在谈论 WPF DXEditor ButtonEdit 而不是 XtraEditors.ButtonEdit。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-11-07
    • 2011-07-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-06-02
    • 1970-01-01
    相关资源
    最近更新 更多