图表WPF中的自定义系列标签

时间:2017-01-06 21:25:32

标签: c# wpf charts

我们有一个图表控件,可以显示并排条形图。它基于ArguementDataMember“x”和ValueDataMember“y”。图表与具有属性x,y,z的Datasource(chartData,observablecollection)绑定。 我试图在系列标签中显示“z”。但是不能。得到错误

"BindingExpression path error: 'z' property not found on 'object' ''SeriesLabelItem' (HashCode=58379838)'. BindingExpression:Path=z; DataItem='SeriesLabelItem' (HashCode=58379838); target element is 'TextBlock' (Name=''); target property is 'Text' (type 'String')"

我的代码如下所示

 <dxc:BarSideBySideSeries2D DisplayName="Last Year" Brush="Goldenrod" ArgumentScaleType="Qualitative" ArgumentDataMember="x"  
                                                   ValueDataMember="y" ToolTipEnabled="False"  Name="Last" DataSource="{Binding ChartData}">

                            <dxc:BarSideBySideSeries2D.Label >
                                <dxc:SeriesLabel Visible="True" VerticalAlignment="Center"  Name="lblLast" DataContext="{Binding ChartData}">
                                    <dxc:SeriesLabel.ElementTemplate>
                                        <DataTemplate>
                                            <TextBlock Text="{Binding z}" Foreground="Black" FontSize="10" FontWeight="Bold" Background="White">
                                            </TextBlock>
                                        </DataTemplate>
                                    </dxc:SeriesLabel.ElementTemplate>
                                </dxc:SeriesLabel>

                            </dxc:BarSideBySideSeries2D.Label>
                            <dxc:BarSideBySideSeries2D.Model>
                                <dxc:SimpleBar2DModel/>
                            </dxc:BarSideBySideSeries2D.Model>
                        </dxc:BarSideBySideSeries2D>

1 个答案:

答案 0 :(得分:0)

元素模板对要应用模板的元素的数据上下文一无所知。在模板内部,您只能绑定到SeriesLabelItem的属性。要提供标签的自定义文本,您可以使用CustomDrawSeriesPoint事件。方法的实现如下:

private void chartControl_CustomDrawSeriesPoint(object sender, CustomDrawSeriesPointEventArgs e)
        {
            MyClass point = e.SeriesPoint.Tag as MyClass;
            if (point != null)
            {
                e.LabelText = point.z;
            }
        }

模板将如下所示:

<DataTemplate>
     <TextBlock Text="{Binding Text}" Foreground="Black" FontSize="10" FontWeight="Bold" Background="White">
     </TextBlock>
</DataTemplate>