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 for your update(s) on this matter. I don’t believe Notifier will work for the Dictionary in this case, as the Notifier will notify your UI that the actual instance of the Dictionary has changed, and this is not what is actually happening. The instance is the same, but you are adding to the collection.
I am glad to hear that ObservableCollection is working for you in this case, but if you are looking for a different Label and Value to use for the LabelMemberPath and ValueMemberPath of the XamPieChart, I would not recommend binding to ObservableCollection<double>. Instead, I would recommend creating your own custom class that has properties to represent the label and the value and bind to that. For example, using the below class you could bind to an ObservableCollection<MyDataItem> and set the LabelMemberPath and the ValueMemberPath to “Label” and “Value” respectively:
public class MyDataItem { public string Label { get; set; } public int Value { get; set; } }