Hi,
I have a control with xamPieCHart on it.
<StackPanel> <ig:XamPieChart x:Name="_pieChart" ItemsSource="{Binding ChartData}" ValueMemberPath="Value" LabelMemberPath="Key" HorizontalAlignment="Left" Margin="10,10,0,10" VerticalAlignment="Top" Height="249" Width="268" BorderBrush="Aqua" BorderThickness="2"> </ig:XamPieChart> <TextBlock Text="{Binding Label}"/> </StackPanel>
and this is my ViewModel
public class PieChartViewModel { public PieChartViewModel() { Label = "First constructor"; ChartData = new Dictionary<String, double>(); } public PieChartViewModel(IHydraulicsSnapshotAnalysis analysis, MeasurementUnits units) { Table = new SnapshotPressureTableViewModel(analysis, units); } public void UpdateVM() { ChartData.Add("Item 1", 23); ChartData.Add("Item 2", 25); ChartData.Add("Something", 11); ChartData.Add("BlaBla", 19); Label = "Test Biniding on this control"; } }
Binding works correctly - When I add some entries to the dictionary then I will get the pie chart no problem.
What the problem is that when I run UpdateVM() from external class the pie chart on the control wont update. Neither will label. What am I doing wrong here?
Thanks
Michal
Hello Michal,
I have been investigating into the behavior you are looking to achieve, and I believe this issue is coming from missing interfaces that will update your UI that something has changed. Specifically, the .NET Dictionary class does not implement the INotifyCollectionChanged interface, and so it will not update the UI when updated. You can get around this by setting the XamPieChart.ItemsSource to null and then back to your Dictionary to “force” a refresh. Alternatively, if you were to use a collection type that does implement this by default such as ObservableCollection, the UI update would happen automatically.
Regarding the Label issue, I believe this is similar in that you likely do not have the INotifyPropertyChanged interface implemented on the Label property. You can read further about this interface here.
Please let me know if you have any other questions or concerns on this matter.
Andrew, observable collection works well with values for the pie chart. How do I implement biding for the labels of a pie chart when the ItemSource is bound to ObservableCollection<double> for the values?
Thank you Andrew. I was able to get Lable updated by inheriting Notifier in my ViewModel and adding Notify() method to property set block. This however does not work for dictionary. I will try something else.