[英]How to avoid redundant property for view model
I have an interface from which my model and viewmodel implement.I have a list property in interface which model implements as list but for some reasons I need to use observable collection in view model.How can I implement same property with use of observable collection.Here is my sample code, 我有一个模型和viewmodel的实现接口。在接口中有一个list属性,该模型以列表的形式实现,但由于某些原因,我需要在视图模型中使用可观察的集合。如何使用可观察的集合来实现相同的属性。这是我的示例代码,
public class MyObject
{
public string Property1 { get; set; }
public string Property1 { get; set; }
}
public interface IFoo
{
List<MyObject> MyList { get; set; }
}
public class Model : IFoo
{
private List<MyObject> mMyList;
public List<MyObject> MyList
{
get { return mMyList; }
set { mMyList = value; }
}
}
public class ViewModel : IFoo
{
// I want
//ObservableCollection<string> Mylist
//for view purpose,How can I use the same property from interface
}
Stop being so demanding. 别这么苛刻。 In your interfaces, I mean.
在您的界面中,我的意思是。
public interface IFoo
{
IList<MyObject> MyList { get; set; }
}
And use an OC behind the scenes 并在幕后使用OC
ObservableCollection implements IList, so you can easily ObservableCollection实现IList,因此您可以轻松实现
public class Bar : IFoo
{
public IList<MyObject> MyList {get; private set;}
public Bar()
{
MyList = new ObservableCollection<MyObject>();
//snip
The Binding will know our secret and act accordingly. 绑定者将知道我们的秘密并采取相应的行动。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.