WinRT DataBinding with ObservableCollection
-
14-10-2022 - |
Pregunta
In my ViewModel I've defined following ObservableCollection
.
private readonly ObservableCollection<ChartSerie> _seriesData = new ObservableCollection<ChartSerie>();
public ObservableCollection<ChartSerie> SeriesData
{
get { return _seriesData; }
}
On my Page I bind this collection.
ItemSource="{Binding SeriesData}"
ItemSource is a DependencyProperty in my control.
public object ItemSource
{
get { return (object)GetValue(ItemSourceProperty); }
set { SetValue(ItemSourceProperty, value); }
}
public static readonly DependencyProperty ItemSourceProperty =
DependencyProperty.Register("ItemSource", typeof(object), typeof(MultipleColumnChart), new PropertyMetadata(null, OnItemSourceChanged));
The control inherits from Grid. I've defined OnItemSourceChanged
as following.
private static void OnItemSourceChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
var chart = d as MultipleDoubleColumnChart;
foreach (var serie in (IList)chart.ItemSource)
{
var child = new ColumnChart();
...
...
}
}
When the Page and ViewModel are created the SeriesData collection is empty and the first and only call to OnItemSourceChanged
does nothing (as expected). After a certain button click on the page the data were loaded and new ChartSerie object were created and added to SeriesData
.
Although SeriesData
is an ObservableCollection
the OnItemSourceChanged
Method is not called anymore.
What is/went wrong and what do I need to change that OnItemSourceChanged will be called?
No hay solución correcta
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow