I want to create a simple style for a text box. I want to keep everything about the standard text box and appearance, with the exception of one element.
OnFocus on wants to change the border color in the text box.
I wrote the following and it works. However, all the restyling, I have to declare the height, the appearance of the unfocused border is also different. How to create a template to just affect only the onfocus state.
<Style x:Key="TextBoxStyle" TargetType="TextBox">
<Setter Property="BorderBrush" Value="Gold" />
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="TextBox">
<Grid Height="{TemplateBinding Height}"
>
<VisualStateManager.VisualStateGroups>
<VisualStateGroup x:Name="CommonStates">
<VisualState x:Name="Normal" />
<VisualState x:Name="MouseOver" />
<VisualState x:Name="Pressed" />
<VisualState x:Name="Disabled" />
</VisualStateGroup>
<VisualStateGroup x:Name="FocusStates">
<VisualState x:Name="Focused">
<Storyboard>
<ColorAnimation Storyboard.TargetName="brd"
Storyboard.TargetProperty="(Border.BorderBrush).(SolidColorBrush.Color)"
Duration="0"
To="Red" />
</Storyboard>
</VisualState>
<VisualState x:Name="Unfocused"/>
</VisualStateGroup>
</VisualStateManager.VisualStateGroups>
<Border x:Name="brd"
BorderBrush="{TemplateBinding BorderBrush}"
BorderThickness="{TemplateBinding BorderThickness}"
Background="{TemplateBinding Background}"
CornerRadius="2">
<ContentPresenter x:Name="contentPresenter" />
</Border>
</Grid>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
source
share