WPF TextBox触发器来清除文本

前端之家收集整理的这篇文章主要介绍了WPF TextBox触发器来清除文本前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我有很多TextBox控件,我试图编写一个样式,当禁用控件时清除Text属性.
我不想在代码背后有事件处理程序.

我写道:

  1. <Style TargetType="{x:Type TextBox}">
  2. <Style.Triggers>
  3. <Trigger Property="IsEnabled" Value="False">
  4. <Setter Property="Text" Value="{x:Null}" />
  5. </Trigger>
  6. </Style.Triggers>
  7. </Style>

问题是如果TextBox定义如下:

  1. <TextBox Text={Binding Whatever} />

那么触发器不起作用(可能是因为它被绑定)
如何克服这个问题?

解决方法

因为您明确地设置TextBox中的文本,样式的触发器不能覆盖它.尝试这个:
  1. <TextBox>
  2. <TextBox.Style>
  3. <Style TargetType="{x:Type TextBox}">
  4. <Setter Property="Text" Value="{Binding Whatever}" />
  5.  
  6. <Style.Triggers>
  7. <Trigger Property="IsEnabled"
  8. Value="False">
  9. <Setter Property="Text" Value="{x:Null}" />
  10. </Trigger>
  11. </Style.Triggers>
  12. </Style>
  13. <TextBox.Style>
  14. </TextBox>

猜你在找的HTML相关文章