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 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,
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.
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?
Hi Szilvester,
Did you find any resolution to this? I've tried a couple more options unfortunatelly to no avail. The main purpose of the Legend object is just to present some data related to the chart. Therefore the Legend will update each time you are changing the Fill of a DataPoint. So I don't think it is a good idea to use a checkbox in the template because it will return each original state each time the chart is refreshing. Instead I think it is better to place the checkboxes outside the grid.
Vlad
In my use-case I get a XamChart already bound to a datasource that I don't know anything about, so the second solution (adding/removing the series) is not a viable option.
I wanted to preserv the Legend colors when the series are reshown, but couldn't get hold of the original color. The Fill property of the Series is null, the Legend.Items[] collection is empty when I get the checked event from the checkbox. The only place I can get the color from is the Fill property in the LegendItemTemplate, but that changes to whatever I change the series.Fill (Transparent here). I also tried to grab that color by assigning it to an attached property on the checkbox with a OneTime binding, but unfortunately when I change the color, the whole chart gets rerendered, including the Legend, therefore the checkboxes get recreated.
One solution could be to define my own palette for the legend and set it explicitly, then I will know the color of each series when I need to reshow it. But I don't want to modify the default palette.
I have an idea. The Series is a DependencyObject, I can attach a property to it, and store the color there, but I can't get hold of the corresponding series object from the datatemplate/checkbox, only in the codebehind, which is triggered when the user clicks the checkbox. So I need to check the Fill color of the template and if it is not transparent store it in an attached property on the series, then next time I can restore it. I'm going to give this a go..
Let me know if you have a better idea.