首先,我对wpf和XAML很新。我的大部分背景都是使用Windows窗体应用程序。
我有一个非常简单的图表,其中三个Line Series绑定到数据对象。我想自定义图例,不仅显示线系列颜色和标题,还要在每个图例项下添加一个文本块,以显示系列中的最后一个数据点值。
这是我到目前为止所做的:
<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>
答案 0 :(得分:1)
我认为问题在于你正在使用绑定到你的DataContext对象的{Binding},我认为在这种情况下是LegendItem本身。这是what {Binding} does的一个很好的解释。您可能希望添加路径以获取特定属性,或使用转换器以您希望的方式格式化结果。