TextBox 触发器使用样式清除文本
首先,我已经使用 WPF 大约一周了。我想设置一个文本框的样式,以便在禁用它时将其清除。 这篇文章解释了如何做到这一点,但是我对如何做感到困惑将通用样式设置为资源,以便每个 TextBox 都可以绑定到不同的属性,而无需为每个 TextBox 重复样式。
<Window.Resources>
<Style TargetType="{x:Type TextBox}" x:Key="style1">
<Setter Property="Text" Value="{What do I really put here?}" />
<Style.Triggers>
<Trigger Property="IsEnabled" Value="False">
<Setter Property="Text" Value="{x:Null}" />
</Trigger>
</Style.Triggers>
</Style>
</Window.Resources>
....
<TextBox Style="{StaticResource style1}" Text="{Binding SomeProperty}"/>
谢谢!
First, let me say I've been working with WPF for about a week. I want to style a TextBox so that when it is disable, it is cleared. This article explained how to do it, however I'm confused on how to set the generic style as a resource so that every TextBox can bind to a different property without repeating the style for each TextBox.
<Window.Resources>
<Style TargetType="{x:Type TextBox}" x:Key="style1">
<Setter Property="Text" Value="{What do I really put here?}" />
<Style.Triggers>
<Trigger Property="IsEnabled" Value="False">
<Setter Property="Text" Value="{x:Null}" />
</Trigger>
</Style.Triggers>
</Style>
</Window.Resources>
....
<TextBox Style="{StaticResource style1}" Text="{Binding SomeProperty}"/>
Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您将无法像这样使用
Text
属性。在具有该样式的任何TextBox
上显式设置Text
属性将覆盖触发器中的Text
设置器(就像您注意到的那样)。如果您只需要清除
TextBox
而不是它所绑定的属性,则解决方法是为您要清除的文本使用附加属性(或Tag
)。将Text
绑定到Style
中。例如..
然后
TextBox
可以使用这个Style
就像You won't be able to use the
Text
property like that. Setting theText
property explicitly on anyTextBox
that has that style will override theText
setter in the trigger (like you noticed).If you only need the
TextBox
to be cleared and not the property it is binding to, then a workaround is to use an attached property (orTag
) for the text which you bindText
to in theStyle
.Example..
Then a
TextBox
can use thisStyle
like