Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/25.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# ViewModel中的集合不会通知我的视图_C#_.net_Xaml_Mvvm_Binding - Fatal编程技术网

C# ViewModel中的集合不会通知我的视图

C# ViewModel中的集合不会通知我的视图,c#,.net,xaml,mvvm,binding,C#,.net,Xaml,Mvvm,Binding,我将视图的数据上下文设置为ViewModel,如下所示 PersonVM pvm = null; public MainPage() { this.InitializeComponent(); pvm = new PersonVM(); this.DataContext = pvm; } 然后单击一个按钮,我想向我的收藏中添加更多项目 private void Btn_PointerPressed(object sender,

我将视图的数据上下文设置为ViewModel,如下所示

PersonVM pvm = null;

public MainPage()
    {
        this.InitializeComponent();

        pvm = new PersonVM();
        this.DataContext = pvm;
    }
然后单击一个按钮,我想向我的收藏中添加更多项目

private void Btn_PointerPressed(object sender, Windows.UI.Xaml.Input.TappedRoutedEventArgs e)
    {
        pvm.DataSource.Add(new PersonVMWrapper(new PersonModel() { Name = "asdasd", Age = 23 }));
    }
这是我的ViewModel,在这里我显然做错了什么,但无法找到答案

namespace App3vv.ViewModel
{
public class PersonVMWrapper : INotifyPropertyChanged
{
    PersonModel _pm = null;

    public PersonVMWrapper(PersonModel pm)
    {
        _pm = pm;
    }

    public string Name
    {
        get
        {
            return "mr." + _pm.Name;
        }

        set { RaisePropertyChanged("Name"); }
    }

    public string Age
    {
        get
        {
            return _pm.Age.ToString() + " years";
        }

        set { RaisePropertyChanged("Age"); }
    }

    public event PropertyChangedEventHandler PropertyChanged;

    private void RaisePropertyChanged(string propertyName)
    {
        PropertyChangedEventHandler handler = this.PropertyChanged;
        if (handler != null)
        {
            handler(this, new PropertyChangedEventArgs(propertyName));
        }
    }
}

public class PersonVM : INotifyPropertyChanged
{
    private ObservableCollection<PersonVMWrapper> personDataSource;

    public PersonVM()
    {
        this.DataSource.Add(new PersonVMWrapper(new PersonModel() { Name = "John", Age = 32 }));
        this.DataSource.Add(new PersonVMWrapper(new PersonModel() { Name = "Kate", Age = 27 }));
        this.DataSource.Add(new PersonVMWrapper(new PersonModel() { Name = "Sam", Age = 30 }));

        DataSource.CollectionChanged += DataSource_CollectionChanged;
    }
    void DataSource_CollectionChanged(object sender, NotifyCollectionChangedEventArgs e)
    {
        this.RaisePropertyChanged("DataSource");
    }

    public ObservableCollection<PersonVMWrapper> DataSource
    {
        get
        {
            if (this.personDataSource == null)
            {
                this.personDataSource = new ObservableCollection<PersonVMWrapper>();
            }
            return this.personDataSource;
        }
        set
        {
        }
    }

    public event PropertyChangedEventHandler PropertyChanged;

    private void RaisePropertyChanged(string propertyName)
    {
        PropertyChangedEventHandler handler = this.PropertyChanged;
        if (handler != null)
        {
            handler(this, new PropertyChangedEventArgs(propertyName));
        }
    }
}
}
名称空间App3vv.ViewModel
{
公共类PersonVMWrapper:INotifyPropertyChanged
{
PersonModel_pm=null;
公共PersonVMWrapper(PersonModel pm)
{
_pm=pm;
}
公共字符串名
{
收到
{
返回“先生”+_pm.Name;
}
集合{RaisePropertyChanged(“Name”);}
}
公共字符串时代
{
收到
{
返回_pm.Age.ToString()+“年”;
}
集合{RaisePropertyChanged(“Age”);}
}
公共事件属性更改事件处理程序属性更改;
私有void RaisePropertyChanged(字符串propertyName)
{
PropertyChangedEventHandler处理程序=this.PropertyChanged;
if(处理程序!=null)
{
处理程序(这是新的PropertyChangedEventArgs(propertyName));
}
}
}
公共类PersonVM:INotifyPropertyChanged
{
私有可观测收集个人数据源;
公共人员虚拟机()
{
Add(newpersonvmwrapper(newpersonmodel(){Name=“John”,Age=32}));
Add(newpersonvmwrapper(newpersonmodel(){Name=“Kate”,Age=27}));
Add(newpersonvmwrapper(newpersonmodel(){Name=“Sam”,Age=30}));
DataSource.CollectionChanged+=DataSource\u CollectionChanged;
}
无效数据源\u CollectionChanged(对象发送方,NotifyCollectionChangedEventArgs e)
{
此.RaisePropertyChanged(“数据源”);
}
公共可观测收集数据源
{
收到
{
if(this.personDataSource==null)
{
this.personDataSource=新的ObservableCollection();
}
返回此.personDataSource;
}
设置
{
}
}
公共事件属性更改事件处理程序属性更改;
私有void RaisePropertyChanged(字符串propertyName)
{
PropertyChangedEventHandler处理程序=this.PropertyChanged;
if(处理程序!=null)
{
处理程序(这是新的PropertyChangedEventArgs(propertyName));
}
}
}
}

编辑:我实现了CollectionChange,但我的视图仍然没有添加新项。

将项添加到属性上的集合不会调用该属性上的setter,因此不会调用RaisePropertyChanged。您需要连接
CollectionChanged
事件您与集合的绑定看起来如何?如何连接此CollectionChanged事件?我的ViewModel中的所有集合都需要它吗(如果我有多个集合)?我发现了这一点,但为什么使用不同的列表?这将如何反映我的视图?