Access XYDataPoint in a Bar Char BarTemplate

Charts for WPF Forum

Posted 6 years ago by Barry smith
Version: 18.1.0672

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">
  <TextBlock Text="{Binding DataContext.YValue, StringFormat={}{0:f1},
    RelativeSource={RelativeSource AncestorType=Border}}" HorizontalAlignment="Center" TextWrapping="Wrap"/>

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?

Comments (1)

Posted 6 years ago by Actipro Software Support - Cleveland, OH, USA

Hi Barry,

Yes we have a Border that has the DataContext and normally renders the bar.  Then there is a ContentPresenter that can render the BarTemplate within that.  ContentPresenters lose the DataContext inheritance since their Content becomes their DataContext.  The problem with passing the data point down to the content, is that then the string representation of the data point shows up in the bar with a default "null" BarTemplate.

We making a change to only include the ContentPresenter if you specify a BarTemplate and will pass the data point as Content to that ContentPresenter.  Please write our support address and mention this thread if you'd like to try a preview build with the change to ensure it meets your needs.

Actipro Software Support

The latest build of this product (v24.1.2) was released 2 months ago, which was after the last post in this thread.

Add Comment

Please log in to a validated account to post comments.