I want to make a simple style for the textbox. I want to retain everything about the standard textbox look and feel except one item.
OnFocus on want to be able to change the border color of the textbox.
I have written the following and it does work. However, everything is restyled, I have to declare height, the look and feel of the non focused border is different as well. How can I create the template to just effect 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>
You need to duplicate the entire template of the original TextBox which you can find here. Then make the changes you need.
Example of what SirDemon mentioned...
Here is a style for a textblock:
<Style
x:Key="detailBlk"
TargetType="TextBlock">
<Setter
Property="FontSize"
Value="10" />
<Setter
Property="Foreground"
Value="Purple" />
</Style>
Let's say I wanted another style with a FontSize of 20, but the foreground color still purple:
<Style
x:Key="detailBlk20"
TargetType="TextBlock"
BasedOn="{StaticResource detailBlk}">
<Setter
Property="FontSize"
Value="20" />
</Style>
Edit: Sorry, reread question. You want to change the template. The setter properties can set any property. Interestingly, the template is a property so that can be set in the style. However, as far as I know you cannot change individual parts of the template.
精彩评论