本文介绍了WPF TextBox触发器清除文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有很多 TextBox 控件,我正在尝试编写一种清除Text 属性何时的样式该控件被禁用。 我不想在代码中包含事件处理程序。
I have many TextBox controls and I'm trying to write a style that clears the Text property when the Control is disabled. I don't want to have Event Handlers in code behind.
我这样写:
<Style TargetType="{x:Type TextBox}"> <Style.Triggers> <Trigger Property="IsEnabled" Value="False"> <Setter Property="Text" Value="{x:Null}" /> </Trigger> </Style.Triggers> </Style>问题在于,如果TextBox的定义如下:
The problem is that if the TextBox is defined like:
<TextBox Text={Binding Whatever} />然后触发器不起作用(可能是因为它绑定了)如何克服此问题?
then the trigger does not work (probably because it's bound) How to overcome this problem?
推荐答案由于要在TextBox中显式设置Text,因此样式的触发器无法覆盖它。试试这个:
Because you're explicitly setting the Text in the TextBox, the style's trigger can't overwrite it. Try this:
<TextBox> <TextBox.Style> <Style TargetType="{x:Type TextBox}"> <Setter Property="Text" Value="{Binding Whatever}" /> <Style.Triggers> <Trigger Property="IsEnabled" Value="False"> <Setter Property="Text" Value="{x:Null}" /> </Trigger> </Style.Triggers> </Style> </TextBox.Style> </TextBox>更多推荐
WPF TextBox触发器清除文本
发布评论