WPF:更改继承主题中元素的样式
我想将主题的样式更改为继承样式(通过基于继承)。有什么想法吗?这基本上是为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 中做更多的事情,因为我将定义许多继承的样式,然后只需更改此样式即可。
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您正在浪费已经可用的非常有用的
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 theBackground
for the purpose that charting intended. Instead of setting it to transparent in your style, let your derived style set or overrideBackground
and then use{TemplateBinding Background}
in the control template where are you current using{StaticResource Backbrush}
. Your other use of{TemplateBinding Background}
on theGrid
element you can remove since it seems clear that your intention is that the grid background will be transparent.