WPF TextBox触发器清除文本 [英] WPF TextBox trigger to clear Text
本文介绍了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触发器清除文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文