wpf 从另一个 ViewModel 访问一个属性
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16506653/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Accessing a property in one ViewModel from another
提问by Asaf
I want main viewmodel to have a certain list, and then access from many other viewmodels.
我希望主视图模型有一个特定的列表,然后从许多其他视图模型访问。
For example, in MainViewModel.cs I will have a list of 50 numbers, then in NumListViewModel.cs, I'd like to access it in order to show it as a list, and in AddNumViewModel.cs I'd like to be able to update that list.
例如,在 MainViewModel.cs 中,我将有一个包含 50 个数字的列表,然后在 NumListViewModel.cs 中,我想访问它以将其显示为列表,而在 AddNumViewModel.cs 中,我希望能够更新该列表。
It's been suggested that I use events / evenaggerator, which I did, but unfortunately, for all I know all I can do with it is send a num from one view to another and tell it to update the list, but the problem is, as the program grows, I will need to have a lot of subscribers in the main view model, and when something actually happens I will have to "publish" events according to the number of subscribers which makes it even harder to maintain.
有人建议我使用 events/evenaggerator,我确实这样做了,但不幸的是,据我所知,我所能做的就是从一个视图向另一个视图发送一个 num 并告诉它更新列表,但问题是,作为程序增长,我将需要在主视图模型中有很多订阅者,当实际发生某些事情时,我将不得不根据订阅者的数量“发布”事件,这使得维护变得更加困难。
I also found another answer, instructing to create an instance of anotherVM within the mainVM, with a parameter set to "this" which is a reference to the mainVM. It works, but then again, it could get quite long.
我还找到了另一个答案,指示在 mainVM 中创建 anotherVM 的一个实例,并将参数设置为“this”,这是对 mainVM 的引用。它有效,但话又说回来,它可能会持续很长时间。
So my question is, is there a betterway to access a property from another VM?
Like literally have the an instance of the class that holds the list in the mainVM, and then just be able to update / access it from the other VMs, without having to explicitly program which VM can. Would make life so much easier.
所以我的问题是,有没有更好的方法可以从另一个 VM 访问属性?
就像字面上有一个在 mainVM 中保存列表的类的实例,然后只需能够从其他 VM 更新/访问它,而无需显式编程哪个 VM 可以。会让生活变得更轻松。
In your answer, please try to avoid suggesting frameworks. Although there are some really good ones, I want to be able to do at least that by myself.
在您的回答中,请尽量避免建议框架。虽然有一些非常好的,但我希望至少能够自己做。
For example:
例如:
MainVM.cs:
主VM.cs:
public class MainVM
{
List lst = new List(); //Let's just say it's full...
}
OtherVM.cs:
其他VM.cs:
public class OtherVM
{
lst.Add(3);
}
PS: Yes I know it has been asked already, and yes I have done my research, BUT I the answers I found are too 'static', I guess?
PS:是的,我知道已经有人问过了,是的,我已经完成了我的研究,但是我发现的答案太“静态”了,我猜?
回答by BTownTKD
If you want directaccess to the list from an external ViewModel, then your options are to:
如果您想从外部 ViewModel直接访问列表,那么您的选择是:
Pass the List to the OtherVM as a constructor argument or public property. Then the OtherVM can treat it like a member.
Pass the MainVM to the OtherVM as a constructor argument or public property. Then the OtherVM can access the List by first accessing the MainVM.
Give the MainVM a static property called "Default" or "Instance," so you can access the static instance of MainVM from within OtherVM, without assigning it as a member field.
将 List 作为构造函数参数或公共属性传递给 OtherVM。然后OtherVM 可以将其视为成员。
将 MainVM 作为构造函数参数或公共属性传递给 OtherVM。然后OtherVM 可以通过首先访问MainVM 来访问List。
为 MainVM 提供一个名为“默认”或“实例”的静态属性,以便您可以从 OtherVM 中访问 MainVM 的静态实例,而无需将其分配为成员字段。
Example:
例子:
public class MainVM
{
private static MainVM _instance = new MainVM();
public static MainVM Instance { get { return _instance; } }
public List<XX> MyList { get; set; }
//other stuff here
}
//From within OtherVM:
MainVM.Instance.MyList.Add(stuff);

