Wpf clear disabled TextBox.Text when there are validation error
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 的 属性,我提出 属性changed on textboxes' lost focus command。刷新这些属性后,我清除禁用文本框 (TextProperty1-3) 的绑定属性。
但是,当某些禁用的文本框出现验证错误时,源 属性 会被清除,但 textbox.text 不会。
我如何在 mvvm 中解决这个问题?我不想设置 textbox.text.
希望问题清楚。
感谢您提供任何帮助或其他解决方案。
我用派生的文本框解决了这个问题class。
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;
}
}
我有三个文本框,其中文本绑定到三个属性。 当我输入第三个文本框时,我需要禁用两个文本框。而且我必须清除禁用文本框的值。
`
<TextBox Text="{Binding TextProperty1}" IsEnabled="{Binding T1Enabled}"/>
<TextBox Text="{Binding TextProperty2}" IsEnabled="{Binding T2Enabled}"/>
<TextBox Text="{Binding TextProperty3}" IsEnabled="{Binding T3Enabled}"/>
`
T1-3Enabled 是一个只有 getter 的 属性,我提出 属性changed on textboxes' lost focus command。刷新这些属性后,我清除禁用文本框 (TextProperty1-3) 的绑定属性。
但是,当某些禁用的文本框出现验证错误时,源 属性 会被清除,但 textbox.text 不会。
我如何在 mvvm 中解决这个问题?我不想设置 textbox.text.
希望问题清楚。 感谢您提供任何帮助或其他解决方案。
我用派生的文本框解决了这个问题class。
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;
}
}