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!
You won’t be able to use the
Textproperty like that. Setting theTextproperty explicitly on anyTextBoxthat has that style will override theTextsetter in the trigger (like you noticed).If you only need the
TextBoxto 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 bindTextto in theStyle.Example..
Then a
TextBoxcan use thisStylelike