WPF TextBox触发器清除文本

编程入门 行业动态 更新时间:2024-10-27 22:32:09
本文介绍了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触发器清除文本

本文发布于:2023-11-06 04:53:12,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1562758.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:触发器   文本   WPF   TextBox

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!