问题
I've implemented a resource dictionary so that I could customize my tooltip on a pie chart. But now the series returns a pie chart where the slices are all the same color. How do I get it to act like it did before I applied the resource dictionary? PieDataPoint.xaml is the file I copied into my project to use as a reesource dictionary.
回答1:
I was able to solve my issue. Below is my resource dictionary. I had to manually define my colors as a style near the bottom.
<ResourceDictionary
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:datavis="clr-namespace:System.Windows.Controls.DataVisualization;assembly=System.Windows.Controls.DataVisualization.Toolkit"
xmlns:charting="clr-namespace:System.Windows.Controls.DataVisualization.Charting;assembly=System.Windows.Controls.DataVisualization.Toolkit"
>
<!--http://silverlight.codeplex.com/SourceControl/changeset/view/18791#507651-->
<ControlTemplate
x:Key="MyPieDataPointTemplate"
TargetType="charting:PieDataPoint">
<Grid
x:Name="Root"
Opacity="0">
<VisualStateManager.VisualStateGroups>
<VisualStateGroup x:Name="CommonStates">
<VisualStateGroup.Transitions>
<VisualTransition GeneratedDuration="0:0:0.1"/>
</VisualStateGroup.Transitions>
<VisualState x:Name="Normal"/>
<VisualState x:Name="MouseOver">
<Storyboard>
<DoubleAnimation
Storyboard.TargetName="MouseOverHighlight"
Storyboard.TargetProperty="Opacity"
To="0.6"
Duration="0"/>
</Storyboard>
</VisualState>
</VisualStateGroup>
<VisualStateGroup x:Name="SelectionStates">
<VisualStateGroup.Transitions>
<VisualTransition GeneratedDuration="0:0:0.1"/>
</VisualStateGroup.Transitions>
<VisualState x:Name="Unselected"/>
<VisualState x:Name="Selected">
<Storyboard>
<DoubleAnimation
Storyboard.TargetName="SelectionHighlight"
Storyboard.TargetProperty="Opacity"
To="0.6"
Duration="0"/>
</Storyboard>
</VisualState>
</VisualStateGroup>
<VisualStateGroup x:Name="RevealStates">
<VisualStateGroup.Transitions>
<VisualTransition GeneratedDuration="0:0:0.5"/>
</VisualStateGroup.Transitions>
<VisualState x:Name="Shown">
<Storyboard>
<DoubleAnimation
Storyboard.TargetName="Root"
Storyboard.TargetProperty="Opacity"
To="1"
Duration="0"/>
</Storyboard>
</VisualState>
<VisualState x:Name="Hidden">
<Storyboard>
<DoubleAnimation
Storyboard.TargetName="Root"
Storyboard.TargetProperty="Opacity"
To="0"
Duration="0"/>
</Storyboard>
</VisualState>
</VisualStateGroup>
</VisualStateManager.VisualStateGroups>
<Path
x:Name="Slice"
Data="{TemplateBinding Geometry}"
Fill="{TemplateBinding Background}"
Stroke="{TemplateBinding BorderBrush}"
StrokeMiterLimit="1">
<ToolTipService.ToolTip>
<StackPanel>
<ContentControl
Content="{TemplateBinding FormattedIndependentValue}"
FontWeight="Bold"/>
<ContentControl
Content="{TemplateBinding FormattedDependentValue}"/>
<ContentControl
Content="{TemplateBinding FormattedRatio}"/>
</StackPanel>
</ToolTipService.ToolTip>
</Path>
<Path
x:Name="SelectionHighlight"
Data="{TemplateBinding GeometrySelection}"
Fill="Red"
StrokeMiterLimit="1"
IsHitTestVisible="False"
Opacity="0"/>
<Path
x:Name="MouseOverHighlight"
Data="{TemplateBinding GeometryHighlight}"
Fill="White"
StrokeMiterLimit="1"
IsHitTestVisible="False"
Opacity="0"/>
</Grid>
</ControlTemplate>
<!--http://silverlight.codeplex.com/SourceControl/changeset/view/18791#507636-->
<datavis:ResourceDictionaryCollection x:Key="MyPalette">
<!-- Blue -->
<ResourceDictionary>
<RadialGradientBrush x:Key="Background" GradientOrigin="-0.1,-0.1" Center="0.075,0.015" RadiusX="1.05" RadiusY="0.9">
<GradientStop Color="#FFB9D6F7"/>
<GradientStop Color="#FF284B70" Offset="1"/>
</RadialGradientBrush>
<Style x:Key="DataPointStyle" TargetType="Control">
<Setter Property="Template" Value="{StaticResource MyPieDataPointTemplate}"/>
<Setter Property="Background" Value="{StaticResource Background}"/>
</Style>
</ResourceDictionary>
<!-- Red -->
<ResourceDictionary>
<RadialGradientBrush x:Key="Background" GradientOrigin="-0.1,-0.1" Center="0.075,0.015" RadiusX="1.05" RadiusY="0.9">
<GradientStop Color="#FFFBB7B5"/>
<GradientStop Color="#FF702828" Offset="1"/>
</RadialGradientBrush>
<Style x:Key="DataPointStyle" TargetType="Control">
<Setter Property="Template" Value="{StaticResource MyPieDataPointTemplate}"/>
<Setter Property="Background" Value="{StaticResource Background}"/>
</Style>
</ResourceDictionary>
<!-- Light Green -->
<ResourceDictionary>
<RadialGradientBrush x:Key="Background" GradientOrigin="-0.1,-0.1" Center="0.075,0.015" RadiusX="1.05" RadiusY="0.9">
<GradientStop Color="#FFB8C0AC"/>
<GradientStop Color="#FF5F7143" Offset="1"/>
</RadialGradientBrush>
<Style x:Key="DataPointStyle" TargetType="Control">
<Setter Property="Template" Value="{StaticResource MyPieDataPointTemplate}"/>
<Setter Property="Background" Value="{StaticResource Background}"/>
</Style>
</ResourceDictionary>
<!-- Yellow -->
<ResourceDictionary>
<RadialGradientBrush x:Key="Background" GradientOrigin="-0.1,-0.1" Center="0.075,0.015" RadiusX="1.05" RadiusY="0.9">
<GradientStop Color="#FFFDE79C"/>
<GradientStop Color="#FFF6BC0C" Offset="1"/>
</RadialGradientBrush>
<Style x:Key="DataPointStyle" TargetType="Control">
<Setter Property="Template" Value="{StaticResource MyPieDataPointTemplate}"/>
<Setter Property="Background" Value="{StaticResource Background}"/>
</Style>
</ResourceDictionary>
<!-- Indigo -->
<ResourceDictionary>
<RadialGradientBrush x:Key="Background" GradientOrigin="-0.1,-0.1" Center="0.075,0.015" RadiusX="1.05" RadiusY="0.9">
<GradientStop Color="#FFA9A3BD"/>
<GradientStop Color="#FF382C6C" Offset="1"/>
</RadialGradientBrush>
<Style x:Key="DataPointStyle" TargetType="Control">
<Setter Property="Template" Value="{StaticResource MyPieDataPointTemplate}"/>
<Setter Property="Background" Value="{StaticResource Background}"/>
</Style>
</ResourceDictionary>
<!-- Magenta -->
<ResourceDictionary>
<RadialGradientBrush x:Key="Background" GradientOrigin="-0.1,-0.1" Center="0.075,0.015" RadiusX="1.05" RadiusY="0.9">
<GradientStop Color="#FFB1A1B1"/>
<GradientStop Color="#FF50224F" Offset="1"/>
</RadialGradientBrush>
<Style x:Key="DataPointStyle" TargetType="Control">
<Setter Property="Template" Value="{StaticResource MyPieDataPointTemplate}"/>
<Setter Property="Background" Value="{StaticResource Background}"/>
</Style>
</ResourceDictionary>
<!-- Dark Green -->
<ResourceDictionary>
<RadialGradientBrush x:Key="Background" GradientOrigin="-0.1,-0.1" Center="0.075,0.015" RadiusX="1.05" RadiusY="0.9">
<GradientStop Color="#FF9DC2B3"/>
<GradientStop Color="#FF1D7554" Offset="1"/>
</RadialGradientBrush>
<Style x:Key="DataPointStyle" TargetType="Control">
<Setter Property="Template" Value="{StaticResource MyPieDataPointTemplate}"/>
<Setter Property="Background" Value="{StaticResource Background}"/>
</Style>
</ResourceDictionary>
<!-- Gray Shade -->
<ResourceDictionary>
<RadialGradientBrush x:Key="Background" GradientOrigin="-0.1,-0.1" Center="0.075,0.015" RadiusX="1.05" RadiusY="0.9">
<GradientStop Color="#FFB5B5B5"/>
<GradientStop Color="#FF4C4C4C" Offset="1"/>
</RadialGradientBrush>
<Style x:Key="DataPointStyle" TargetType="Control">
<Setter Property="Template" Value="{StaticResource MyPieDataPointTemplate}"/>
<Setter Property="Background" Value="{StaticResource Background}"/>
</Style>
</ResourceDictionary>
<!-- Blue -->
<ResourceDictionary>
<RadialGradientBrush x:Key="Background" GradientOrigin="-0.1,-0.1" Center="0.075,0.015" RadiusX="1.05" RadiusY="0.9">
<GradientStop Color="#FF98C1DC"/>
<GradientStop Color="#FF0271AE" Offset="1"/>
</RadialGradientBrush>
<Style x:Key="DataPointStyle" TargetType="Control">
<Setter Property="Template" Value="{StaticResource MyPieDataPointTemplate}"/>
<Setter Property="Background" Value="{StaticResource Background}"/>
</Style>
</ResourceDictionary>
<!-- Brown -->
<ResourceDictionary>
<RadialGradientBrush x:Key="Background" GradientOrigin="-0.1,-0.1" Center="0.075,0.015" RadiusX="1.05" RadiusY="0.9">
<GradientStop Color="#FFC1C0AE"/>
<GradientStop Color="#FF706E41" Offset="1"/>
</RadialGradientBrush>
<Style x:Key="DataPointStyle" TargetType="Control">
<Setter Property="Template" Value="{StaticResource MyPieDataPointTemplate}"/>
<Setter Property="Background" Value="{StaticResource Background}"/>
</Style>
</ResourceDictionary>
<!-- Cyan -->
<ResourceDictionary>
<RadialGradientBrush x:Key="Background" GradientOrigin="-0.1,-0.1" Center="0.075,0.015" RadiusX="1.05" RadiusY="0.9">
<GradientStop Color="#FFADBDC0"/>
<GradientStop Color="#FF446A73" Offset="1"/>
</RadialGradientBrush>
<Style x:Key="DataPointStyle" TargetType="Control">
<Setter Property="Template" Value="{StaticResource MyPieDataPointTemplate}"/>
<Setter Property="Background" Value="{StaticResource Background}"/>
</Style>
</ResourceDictionary>
<!-- Special Blue -->
<ResourceDictionary>
<RadialGradientBrush x:Key="Background" GradientOrigin="-0.1,-0.1" Center="0.075,0.015" RadiusX="1.05" RadiusY="0.9">
<GradientStop Color="#FF2F8CE2"/>
<GradientStop Color="#FF0C3E69" Offset="1"/>
</RadialGradientBrush>
<Style x:Key="DataPointStyle" TargetType="Control">
<Setter Property="Template" Value="{StaticResource MyPieDataPointTemplate}"/>
<Setter Property="Background" Value="{StaticResource Background}"/>
</Style>
</ResourceDictionary>
<!-- Gray Shade 2 -->
<ResourceDictionary>
<RadialGradientBrush x:Key="Background" GradientOrigin="-0.1,-0.1" Center="0.075,0.015" RadiusX="1.05" RadiusY="0.9">
<GradientStop Color="#FFDCDCDC"/>
<GradientStop Color="#FF757575" Offset="1"/>
</RadialGradientBrush>
<Style x:Key="DataPointStyle" TargetType="Control">
<Setter Property="Template" Value="{StaticResource MyPieDataPointTemplate}"/>
<Setter Property="Background" Value="{StaticResource Background}"/>
</Style>
</ResourceDictionary>
<!-- Gray Shade 3 -->
<ResourceDictionary>
<RadialGradientBrush x:Key="Background" GradientOrigin="-0.1,-0.1" Center="0.075,0.015" RadiusX="1.05" RadiusY="0.9">
<GradientStop Color="#FFF4F4F4"/>
<GradientStop Color="#FFB7B7B7" Offset="1"/>
</RadialGradientBrush>
<Style x:Key="DataPointStyle" TargetType="Control">
<Setter Property="Template" Value="{StaticResource MyPieDataPointTemplate}"/>
<Setter Property="Background" Value="{StaticResource Background}"/>
</Style>
</ResourceDictionary>
<!-- Gray Shade 4 -->
<ResourceDictionary>
<RadialGradientBrush x:Key="Background" GradientOrigin="-0.1,-0.1" Center="0.075,0.015" RadiusX="1.05" RadiusY="0.9">
<GradientStop Color="#FFF4F4F4"/>
<GradientStop Color="#FFA3A3A3" Offset="1"/>
</RadialGradientBrush>
<Style x:Key="DataPointStyle" TargetType="Control">
<Setter Property="Template" Value="{StaticResource MyPieDataPointTemplate}"/>
<Setter Property="Background" Value="{StaticResource Background}"/>
</Style>
</ResourceDictionary>
</datavis:ResourceDictionaryCollection>
来源:https://stackoverflow.com/questions/4715959/ive-customized-my-toolkit-pie-chart-but-now-all-slices-are-same-color