【问题标题】:Wpf clear disabled TextBox.Text when there is validation error出现验证错误时,Wpf clear disabled TextBox.Text
【发布时间】:2018-08-30 12:42:43
【问题描述】:

我有三个文本框,其中的文本绑定到三个属性。 当我输入第三个文本框时,我需要禁用两个文本框。我必须清除禁用文本框的值。

`

 <TextBox Text="{Binding TextProperty1}"  IsEnabled="{Binding T1Enabled}"/>
 <TextBox Text="{Binding TextProperty2}"  IsEnabled="{Binding T2Enabled}"/>
 <TextBox Text="{Binding TextProperty3}"  IsEnabled="{Binding T3Enabled}"/>

`

T1-3Enabled 是一个只有 getter 的属性,我在文本框的失去焦点命令上提​​出 propertychanged。当这些属性刷新时,我清除禁用文本框(TextProperty1-3)的绑定属性。

但是,当某些禁用的文本框出现验证错误时,源属性会被清除,但 textbox.text 不会。

如何在 mvvm 中解决这个问题?我不想设置 textbox.text。

我希望问题很清楚。 感谢您提供任何帮助或其他解决方案。

【问题讨论】:

  • 你如何验证它们?更重要的是...验证错误是什么意思?

标签: wpf mvvm validationerror


【解决方案1】:

我用派生的文本框类解决了这个问题。

public class MyTextBox : TextBox
{
    public MyTextBox()
    {
        IsEnabledChanged += MyTextBox_IsEnabledChanged;
    }

    private void MyTextBox_IsEnabledChanged(object sender, DependencyPropertyChangedEventArgs e)
    {
        if(e.NewValue is bool)
            if (!(bool)e.NewValue)
                Text = string.Empty;
    }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-03-19
    • 2013-10-14
    • 1970-01-01
    • 1970-01-01
    • 2013-02-18
    • 2017-11-19
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多