Search code examples
c#wpfwpftoolkit

WPF Chart Toolkit. Bind Label's content in ColumnDataPoint to ColumnSeries ViewModel


I have an extended ObservableCollection that holds the datapoints plus some extra information

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

Then I also have a ViewModel holding the ColumnSeries that uses this ExtendedCollection

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

Finally, I am trying to display the collection's property dateStamp on a label templated in each datapoint's column

<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>

I've tried many different Bindings for the Label's Content but none works


Solution

  • Try to change the path of the binding to "DataContext.ColumnValues.dateStamp":

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