C# WPF MVVM直接从ViewModelClass访问对象列表
我的问题可能听起来很傻,但我真的不知道怎么做,因为它给了我一个错误。我有一个类,在ObservableCollection中有对象: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
公共类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;
}