WPF TextBox 触发器以清除文本

编程入门 行业动态 更新时间:2024-10-28 06:34:49
本文介绍了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:54:22,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1562762.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:触发器   文本   WPF   TextBox

发布评论

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

>www.elefans.com

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