如何为 WPF 工具包图表赋予样式
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15184501/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to give Style to WPF Toolkit Chart
提问by Avinash Singh


I am using WPF Toolkit Chart with PieChart in my WPF Application.
我在 WPF 应用程序中使用 WPF Toolkit Chart 和 PieChart。
I want to change by default white background to Transparent in PieChart Picture..
我想在 PieChart 图片中默认将白色背景更改为透明..
How to give Style to Achieve that
如何赋予风格以实现这一目标
采纳答案by kmatyaszek
If you looked at visual tree you find out that you must change Background property of grid and border to change background to transparent (elements highlighted in yellow in the below picture).
如果您查看可视化树,您会发现必须更改网格和边框的背景属性才能将背景更改为透明(下图中以黄色突出显示的元素)。


To do that you can change color in Loadedevent. First you must find EdgePanelwith name ChartAreaand after that you must change color of grid and border. If you want to set also background of Legendto transparent you must find Legendelement and set appropriate properties.
为此,您可以在Loaded事件中更改颜色。首先你必须找到EdgePanel名字ChartArea,然后你必须改变网格和边框的颜色。如果您还想将背景设置Legend为透明,您必须找到Legend元素并设置适当的属性。
<DVC:Chart Canvas.Top="80" Canvas.Left="10" Name="mcChart"
Width="400" Height="250"
Background="Orange"
Loaded="mcChart_Loaded">
<DVC:Chart.Series>
<DVC:PieSeries Title="Experience"
ItemsSource="{StaticResource FruitCollection}"
IndependentValueBinding="{Binding Path=Name}"
DependentValueBinding="{Binding Path=Share}">
</DVC:PieSeries>
</DVC:Chart.Series>
</DVC:Chart>
Code-behind:
代码隐藏:
private void mcChart_Loaded(object sender, RoutedEventArgs e)
{
EdgePanel ep = VisualHelper.FindChild<EdgePanel>(sender as Chart, "ChartArea");
if (ep != null)
{
var grid = ep.Children.OfType<Grid>().FirstOrDefault();
if (grid != null)
{
grid.Background = new SolidColorBrush(Colors.Transparent);
}
var border = ep.Children.OfType<Border>().FirstOrDefault();
if (border != null)
{
border.BorderBrush = new SolidColorBrush(Colors.Transparent);
}
}
Legend legend = VisualHelper.FindChild<Legend>(sender as Chart, "Legend");
if (legend != null)
{
legend.Background = new SolidColorBrush(Colors.Transparent);
legend.BorderBrush = new SolidColorBrush(Colors.Transparent);
}
}
Helper class to find child element in this case EdgePanel:
在这种情况下查找子元素的助手类EdgePanel:
class VisualHelper
{
public static T FindChild<T>(DependencyObject parent, string childName) where T : DependencyObject
{
if (parent == null) return null;
T foundChild = null;
int childrenCount = VisualTreeHelper.GetChildrenCount(parent);
for (int i = 0; i < childrenCount; i++)
{
var child = VisualTreeHelper.GetChild(parent, i);
T childType = child as T;
if (childType == null)
{
foundChild = FindChild<T>(child, childName);
if (foundChild != null) break;
}
else if (!string.IsNullOrEmpty(childName))
{
var frameworkElement = child as FrameworkElement;
if (frameworkElement != null && frameworkElement.Name == childName)
{
foundChild = (T)child;
break;
}
}
else
{
foundChild = (T)child;
break;
}
}
return foundChild;
}
}
回答by Martin Liversage
WPF was designed to allow you to style controls through XAML; not code. Making the plot area and legend transparent in a pie chart is also possible through styling. Unfortunately, the border around the plot area cannot be controlled using a property and instead you have to modify the entire control template. In the end using styling is probably just as tedious as writing code behind that modifies the visual tree, but to me at least, it still feels like a cleaner approach.
WPF 旨在允许您通过 XAML 设置控件样式;不是代码。也可以通过样式使饼图中的绘图区域和图例透明。不幸的是,绘图区域周围的边框无法使用属性进行控制,而您必须修改整个控制模板。最后,使用样式可能和编写修改可视化树的代码一样乏味,但至少对我来说,它仍然感觉像是一种更简洁的方法。
<chartingToolkit:Chart>
<chartingToolkit:Chart.PlotAreaStyle>
<Style TargetType="Grid">
<Setter Property="Background" Value="Transparent"/>
</Style>
</chartingToolkit:Chart.PlotAreaStyle>
<chartingToolkit:Chart.LegendStyle>
<Style TargetType="visualizationToolkit:Legend">
<Setter Property="Margin" Value="15,0"/>
<Setter Property="VerticalAlignment" Value="Center"/>
<Setter Property="BorderBrush" Value="Transparent"/>
<Setter Property="Background" Value="Transparent"/>
</Style>
</chartingToolkit:Chart.LegendStyle>
<chartingToolkit:Chart.Style>
<Style TargetType="chartingToolkit:Chart">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="chartingToolkit:Chart">
<Border Background="{TemplateBinding Background}" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" Padding="{TemplateBinding Padding}">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="Auto" />
<RowDefinition Height="*" />
</Grid.RowDefinitions>
<visualizationToolkit:Title Content="{TemplateBinding Title}" Style="{TemplateBinding TitleStyle}" />
<!-- Use a nested Grid to avoid possible clipping behavior resulting from ColumnSpan+Width=Auto -->
<Grid Grid.Row="1" Margin="0,15,0,15">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*" />
<ColumnDefinition Width="Auto" />
</Grid.ColumnDefinitions>
<visualizationToolkit:Legend x:Name="Legend" Title="{TemplateBinding LegendTitle}" Style="{TemplateBinding LegendStyle}" Grid.Column="1" />
<chartingprimitives:EdgePanel x:Name="ChartArea" Style="{TemplateBinding ChartAreaStyle}">
<Grid Canvas.ZIndex="-1" Style="{TemplateBinding PlotAreaStyle}" />
<!--<Border Canvas.ZIndex="10" BorderBrush="#FF919191" BorderThickness="1" />-->
</chartingprimitives:EdgePanel>
</Grid>
</Grid>
</Border>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</chartingToolkit:Chart.Style>
<chartingToolkit:PieSeries ... />
</chartingToolkit:Chart>
The PlotAreaStyleand LegendStyleare modified to make them transparent. The border around the plot area is removed by modifying the ControlTemplateof the chart and simply commenting out the offending Borderelement.
该PlotAreaStyle和LegendStyle被修改,以使其透明。通过修改ControlTemplate图表并简单地注释掉有问题的Border元素,可以删除绘图区域周围的边框。

