有一个简单的方法来改变颜色:重新定义Palette
属性。我已经回答了一个关于颜色的类似问题,这里是:https ://stackoverflow.com/a/5626435/427225
对于图例项,是无法访问FormattedRatio
属性的,因为工具包库中有很多bug,但是可以显示绑定项的属性,我举个例子。
首先,您需要为LegendItem
类创建样式。它与默认的相同,除了 2 处更改:ContentTemplate
我命名的您自己的,并将属性的testItemTemplate
绑定更改为,而不是之前的。Content
{Binding DataContext}
<UserControl.Resources>
<Style x:Key="testLegendItemStyle" TargetType="chart:LegendItem">
<Setter Property="ContentTemplate" Value="{StaticResource testItemTemplate}" />
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="chart:LegendItem">
<Border Background="{TemplateBinding Background}" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}">
<StackPanel Orientation="Horizontal">
<Rectangle Width="8" Height="8" Fill="{Binding Background}" Stroke="{Binding BorderBrush}" StrokeThickness="1" Margin="0,0,3,0"/>
<datavis:Title Content="{Binding DataContext}" ContentTemplate="{TemplateBinding ContentTemplate}"/>
</StackPanel>
</Border>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</UserControl.Resources>
<chart:Chart>
<chart:PieSeries ItemsSource="{Binding Items}" IndependentValuePath="Title" DependentValuePath="Value" LegendItemStyle="{StaticResource testLegendItemStyle}" />
</chart:Chart>
接下来,您需要testItemTemplate
根据您的模型编写前面提到的,这里是示例。
C# 视图模型
var items = new[] {
new ItemViewModel(){ Title = "Apples", Value = 35, CalculatedAndFormattedValue = "1%" },
new ItemViewModel(){ Title = "Bananas", Value = 43, CalculatedAndFormattedValue = "4%" },
new ItemViewModel(){ Title = "Oranges", Value = 29, CalculatedAndFormattedValue = "3%" },
new ItemViewModel(){ Title = "Cherries", Value = 51, CalculatedAndFormattedValue = "2%" },
new ItemViewModel(){ Title = "Lemons", Value = 31, CalculatedAndFormattedValue = "5%" },
};
Xaml 数据模板
<DataTemplate x:Key="testItemTemplate">
<TextBlock>
<Run Text="{Binding Title}" />
<Run Text=" - " />
<Run Text="{Binding CalculatedAndFormattedValue}" />
</TextBlock>
</DataTemplate>
不用说,你应该自己计算百分比