Changing styles when text has been changed
Having trouble figuring how out to have the text change styles when the text is changed. I have it set up so the styles change when I am typing and the default is set to be grayed out to indicate what needs to go in that field. I want the text to be black and of normal font weight when it is changed from the default text and to remain that way.
Here are my current trigger styles
<Style TargetType="{x:Type TextBox}">
<Setter Property="Foreground" Value="DimGray" />
<Setter Property="FontSize" Value="15" />
<Setter Property="FontStyle" Value="Italic" />
<Setter Property="Padding" Value="5,4,0,4" />
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type TextBoxBase}">
<Border
x:Name="Bd"
BorderBrush="{TemplateBinding BorderBrush}"
BorderThickness="{TemplateBinding BorderThickness}"
CornerRadius="7"
>
<Border.Background>
<LinearGradientBrush StartPoint="0,0" EndPoint="2,1">
<GradientStop Color="{Binding Path=GradientColorStart}" Offset="0"/>
<GradientStop Color="{Binding Path=GradientColorEnd}" Offset="1"/>
<!--
<GradientStop Color="#CCCCCC" Offset="0"/>
开发者_StackOverflow <GradientStop Color="#FFFFFF" Offset="1"/>
-->
</LinearGradientBrush>
</Border.Background>
<ScrollViewer x:Name="PART_ContentHost"/>
</Border>
<ControlTemplate.Triggers>
<Trigger Property="IsEnabled" Value="False">
<Setter
Property="Background"
TargetName="Bd"
Value="{DynamicResource {x:Static SystemColors.ControlBrushKey}}"
/>
<Setter Property="Foreground" Value="Green"/>
</Trigger>
<Trigger Property="Width" Value="Auto">
<Setter Property="MinWidth" Value="100"/>
</Trigger>
<Trigger Property="Height" Value="Auto">
<Setter Property="MinHeight" Value="20"/>
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
<Style.Triggers>
<Trigger Property="IsFocused" Value="True">
<Setter Property="Foreground" Value="Black" />
<Setter Property="FontWeight" Value="Bold" />
<Setter Property="FontStyle" Value="Normal" />
<Setter Property="Text" Value="" />
</Trigger>
<Trigger Property="IsEnabled" Value="False">
<Setter Property="Foreground" Value="#00112d" />
<Setter Property="FontWeight" Value="Normal" />
<Setter Property="FontStyle" Value="Normal" />
</Trigger>
</Style.Triggers>
</Style>
This is something i would approach with the TargetNullValue of bindings, the string has to be null at first of course, so this might not be an option for you:
<TextBox Text="{Binding TestString, TargetNullValue=Enter Text Here, UpdateSourceTrigger=PropertyChanged}">
<TextBox.Style>
<Style TargetType="{x:Type TextBox}">
<Style.Triggers>
<DataTrigger Binding="{Binding TestString}" Value="{x:Null}">
<Setter Property="Foreground" Value="{StaticResource {x:Static SystemColors.GrayTextBrushKey}}"/>
</DataTrigger>
</Style.Triggers>
</Style>
</TextBox.Style>
</TextBox>
精彩评论