Silverlight文本框的样式

3

我想为文本框创建一个简单的样式。除了一项之外,我希望保留标准文本框的所有外观和感觉。

在焦点上,我希望能够更改文本框的边框颜色。

我已经编写了以下内容,并且它确实起作用。但是,所有内容都被重新设计,我必须声明高度,非聚焦边框的外观和感觉也不同。如何创建模板以仅影响聚焦状态。

 <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>

你尝试过使用BasedOn属性吗? - SirDemon
2个回答

2

您需要复制原始文本框的整个模板,您可以在此处找到:这里。然后进行所需更改。


1
那是唯一的方法吗??嗨,为了覆盖一个状态,这需要大量工作....呸!!! - Gabe
@gmcalab:是的,我经常听到这样的说法,而且我也有同感,但是想象一个普遍的“更好”的东西是很困难的。大多数“更好的”都是针对个人开发者当前的需求而专门设计的。 - AnthonyWJones

0

SirDemon提到的示例...

这是一个文本块的样式:

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

假设我想要另一种字体大小为20的样式,但前景色仍然是紫色:
<Style
    x:Key="detailBlk20"
    TargetType="TextBlock"
    BasedOn="{StaticResource detailBlk}">
    <Setter
        Property="FontSize"
        Value="20" />
</Style>

编辑: 抱歉,重新阅读了问题。您想要更改模板。设置器属性可以设置任何属性。有趣的是,模板是一个属性,因此可以在样式中设置。但是,据我所知,您无法更改模板的单个部分。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接