WPF:更改继承主题中元素的样式

发布于 2024-10-14 10:11:16 字数 2795 浏览 3 评论 0 原文

我想将主题的样式更改为继承样式(通过基于继承)。有什么想法吗?这基本上是为wpf工具包中的多系列图表定义多种样式。代码如下:

<Style x:Key="A" TargetType="DVC:ColumnDataPoint">
    <Setter Property="Background" Value="Transparent" />
    <Setter Property="BorderBrush" Value="Transparent" />
    <Setter Property="BorderThickness" Value="1" />
    <Setter Property="IsTabStop" Value="False" />
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="DVC:ColumnDataPoint">
                <Border BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" Opacity="0" x:Name="Root">
                    <Grid Background="{TemplateBinding Background}" Name="columngrid">
                        <Grid.Resources>
                            <Style x:Key="aquaboarder" TargetType="Border">
                                <Style.Resources>
                                    <LinearGradientBrush x:Key="BackBrush" StartPoint="0.5,0" EndPoint="0.5,1">
                                        <GradientStop Color="#B211B9D8" Offset="0.1" />
                                        <GradientStop Color="#FF0F56C7" Offset="0.9" />
                                    </LinearGradientBrush>
                                </Style.Resources>
                                <Setter Property="Background" Value="{StaticResource BackBrush}"/>
                            </Style>
                        </Grid.Resources>
                        <Border Name="columnBorder" BorderBrush="Transparent" BorderThickness="1" CornerRadius="20,20,0,0" Style="{StaticResource aquaboarder}">
                        </Border>
                    </Grid>                                             
                    <ToolTipService.ToolTip>
                        <ContentControl Content="{TemplateBinding FormattedDependentValue}" />
                    </ToolTipService.ToolTip>
                </Border>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>

继承的样式如下:

<Style x:Key="B" BasedOn="{StaticResource A}"  TargetType="DVC:ColumnDataPoint">
    <Style.Resources>
        <LinearGradientBrush x:Key="BackBrush" StartPoint="0.5,0" EndPoint="0.5,1">
            <GradientStop Color="#B24DE509" Offset="0.1" />
            <GradientStop Color="#FF238910" Offset="0.9" />
        </LinearGradientBrush>
    </Style.Resources>           
</Style>

我想将“样式A”中的columngrid设置为使用“样式B”中定义的backbrush。我不喜欢在样式 B 中做更多的事情,因为我将定义许多继承的样式,然后只需更改此样式即可。

I want to change the style of a theme an inherited style (inherited through based on). Have any idea? This is basically to define multiple styles for multi-series charts in wpf toolkit. Code looks as follows:

<Style x:Key="A" TargetType="DVC:ColumnDataPoint">
    <Setter Property="Background" Value="Transparent" />
    <Setter Property="BorderBrush" Value="Transparent" />
    <Setter Property="BorderThickness" Value="1" />
    <Setter Property="IsTabStop" Value="False" />
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="DVC:ColumnDataPoint">
                <Border BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" Opacity="0" x:Name="Root">
                    <Grid Background="{TemplateBinding Background}" Name="columngrid">
                        <Grid.Resources>
                            <Style x:Key="aquaboarder" TargetType="Border">
                                <Style.Resources>
                                    <LinearGradientBrush x:Key="BackBrush" StartPoint="0.5,0" EndPoint="0.5,1">
                                        <GradientStop Color="#B211B9D8" Offset="0.1" />
                                        <GradientStop Color="#FF0F56C7" Offset="0.9" />
                                    </LinearGradientBrush>
                                </Style.Resources>
                                <Setter Property="Background" Value="{StaticResource BackBrush}"/>
                            </Style>
                        </Grid.Resources>
                        <Border Name="columnBorder" BorderBrush="Transparent" BorderThickness="1" CornerRadius="20,20,0,0" Style="{StaticResource aquaboarder}">
                        </Border>
                    </Grid>                                             
                    <ToolTipService.ToolTip>
                        <ContentControl Content="{TemplateBinding FormattedDependentValue}" />
                    </ToolTipService.ToolTip>
                </Border>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>

And the inherited style goes as follows:

<Style x:Key="B" BasedOn="{StaticResource A}"  TargetType="DVC:ColumnDataPoint">
    <Style.Resources>
        <LinearGradientBrush x:Key="BackBrush" StartPoint="0.5,0" EndPoint="0.5,1">
            <GradientStop Color="#B24DE509" Offset="0.1" />
            <GradientStop Color="#FF238910" Offset="0.9" />
        </LinearGradientBrush>
    </Style.Resources>           
</Style>

I want to set the columngrid in "style A" to use the backbrush defined in "style B". I do not like to do more stuff in style B as I will have many of inherited styles be defined just changing this style afterwards.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

久随 2024-10-21 10:11:16

您正在浪费已经可用的非常有用的 Background 属性,并试图创建一个具有相同目的的新属性。由于您要覆盖控件模板,因此只需使用 Background 即可实现图表的目的。不要在样式中将其设置为透明,而是让派生样式设置或覆盖 Background,然后在当前使用 {TemplateBinding Background} >{静态资源背景刷}。您可以删除 Grid 元素上的 {TemplateBinding Background} 的其他使用,因为您的意图似乎很明显,即网格背景将是透明的。

You are wasting the perfectly useful Background property that is already available and trying to create a new one that serves the same purpose. Since you are overriding the control template, just use the Background for the purpose that charting intended. Instead of setting it to transparent in your style, let your derived style set or override Background and then use {TemplateBinding Background} in the control template where are you current using {StaticResource Backbrush}. Your other use of {TemplateBinding Background} on the Grid element you can remove since it seems clear that your intention is that the grid background will be transparent.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文