1

我正在尝试为超链接控件设置不同的样式。新样式基于与设置为控件的先前样式相同的基本样式,并且 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"];
4

0 回答 0