Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/274.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 使用ObservableCollection进行WinRT数据绑定_C#_Data Binding_Windows Runtime_Winrt Xaml_Dependency Properties - Fatal编程技术网

C# 使用ObservableCollection进行WinRT数据绑定

C# 使用ObservableCollection进行WinRT数据绑定,c#,data-binding,windows-runtime,winrt-xaml,dependency-properties,C#,Data Binding,Windows Runtime,Winrt Xaml,Dependency Properties,在我的ViewModel中,我定义了以下observateCollection private readonly ObservableCollection<ChartSerie> _seriesData = new ObservableCollection<ChartSerie>(); public ObservableCollection<ChartSerie> SeriesData { get { return _s

在我的ViewModel中,我定义了以下
observateCollection

    private readonly ObservableCollection<ChartSerie> _seriesData = new ObservableCollection<ChartSerie>();
    public ObservableCollection<ChartSerie> SeriesData
    {
        get { return _seriesData; }
    }
ItemSource是我控件中的DependencyProperty

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));
控件继承自网格。我已将
OnItemSourceChanged
定义如下

private static void OnItemSourceChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{            
    var chart = d as MultipleDoubleColumnChart;
    foreach (var serie in (IList)chart.ItemSource)
    {
        var child = new ColumnChart();
        ...
        ...
    }
}
创建页面和视图模型时,序列数据集合为空,对
OnItemSourceChanged
的第一次也是唯一一次调用不会执行任何操作(如预期)。在页面上单击某个按钮后,数据被加载,新的ChartSerie对象被创建并添加到
SeriesData


虽然
SeriesData
是一个
observecollection
方法,但不再调用
onimsourcechanged
方法


发生了什么/出现了什么问题以及需要更改什么才能调用OnItemSourceChanged?

ObservableCollection
不会强制调用
OnItemSourceChanged
。在标准的
ItemsControl
中,它会激发
onitmchanged
。您需要做的是将
ItemSource
强制转换为
INotifyCollectionChanged
(当设置属性时),然后(如果它不为null)将事件处理程序添加到
CollectionChanged
。这将在数据更改时触发。好的,我在OnItemSourceChanged中有这样的内容:
var source=(System.Collections.Specialized.INotifyCollectionChanged)chart.ItemSource;source.CollectionChanged+=source\u CollectionChanged
Source\u CollectionChanged
是静态的。我如何访问像“OnItemSourceChange”参数->
DependencyObject d
中那样的非静态方法?通常,我不会在
OnItemSourceChanged
中设置
CollectionChanged
;我在
ItemSource
的setter方法中设置它。这样,
Source\u CollectionChanged
就不是静态的了。如果仍要按原样使用,可以对方法接收的
sender
参数设置大小写,类似于将
d转换为MultipleDoubleColumnChart
的方式。
private static void OnItemSourceChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{            
    var chart = d as MultipleDoubleColumnChart;
    foreach (var serie in (IList)chart.ItemSource)
    {
        var child = new ColumnChart();
        ...
        ...
    }
}