繁体   English   中英

Wpf 在出现验证错误时清除禁用的 TextBox.Text

[英]Wpf clear disabled TextBox.Text when there is validation error

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

`

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

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

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

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM