Hi,
Is there a way to hide/show data series on a WPF XamChart?
The best would be a checkbox for each series on the Legend. I can do that with a LegendItemTemplate, but how can I connect the checkbox to a series (could hide it by setting the Fill to transparent)?
Thanks in advance,
Szilveszter
Hi Szilvester,
There are two ways to show/hide the series in the XamChart :
1. Setting the Fill property of each Series to Transparent (also the StrokeThickness to 0 so you can remove the outer border) .
2. Manually add/remove the Series objects from the XamChart.Series collection.
Regarding the first option If you use a checkboxes outside the chart you can easily bind the Fill property of the Series to the IsChecked property of the checkbox using converter. The binding is possible because the elements are in the VisualTree so you can use ElementName in the binding syntax e.g.
<Series1" StrokeThickness="0" Fill="{Binding ElementName=check1,Path=IsChecked,Converter={StaticResource customConverter}, ConverterParameter=Green}">
However in your case the checkboxes are in a DataTemplate so your best option is to handle the Checked/Unchecked events, get the Series and set the Fill property in codebehind. Please note that when the Fill property changes the XamChart updates the layout which can probably retrieve the current status of the checkboxes and cause some issues. You can take a look at my sample project showing the both scenarios.
Let me know if you have any questions with this matter.
Vlad
This sample doesn't seem to work well if I am using PRISM. I tried pulling the checkboxes and the chart into a usercontrol and defined a view region in my MainWindow (I collapsed the visibility of the Legend). I register the view into the region, but when the control gets loaded the checkboxes don't work. I get Binding Expression errors as well eg.
System.Windows.Data Error: 4 : Cannot find source for binding with reference 'ElementName=check1'. BindingExpression:Path=IsChecked; DataItem=null; target element is 'Fill'
System.Windows.Data Error: 4 : Cannot find source for binding with reference 'ElementName=check2'. BindingExpression:Path=IsChecked; DataItem=null; target element is 'Fill'
Any idea?
I'm sorry for not getting back to you sooner. Were you able to resolve the issue? From the exception message it looks like there is an wrong binding between the Fill property of the Series and the IsChecked property of the checkbox. Note that you need to use a boolean to brush converter.
Hi Vlad,
Yes, I managed to solve this with some tricky binding, I had to store the color for each series in an attached property, but it worked.
Thanks,