WPF TextBox 触发器以清除文本 [英] WPF TextBox trigger to clear Text

查看:11
本文介绍了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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆