WPF TextBox trigger to clear Text - wpf

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.
I wrote this:
<Style TargetType="{x:Type TextBox}">
<Style.Triggers>
<Trigger Property="IsEnabled" Value="False">
<Setter Property="Text" Value="{x:Null}" />
</Trigger>
</Style.Triggers>
</Style>
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?

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>

Related

How to inherit custom Textblock style in wpf datagrid?

I have TextBlock in the DataGridTemplateColumn in WPF Datagrid. when I check "IsEnable" false to inherit the Textblock Style inside the DatagridTemplateColumn. Here is XAML code i'm using:
<Style TargetType="{x:Type DataGrid}" >
<Setter Property="Template" >
<Setter.Value>
<ControlTemplate TargetType="{x:Type DataGrid }">
<ControlTemplate.Resources >
<Style TargetType="{x:Type TextBlock }">
<Style.Triggers>
<Trigger Property="IsEnabled" Value="False">
<Setter Property="Foreground" Value="{StaticResource {x:Static SystemColors.GrayTextBrushKey}}"/>
</Trigger>
</Style.Triggers>
</Style>
</ControlTemplate.Resources>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
This did not work and later i tried:
<Style TargetType="TextBlock" >
<Style.Triggers>
<Trigger Property="IsEnabled" Value="False">
<Setter Property="Foreground" Value="{StaticResource {x:Static SystemColors.GrayTextBrushKey}}" />
</Trigger>
</Style.Triggers>
</Style>
Any Thoughts on how to check if the Texblock inside the Datagrid "IsEnabled" and inherit the Style?.
WPF does not apply implicit styles inside templates unless the TargetType derives from Control. Since TextBlock doesn't derive from Control, its style is not applied. So you either have to manually apply the style to every non-Control or define the implicit style inside the template.
Define your styles inside datagrid resources as
<DataGrid.Resources>
<Style TargetType="{x:Type TextBlock}">
<Style.Triggers>
<Trigger Property="IsEnabled" Value="False">
<Setter Property="Foreground" Value="Red"/>
</Trigger>
</Style.Triggers>
</Style>
</DataGrid.Resources>
I'm going to assume you are trying to toggle the foreground color based on the IsEnabled state of the TextBlock.
Where are you setting the IsEnabled = true Foreground color? You have not given the code for the actual TextBlock you are going to style.
Try this:
<Style TargetType="{x:Type TextBlock}">
<Style.Triggers>
<Trigger Property="IsEnabled" Value="True">
<Setter Property="Foreground" Value="Red" />
</Trigger>
<Trigger Property="IsEnabled" Value="False">
<Setter Property="Foreground" Value="Green" />
</Trigger>
</Style.Triggers>
</Style>
If this doesn't work, it means wherever your Textblock is defined, you are doing this -
<TextBlock .... Foreground="SomeColor" />
and you need to remove the Foreground setting directly on the TextBlock so that the Foreground color can be set by the style.

WPF - TextBox : trigger on text value is not working

My textbox :
<TextBox Grid.Column="1" TextWrapping="Wrap" Height="50">
<TextBox.Style>
<Style TargetType="TextBox">
<Setter Property="Foreground" Value="DarkGray"/>
<Setter Property="Text" Value="Must be seperated with coma"/>
<Style.Triggers>
<Trigger Property="IsFocused" Value="True">
<Setter Property="Foreground" Value="Red"/>
<Setter Property="Text" Value="{x:Null}"/>
</Trigger>
</Style.Triggers>
</Style>
</TextBox.Style>
</TextBox>
The problem is : when I put my mouse on the textbox, the foreground change in red, that's cool but the text doesn't clear up. Have you any idea of why ?
Thanks a lot !

WPF - Enable/Disable tabstop based on whether property has data

I need to set the tabstop of a textbox based on whether or not the bound property has data or not. The properties are nearly all strings - I want to disable the tabstop if the property is null or empty.
I am using a style for these textboxes.
Here is the style I am currently using:
<Style TargetType="TextBox" x:Key="FauxLabel">
<Setter Property="Background" Value="Transparent" />
<Setter Property="IsTabStop" Value="True" />
<Setter Property="IsReadOnly" Value="True" />
<!-- rest of setters truncated -->
</Style>
And here is an example of my usage:
<TextBox
Name="Account"
Style="{StaticResource ResourceKey=FauxLabel}"
Text="{Binding
Path=SelectedItem.AccountNumber,
ElementName=CrfResults}"/>
In this exmple, if the AccountNumber property is null or empty, I want to disable the tabstop. I am using Visual Studio 2010 and .Net 4.0. Can anyone help me out?
Update:
Thanks to Rachel for her answer. I was able to update the style to handle all textboxes using that style using by adding the trigger below which binds to the text property, rather than the underlying bound property:
<Style.Triggers>
<Trigger Property="Text" Value="{x:Null}">
<Setter Property="IsTabStop" Value="False">
</Setter>
</Trigger>
<Trigger Property="Text" Value="">
<Setter Property="IsTabStop" Value="False">
</Setter>
</Trigger>
</Style.Triggers>
Use a DataTrigger which checks if the value is {x:Null}
<Style TargetType="TextBox" x:Key="FauxLabel">
<Setter Property="IsTabStop" Value="True" />
<Style.Triggers>
<DataTrigger Property="{Binding ElementName=CrfResults, Path=SelectedItem.AccountNumber}" Value="{x:Null}">
<Setter Property="IsTabStop" Value="False" />
</DataTrigger>
</Style.Triggers>
</Style>
You could probably do it with a regular Trigger instead of a DataTrigger too

Overriding background color of textbox WPF

I want to change the background of a textbox using a style(in WPF).But text box is already based on a style.So when i give background it setter it is not overridding original style.Below is the code i am trying to work:
<TextBox text="value">
<TextBox.Style>
<Style TargetType="{x:Type TextBox}" BasedOn="{StaticResource {x:Type TextBox}}">
<Style.Triggers>
<MultiDataTrigger>
<MultiDataTrigger.Conditions>
<Condition Binding="{Binding Path=path}" Value="False"/>
</MultiDataTrigger.Conditions>
<MultiDataTrigger.Setters>
<Setter Property="ForeGround" Value="Red"/>
<Setter Property="Background" Value="LightGray"/>
</MultiDataTrigger.Setters>
</MultiDataTrigger>
</Style.Trigger>
</Style>
The code is chnaging the foreground to Red but there is no chnage in background of textbox.
How can i override the backgorund color.I need to do this in WPF.
This is most definitely not the code you are using, there is no ForeGround property and in theory (that error aside) this would work and the background would change if the trigger is fired.
Another simple example that works:
<CheckBox Name="cb" Content="Red BG"/>
<TextBox>
<TextBox.Style>
<Style TargetType="{x:Type TextBox}" BasedOn="{StaticResource {x:Type TextBox}}">
<Style.Triggers>
<DataTrigger Binding="{Binding IsChecked, ElementName=cb}" Value="True">
<Setter Property="Background" Value="Red"/>
</DataTrigger>
</Style.Triggers>
</Style>
</TextBox.Style>
</TextBox>
The following however would not work:
<CheckBox Name="cb" Content="Red BG"/>
<TextBox Background="Green">
<TextBox.Style>
<Style TargetType="{x:Type TextBox}" BasedOn="{StaticResource {x:Type TextBox}}">
<Style.Triggers>
<DataTrigger Binding="{Binding IsChecked, ElementName=cb}" Value="True">
<Setter Property="Background" Value="Red"/>
</DataTrigger>
</Style.Triggers>
</Style>
</TextBox.Style>
</TextBox>
Note that the background was set to green first in the declaration, this direct value will override the style due to dependency property value precedence, if you want to set an initial value this needs to be done in a setter in the style (outside of any trigger). I suspect that this might be your issue.
just put the setter statement after the style statement
<Setter Property="Background" Value="red"/>
like..
<Style TargetType="{x:Type TextBox}" BasedOn="{StaticResource {x:Type TextBox}}">
<Setter Property="Background" Value="red"/>
<Style.Triggers>.........

wpf: add style to ComboBoxItem trough binding

I've a ComboBox and a Style for the items. The style is defined by using Style.Triggers in this way:
<Style>
<Style.Triggers>
<Trigger Property="Tag" Value="false">
<Setter Property="Background" Value="LightBlue"/>
<Setter Property="Foreground" Value="BlueViolet"/>
</Trigger>
<Trigger Property="Tag" Value="true">
<Setter Property="Background" Value="LightGreen"/>
<Setter Property="Foreground" Value="Green"/>
</Trigger>
</Style.Triggers>
</Style>
In order to embed this Style I would write next :
<ComboBox>
<ComboBoxItem Content="xxxx" Tag="true"/>
<ComboBoxItem Content="yyyy" Tag="false"/>
</ComboBox>
but how I can embed this Style in case I use DataContext binding?
Thanks in advance.
You could try adding a style setter to the ComboBox's Resources:
<ComboBox.Resources>
<Style TargetType="{x:Type ComboBoxItem}">
<Setter Property="Tag" Value="{Binding SomeValue}" />
</Style>
</ComboBox.Resources>

Resources