【问题标题】:Reuse of the same style with changes in template in Silverlight 5在 Silverlight 5 中通过更改模板重用相同样式
【发布时间】:2023-03-25 04:36:01
【问题描述】:

我有很多hiperlink按钮的样式都是一样的,只是模板中的路径不同。

这是 XAML 片段

<Style x:Key="HeatmapLinkStyle" TargetType="HyperlinkButton">
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="HyperlinkButton">
                <Grid Margin="4,2" Height="40" VerticalAlignment="Top">
                    <VisualStateManager.VisualStateGroups>
                        <VisualStateGroup x:Name="CommonStates">
                            <VisualState x:Name="Normal"/>
                            <VisualState x:Name="MouseOver">
                                <Storyboard>
                                    <DoubleAnimation Duration="0:0:0.2" To="1.5" Storyboard.TargetProperty="(UIElement.RenderTransform).(CompositeTransform.ScaleX)" Storyboard.TargetName="element" d:IsOptimized="True"/>
                                    <DoubleAnimation Duration="0:0:0.2" To="1.5" Storyboard.TargetProperty="(UIElement.RenderTransform).(CompositeTransform.ScaleY)" Storyboard.TargetName="element" d:IsOptimized="True"/>
                                </Storyboard>
                            </VisualState>
                        </VisualStateGroup>
                        <VisualStateGroup x:Name="LinkStates">
                            <VisualState x:Name="ActiveLink">
                                <Storyboard>
                                    <ObjectAnimationUsingKeyFrames BeginTime="00:00:00" Duration="00:00:00.0010000" Storyboard.TargetName="ActiveLinkBorder" Storyboard.TargetProperty="(UIElement.Visibility)">
                                        <DiscreteObjectKeyFrame KeyTime="00:00:00">
                                            <DiscreteObjectKeyFrame.Value>
                                                <Visibility>Visible</Visibility>
                                            </DiscreteObjectKeyFrame.Value>
                                        </DiscreteObjectKeyFrame>
                                    </ObjectAnimationUsingKeyFrames>
                                </Storyboard>
                            </VisualState>
                            <VisualState x:Name="InactiveLink"/>
                        </VisualStateGroup>
                    </VisualStateManager.VisualStateGroups>
                    <Grid Width="32" Height="32" VerticalAlignment="Top">
                        <Path Data="M50.5,4.7500001C25.232973,4.75 4.75,25.232973 4.7500001,50.5 4.75,75.767029 25.232973,96.25 50.5,96.25 75.767029,96.25 96.25,75.767029 96.25,50.5 96.25,25.232973 75.767029,4.75 50.5,4.7500001z M50.5,0C78.390381,0 101,22.609621 101,50.5 101,78.390381 78.390381,101 50.5,101 22.609621,101 0,78.390381 0,50.5 0,22.609621 22.609621,0 50.5,0z" Stretch="Fill" Fill="#FF3FA9F5" Visibility="Visible" />
                        <Path x:Name="element" Data="F1M-1834.73,-354.432L-1772.06,-354.432 -1772.06,-417.099 -1834.73,-417.099 -1834.73,-354.432z M-1832.68,-385.765L-1803.39,-385.765 -1803.39,-415.052 -1774.11,-415.052 -1774.11,-385.765 -1803.39,-385.765 -1803.39,-356.484 -1832.68,-356.484 -1832.68,-385.765z"  Stretch="Uniform" Fill="#FF3FA9F5" Width="16" Height="16" Margin="0,0,0,0" RenderTransformOrigin="0.5,0.5">
                            <Path.RenderTransform>
                                <CompositeTransform ScaleY="1" ScaleX="1"/>
                            </Path.RenderTransform>
                        </Path>
                        <Ellipse x:Name="container" Fill="Transparent" Cursor="Hand" Height="32" Width="32"/>
                    </Grid>
                    <Border x:Name="ActiveLinkBorder" Width="32" Height="4" VerticalAlignment="Bottom" Opacity="0.5" Background="#FF3FA9F5" Visibility="Collapsed"/>
                </Grid>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>

唯一变化的部分是“元素”路径的数据。

有没有办法只使用一种基本样式并更改其他样式的路径?

谢谢大家!

【问题讨论】:

  • 您可以在样式中为 DataProperty 设置 Binding,如果您设置超链接按钮的 dataContext 将正常工作。

标签: silverlight styles reusability


【解决方案1】:

您可以创建一个基本样式并继承它。

这是一个很好的例子: basedOn Style

希望对你有帮助

【讨论】:

  • 感谢 Vinicius,但我不能使用这种方法,因为我只有一个二传手,它是用于模板的。但是我需要更改模板内的路径,所以我不能使用基本样式,可以吗?
  • 我花了最后一个小时试图做到这一点,但没有成功。对不起。
猜你喜欢
  • 2018-12-21
  • 1970-01-01
  • 1970-01-01
  • 2015-03-23
  • 1970-01-01
  • 2021-08-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多