WPF MVVM 直接从 ViewModelClass 访问对象列表
WPF MVVM Directly accessing object list from ViewModelClass
我的问题听起来可能很愚蠢,但我真的不知道该怎么做,因为它会给我一个错误。所以我有一个 class 对象在 ObservableCollection:
public class UIElementList
{
public ObservableCollection<ChangingUIElements> ElementList { get; set; }
}
我想从我的 ViewModel class 中直接访问此 class,如下所示:
private UIElementList uIElementList = new UIElementList();
public UIElementList UIElementList
{
get => uIElementList.ElementList;
}
但是出了点大问题,因为编译器给我一个错误:
错误 CS0029 无法将类型 'System.Collections.ObjectModel.ObservableCollection<PartialResultOperation.Model.ChangingUIElements>' 隐式转换为 '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; }
}
问题:
public UIElementList UIElementList
{
get => uIElementList.ElementList
}
这里你尝试return一个UIElementList
,但是uIElementList.ElementList
是一个ObservableCollection。因此这行不通。
您的 属性 姓名也与您的 class 同名。请避免这种情况(因此不要 UIElementList UIElementList
写任何其他 属性 名称)。
或者您也可以使用一种方法:
public System.Collections.ObjectModel.ObservableCollection<ChangingUIElements> GetUIElementList()
{
return uIElementList.ElementList;
}
我的问题听起来可能很愚蠢,但我真的不知道该怎么做,因为它会给我一个错误。所以我有一个 class 对象在 ObservableCollection:
public class UIElementList
{
public ObservableCollection<ChangingUIElements> ElementList { get; set; }
}
我想从我的 ViewModel class 中直接访问此 class,如下所示:
private UIElementList uIElementList = new UIElementList();
public UIElementList UIElementList
{
get => uIElementList.ElementList;
}
但是出了点大问题,因为编译器给我一个错误: 错误 CS0029 无法将类型 'System.Collections.ObjectModel.ObservableCollection<PartialResultOperation.Model.ChangingUIElements>' 隐式转换为 '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; }
}
问题:
public UIElementList UIElementList
{
get => uIElementList.ElementList
}
这里你尝试return一个UIElementList
,但是uIElementList.ElementList
是一个ObservableCollection。因此这行不通。
您的 属性 姓名也与您的 class 同名。请避免这种情况(因此不要 UIElementList UIElementList
写任何其他 属性 名称)。
或者您也可以使用一种方法:
public System.Collections.ObjectModel.ObservableCollection<ChangingUIElements> GetUIElementList()
{
return uIElementList.ElementList;
}