Silverlight 5超链接更改样式基于相同的基本样式但前景不同

时间:2013-02-22 07:21:16

标签: hyperlink silverlight-5.0 base foreground

我正在尝试为超链接控件设置不同的样式。新样式是基于与之前设置为控件的样式相同的基本样式,并且Foreground属性已被重写为新颜色。 但在应用程序中,前景不会改变。

几点说明: 如果覆盖属性是FontSize,事情似乎工作正常 在Silverlight 4中,事情很顺利 如果我删除了BasedOn属性(不是从基类派生的),一切正常 风格的第一次设置正在起作用,但后续改变风格的尝试并不起作用 如果我复制样式并更改前景色,则一切正常 *工作正常=前景正确变化。

基本风格:

<Style TargetType="HyperlinkButton" x:Key="NormalHyperlinkStyle">
    <Setter Property="FontFamily" Value="{StaticResource ContentFontFamily}"/>
    <Setter Property="FontSize" Value="{StaticResource DataGridFontSize}"/>
    <Setter Property="Background" Value="Transparent" />
    <Setter Property="Cursor" Value="Hand" />
    <Setter Property="Foreground" Value="{StaticResource BaseColor4}" />
    <Setter Property="HorizontalContentAlignment" Value="Left" />
    <Setter Property="Padding" Value="1"/>
    <Setter Property="VerticalContentAlignment" Value="Top" />
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="HyperlinkButton">
                <Grid Background="{TemplateBinding Background}" Cursor="{TemplateBinding Cursor}">
                    <VisualStateManager.VisualStateGroups>
                        <VisualStateGroup x:Name="CommonStates">
                            <VisualState x:Name="Normal" />
                            <VisualState x:Name="MouseOver">
                                <Storyboard>
                                    <ColorAnimation Duration="0" To="{StaticResource AccentColor}" Storyboard.TargetProperty="(Control.Foreground).(SolidColorBrush.Color)" Storyboard.TargetName="contentPresenter" />
                                    <DoubleAnimation Duration="0" To="1.0" Storyboard.TargetProperty="(UIElement.Opacity)" Storyboard.TargetName="Underline" />
                                </Storyboard>
                            </VisualState>
                            <VisualState x:Name="Pressed" />
                            <VisualState x:Name="Disabled">
                                <Storyboard>
                                    <ColorAnimation Duration="0" To="#FFA0A0A0" Storyboard.TargetProperty="(Control.Foreground).(SolidColorBrush.Color)" Storyboard.TargetName="contentPresenter" />
                                </Storyboard>
                            </VisualState>
                        </VisualStateGroup>
                        <VisualStateGroup x:Name="FocusStates">
                            <VisualState x:Name="Focused">
                                <Storyboard>
                                    <DoubleAnimation Duration="0" Storyboard.TargetName="FocusVisualElement" Storyboard.TargetProperty="(FrameworkElement.Opacity)" To="1" />
                                </Storyboard>
                            </VisualState>
                            <VisualState x:Name="Unfocused" />
                        </VisualStateGroup>
                    </VisualStateManager.VisualStateGroups>

                    <StackPanel Margin="{TemplateBinding Padding}" HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" VerticalAlignment="{TemplateBinding VerticalContentAlignment}" UseLayoutRounding="True">
                        <ContentControl x:Name="contentPresenter" Content="{TemplateBinding Content}" ContentTemplate="{TemplateBinding ContentTemplate}" Foreground="{TemplateBinding Foreground}" />
                        <Rectangle x:Name="Underline" Height="1" VerticalAlignment="Bottom" Fill="{StaticResource AccentColorBrush}" Margin="0,1,0,2" Opacity="0" />
                    </StackPanel>

                    <Rectangle x:Name="FocusVisualElement" IsHitTestVisible="False" Margin="0" Opacity="0" RadiusX="2" RadiusY="2" Stroke="{StaticResource FocusVisualBrush}" StrokeThickness="{StaticResource FocusVisualStrokeThickness}" />
                </Grid>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>

派生风格:

<Style x:Key="ToolbarHyperlinkSelectedStyle" TargetType="HyperlinkButton" BasedOn="{StaticResource NormalHyperlinkStyle}">
    <Setter Property="Foreground" Value="{StaticResource AccentColor}" />
</Style>

代码背后:

        selectedButton.Style =
            (Style)Application.Current.Resources["ToolbarHyperlinkSelectedStyle"];

0 个答案:

没有答案