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 back开发者_高级运维gorund 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>.........
精彩评论