I have a bar chart in which I wish to display the values of the underlying data in the bar itself (I had some issues with space which deterred me from using the LabelVisibility / LabelFormat properties to display the values on mouse hover).
I have assigned a BarTemplate for my BarSeries which uses an ancestor binding to access the original data point:
BarTemplate="{StaticResource TextInColumns}"
And then I define the template as follows. I inspected the visual tree and found that I can access the data point by using the RelativeSource property and looking back to the Border (It looks like the BarTemplate is hosted in a ContentPresenter inside a Border which has the XYDataPoint as its DataContext):
<DataTemplate x:Key="TextInColumns">
<Grid>
<TextBlock Text="{Binding DataContext.YValue, StringFormat={}{0:f1},
RelativeSource={RelativeSource AncestorType=Border}}" HorizontalAlignment="Center" TextWrapping="Wrap"/>
</Grid>
</DataTemplate>
My question is: Is this a reliable method of accessing the underlying data of the datapoint? It feels like I'm relying on undocumented behaviour, which might fairly change from one release to the next. Is there a way to have the DataContext of the BarTemplate set to the XYDataPoint?