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>.........
Related
When the user enters info I want all the controls to look and act the same way, as a result I have done the following.
The label and textbox controls are in a stackpanel as:
<StackPanel Style="{StaticResource ResourceKey=myInput}" HorizontalAlignment="Left">
<Label Content="Label" Name="label1" />
<TextBox Name="textBox1" ></TextBox>
</StackPanel>
the style "myInput" is:
<Style x:Key="myInput" TargetType="{x:Type StackPanel}">
<Style.Resources>
<Style TargetType="{x:Type Label}" BasedOn="{StaticResource {x:Type Label}}">
<Setter Property="FontSize" Value="12" />
</Style>
<Style TargetType="{x:Type TextBox}">
<Setter Property="Margin" Value="5,-5,2,2"></Setter>
<Setter Property="Height" Value="23"/>
<Setter Property="VerticalAlignment" Value="Top"/>
<Style.Triggers>
<Trigger Property="IsFocused" Value="true">
<Setter Property="Background" Value="Blue" >
</Setter>
</Trigger>
</Style.Triggers>
</Style>
</Style.Resources>
</Style>
Now every time that I apply that style to a stackpanel that has a label and a textbox the background of the textbox changes to blue when it receives focus.
How could I set the label's fontweight to bold also when that event fires? I will like to do this with xaml.
Use a DataTrigger on your Label which looks to see if the keyboard focus is inside of the parent StackPanel that contains both objects
<Style TargetType="{x:Type Label}" BasedOn="{StaticResource {x:Type Label}}">
<Setter Property="FontSize" Value="12" />
<Style.Triggers>
<DataTrigger Value="True" Binding="{Binding IsKeyboardFocusWithin,
RelativeSource={RelativeSource AncestorType={x:Type StackPanel}}}">
<Setter Property="FontWeight" Value="Bold" />
</Trigger>
</Style.Triggers>
</Style>
I have tried this method.. without luck..
<Style TargetType="{x:Type DataGridRow}">
<Style.Triggers>
<Trigger Property="ItemsControl.AlternationIndex" Value="0">
<Setter Property="Foreground" Value="Red" />
</Trigger>
</Style.Triggers>
</Style>
Is there a way to get the row Index?
I have even tried
<DataTrigger Binding="{Binding AlternationIndex}" Value="0">
<Setter Property="Foreground" Value="Green"></Setter>
</DataTrigger>
Finally, this is what I ended up with for generically setting alternate row colors.
<Style TargetType="{x:Type DataGrid}">
<Setter Property="Background" Value="#FFF" />
<Setter Property="AlternationCount" Value="2" />
</Style>
<Style TargetType="{x:Type DataGridRow}">
<Style.Triggers>
<Trigger Property="ItemsControl.AlternationIndex" Value="0">
<Setter Property="Background" Value="#CCC"></Setter>
</Trigger>
<Trigger Property="ItemsControl.AlternationIndex" Value="1">
<Setter Property="Background" Value="#EEE"></Setter>
</Trigger>
</Style.Triggers>
</Style>
Unless already done, you have to set the AlternationCount property of DataGrid:
<DataGrid AlternationCount="2"
... />
You should additionally check whether the Foreground property is used for any Control in the DataGridRow. Try setting the Background property to test the alternation stuff.
Try setting the alternating background like this:
AlternationCount="2" AlternatingRowBackground="Bisque"
Try this
<DataGrid AlternationCount="2"
AlternatingRowBackground="Salmon" ........
Finally I used combination of Robin Maben and Th3G33k solution, because I want the alternation colour to override with my own, when some condition is met.
Thanks both.
<DataGrid x:Name="gridCustomerOrderItems" HorizontalAlignment="Stretch"
VerticalAlignment="Stretch" AutoGenerateColumns="False"
AlternationCount="2"
IsReadOnly="True" CanUserReorderColumns="True"
ScrollViewer.CanContentScroll="True"
ScrollViewer.VerticalScrollBarVisibility="Auto"
ScrollViewer.HorizontalScrollBarVisibility="Auto">
<DataGrid.RowStyle>
<Style TargetType="DataGridRow">
<Style.Triggers>
<!--first alteraniting colour-->
<Trigger Property="ItemsControl.AlternationIndex" Value="1">
<Setter Property="Background" Value="#EEE"></Setter>
</Trigger>
<!--then override with my own colour-->
<DataTrigger Binding="{Binding InvoiceSet}" Value="True">
<Setter Property="Background" Value="Green"></Setter>
</DataTrigger>
</Style.Triggers>
</Style>
</DataGrid.RowStyle>
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>
hey i wanna change row foreground color according to a boolean in the model, whats the best way of doing it?
Define the style as following (IsBlah is a boolian property):
<Style x:Key="MyRowStyle" TargetType="{x:Type dg:DataGridRow}">
<Setter Property="Background" Value="White"/>
<Setter Property="Foreground" Value="DarkBlue"/>
<Style.Triggers>
<DataTrigger Binding="{Binding IsBlah}" Value="False" >
<Setter Property="Background" Value="DarkGray" />
<Setter Property="Foreground" Value="White" />
</DataTrigger>
</Style.Triggers>
</Style>
Your DataGrid should have a custom RowStyle. (RowStyle="{StaticResource MyRowStyle})
This is basically the same answer as Boris, but here's the syntax if you prefer to define the style directly within the DataGrid definition.
Note: Blend won't give you a live preview of this so you'll have to run it
<DataGrid>
<DataGrid.RowStyle>
<Style TargetType="DataGridRow">
<Style.Triggers>
<DataTrigger Binding="{Binding HasErrors}" Value="True">
<Setter Property="Foreground" Value="Red"/>
</DataTrigger>
</Style.Triggers>
</Style>
</DataGrid.RowStyle>
</DataGrid>
Is there a way to apply a style just to some tooltips?
I'm trying to specify tooltip template just for tooltips showing validation erros.
Suppose I have a tooltip style, say errorTTStyle, and some validation template:
<Style TargetType="{x:Type TextBox}">
<Style.Triggers>
<Trigger Property="Validation.HasError" Value="true">
<Setter Property="ToolTip" Value="{Binding RelativeSource={RelativeSource Self}, Path=(Validation.Errors).CurrentItem.ErrorContent}" />
</Trigger>
</Style.Triggers>
</Style>
How to force WPF to use errorTTStyle just for this situation (I know I can change tootlip style globaly, but that's not what I want)?
You could add the style for the toolTip in the resources of the Textbox style and it would only be used by the parent style, also base that style in the errorTTStyle if you want to use an external style:
<Style TargetType="{x:Type TextBox}">
<Style.Resources>
<Style TargetType="{x:Type ToolTip}" BasedOn="{StaticResource errorTTStyle}" />
</Style.Resources>
<Style.Triggers>
<Trigger Property="Validation.HasError" Value="true">
<Setter Property="ToolTip" Value="{Binding RelativeSource={RelativeSource Self}, Path=(Validation.Errors).CurrentItem.ErrorContent}" />
</Trigger>
</Style.Triggers>
</Style>