Style for Silverlight Text Box

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>
+3
source share
2 answers

You need to duplicate the entire template of the original TextBox, which you can find here . Then make the necessary changes.

+2
source

, SirDemon...

:

<Style
    x:Key="detailBlk"
    TargetType="TextBlock">
    <Setter
        Property="FontSize"
        Value="10" />
    <Setter
        Property="Foreground"
        Value="Purple" />
</Style>

, FontSize 20, :

<Style
    x:Key="detailBlk20"
    TargetType="TextBlock"
    BasedOn="{StaticResource detailBlk}">
    <Setter
        Property="FontSize"
        Value="20" />
</Style>

: , . . . , , . , , .

0

Source: https://habr.com/ru/post/1756954/


All Articles