Custom legend in wpf chart
Firstly, I am very new to wpf and XAML. The majority of my background is with Windows forms apps.
I have a very simple chart with three Line Series bound to data objects. I would like to customize the legend to show not only the Line Series colors and titles, but also add a textblock under each legend item to display the last data point value in the series.
Here is what I have so far:
<Grid Grid.Row="1" Grid.Column="1" Background="{StaticResource graphBackground}" >
<Grid.Resources >
<Style TargetType="charting:LineSeries" >
<Setter Property="PolylineStyle" >
<Setter.Value>
<Style TargetType="Polyline" >
<Setter Property="StrokeThickness" Value="5" />
</Style>
</Setter.Value>
</Setter>
</Style>
<Style x:Key="LegendItemStyle" TargetType="charting:LegendItem" >
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type charting:LegendItem}">
<Border BorderBrush="Black" BorderThickness="0">
<StackPanel>
开发者_运维知识库 <StackPanel Orientation="Horizontal" >
<Rectangle Width="12" Height="12" Fill="{Binding Background}" />
<datavis:Title Content="{TemplateBinding Content}" FontSize="18" Margin="10"/>
</StackPanel>
<StackPanel Orientation="Horizontal" >
<TextBlock Text="{Binding}" />
</StackPanel>
</StackPanel>
</Border>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</Grid.Resources>
<charting:Chart FontWeight="Bold">
<charting:Chart.Axes>
<charting:LinearAxis Orientation="Y" Minimum="0" Maximum="{Binding Path=graphmax}" Interval="{Binding Path=graphinterval}" ShowGridLines="True" FontSize="18" />
<charting:DateTimeAxis Orientation="X" FontSize="18" Minimum="{Binding Path=datemin}" Maximum="{Binding Path=datemax}" IntervalType="Months" Interval="1" />
</charting:Chart.Axes>
<charting:LineSeries ItemsSource="{Binding Path=dataPoints}" DependentValuePath="Value" IndependentValuePath="Key" Title="13 Wk" LegendItemStyle="{DynamicResource LegendItemStyle}">
<charting:LineSeries.DataPointStyle>
<Style TargetType="charting:LineDataPoint" >
<Setter Property="Background" Value="Green" />
<Setter Property="Opacity" Value="0" />
</Style>
</charting:LineSeries.DataPointStyle>
</charting:LineSeries>
I think the issue is that you are using {Binding}, which would bind to your DataContext object, which I believe is in this case is the LegendItem itself. Here's a good explanation of what {Binding} does. You would either want to add a path to get a specific property, or use a converter to format the result the way you want.
精彩评论