Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/276.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/neo4j/3.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# WPF MVVM直接从ViewModelClass访问对象列表_C#_Wpf_Mvvm - Fatal编程技术网

C# WPF MVVM直接从ViewModelClass访问对象列表

C# WPF MVVM直接从ViewModelClass访问对象列表,c#,wpf,mvvm,C#,Wpf,Mvvm,我的问题可能听起来很傻,但我真的不知道怎么做,因为它给了我一个错误。我有一个类,在ObservableCollection中有对象: 公共类UIElementList { 公共ObservableCollection元素列表{get;set;} } 我想从我的ViewModel类直接访问这个类,如下所示: private UIElementList UIElementList=new UIElementList(); 公共UIElementList UIElementList { get=>u

我的问题可能听起来很傻,但我真的不知道怎么做,因为它给了我一个错误。我有一个类,在ObservableCollection中有对象:

公共类UIElementList
{
公共ObservableCollection元素列表{get;set;}
}
我想从我的ViewModel类直接访问这个类,如下所示:

private UIElementList UIElementList=new UIElementList();
公共UIElementList UIElementList
{
get=>uIElementList.ElementList;
}
但有些严重错误,因为编译器给了我一个错误: 错误CS0029无法将类型“System.Collections.ObjectModel.ObservableCollection”隐式转换为“PartialResultOperation.Model.UIElementList”解决方案:

private UIElementList uIElementList = new UIElementList();
public System.Collections.ObjectModel.ObservableCollection<ChangingUIElements> UIElementList2
{
    get => uIElementList.ElementList;
}

public class UIElementList
{
    public System.Collections.ObjectModel.ObservableCollection<ChangingUIElements> ElementList { get; set; }
}
这里您尝试返回一个
UIElementList
,但是
UIElementList.ElementList
是一个可观察的集合。因此,这是行不通的

此外,属性名称与类具有相同的名称。请避免这种情况(因此,不要写任何其他属性名,而要写
UIElementList UIElementList

或者您也可以使用以下方法:

public System.Collections.ObjectModel.ObservableCollection<ChangingUIElements> GetUIElementList()
{
    return uIElementList.ElementList;
}
public System.Collections.ObjectModel.ObservableCollection GetUIElementList()
{
返回uIElementList.ElementList;
}

非常感谢,它就像一个符咒一样奏效了!:)
public System.Collections.ObjectModel.ObservableCollection<ChangingUIElements> GetUIElementList()
{
    return uIElementList.ElementList;
}