WPF图表工具包。将ColumnData中的标签内容绑定到ColumnSeries ViewModel

时间:2017-09-19 12:13:39

标签: c# wpf wpftoolkit

我有一个包含数据点和一些额外信息的扩展ObservableCollection

public class ExtendedCollection : ObservableCollection<KeyValuePair<string, int>>
{
    public string dateStamp { get; set; }
}

然后我还有一个ViewModel持有ColumnSeries使用此ExtendedCollection

private ExtendedCollection columnValues = new ExtendedCollection();
public ExtendedCollection ColumnValues
{
    get
    {
        return columnValues;
    }
    set
    {
        columnValues = value;
        PropChanged("ColumnValues");
    }
}

最后,我试图在每个数据点列中模板化的标签上显示集合的属性dateStamp

<chartingToolkit:ColumnSeries Name="columnSeries" DependentValuePath="Value" IndependentValuePath="Key" ItemsSource="{Binding ColumnValues}">
    <chartingToolkit:ColumnSeries.DataPointStyle>
        <Style TargetType="chartingToolkit:ColumnDataPoint">
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="chartingToolkit:ColumnDataPoint">
                        <Grid>
                            <Grid.RowDefinitions>
                                <RowDefinition Height="30" />
                                <RowDefinition Height="*" />
                            </Grid.RowDefinitions>
                            <Label Grid.Row="0" Content="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type chartingToolkit:ColumnSeries}},Path=dateStamp, Mode=TwoWay}"></Label>
                        </Grid>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Style>
    </chartingToolkit:ColumnSeries.DataPointStyle>
</chartingToolkit:ColumnSeries>

我为Binding的{​​{1}}尝试了很多不同的Label,但没有效果

1 个答案:

答案 0 :(得分:1)

尝试将绑定路径更改为&#34; DataContext.ColumnValues.dateStamp&#34;:

<Label Grid.Row="0" Content="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type chartingToolkit:ColumnSeries}},Path=DataContext.ColumnValues.dateStamp, Mode=TwoWay}" />