Hello,
I have xamDataChart, with mulitple series, and to want show a custom tooltip. When defining a DataTemplate the Item contains the List where the data are displayed. So it requires an index of that list to get it properties.
<DataTemplate x:Key="tooltipTemplate"> <Grid> <Grid.ColumnDefinitions> <ColumnDefinition Width="Auto"/> <ColumnDefinition Width="Auto"/> </Grid.ColumnDefinitions> <Grid.RowDefinitions> <RowDefinition/> <RowDefinition/> </Grid.RowDefinitions> <TextBlock Grid.Row="0" Grid.Column="0" Margin="0,0,5,0"> <Run Text="{Binding Path=Series.Title}"/><Run Text=":"/> </TextBlock> <TextBlock Grid.Row="0" Grid.Column="1"> <Run Text="{Binding Path=Item.Value, StringFormat={}{0:0.##}, Mode=OneWay}"/> <Run Text="{Binding Path=Item}"/> </TextBlock> </Grid> </DataTemplate>
Error message:
System.Windows.Data Error: 40 : BindingExpression path error: 'Value' property not found on 'object' ''CategoryGroupSerieData' (HashCode=31324050)'. BindingExpression:Path=Item.Value; DataItem='DataContext' (HashCode=13481002); target element is 'Run' (HashCode=2912325); target property is 'Text' (type 'String')
In code behehind I add that resource to the proper series:
var toolTip = new ContentControl(); toolTip.ContentTemplate = FindResource("tooltipTemplate") as DataTemplate; toolTip.SetBinding(ContentProperty, new Binding()); series.ToolTip = toolTip;
How can I access the data item of the proper series in the tooltip template?
Hello Markus,
I attached a sample to help you get started with custom tooltips. Let me know if you still have questions as this is can be a more than difficult issue to resolve. If you still happen to experience the error please try and reproduce it in the sample and reattach it here. Thanks.
1057.XamDataChart.zip
Thanks, but that solution is not working. I don't use StackedColumnSeries. I just use multiple column series. See attached project.
XamDataChart_MultiColumnSeries.zip
It turns out that since the SampleData object is the underlying dataitem for each series, it is preventing you from directly binding the properties in the tooltip datatemplate. The only way to achieve this is with a MultiBinding and a Converter. Please see my modified sample attached.
<DataTemplate x:Key="tooltipTemplate"> <Grid> <Grid.ColumnDefinitions> <ColumnDefinition Width="Auto"/> <ColumnDefinition Width="Auto"/> </Grid.ColumnDefinitions> <Grid.RowDefinitions> <RowDefinition/> <RowDefinition/> </Grid.RowDefinitions> <TextBlock Grid.Row="0" Grid.Column="0" Margin="0,0,5,0"> <Run Text="{Binding Path=Series.Title}"/><Run Text=":"/> </TextBlock> <TextBlock Grid.Row="0" Grid.Column="1" > <TextBlock.Text> <MultiBinding> <MultiBinding.Converter> <local:MyConverter ></local:MyConverter> </MultiBinding.Converter> <Binding Path="Item"></Binding> <Binding Path="Series.ValueMemberPath"></Binding> </MultiBinding> </TextBlock.Text> <!--<Run Text="{Binding Path=Item.Value, StringFormat={}{0:0.##}, Mode=OneWay}"/>--> <!--<Run Text="{Binding Path=Item}"/>--> </TextBlock> </Grid> </DataTemplate>
public class MyConverter : IMultiValueConverter { public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture) { if (values[0] == DependencyProperty.UnsetValue || values[1] == DependencyProperty.UnsetValue) { return null; } else { SampleData sd = values[0] as SampleData; string str = values[1]?.ToString(); string strIndex = str.Replace("Items[", ""); int index = int.Parse(strIndex[0].ToString()); return sd.Items[index].Value.ToString(); } } public object[] ConvertBack(object value, Type[] targetTypes, object parameter, CultureInfo culture) { throw new NotImplementedException(); } }
0042.XamDataChart_MultiColumnSeries.zip