我怎么能代替静态的ObservableCollection所以accesable上的MVVM方式,所有的窗口 [英] How can I replace static ObservableCollection so it accesable on all windows in the MVVM way

查看:161
本文介绍了我怎么能代替静态的ObservableCollection所以accesable上的MVVM方式,所有的窗口的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

ATM我有一个类(somerandomclasss)一个静态的ObservableCollection:

Atm I got a class(somerandomclasss) with a static ObservableCollection:

public static ObservableCollection<PersonViewModel> thepeoplelist = new ObservableCollection<PersonViewModem>();



不过,我为我的项目转换为MVVM,当然这是不以填补所有的好办法我itemscontrols(列表视图为主)我使用这种方式这个源头多个窗口。

However, I am converting my project to MVVM and of course this is not a good way to fill all my itemscontrols(listviews mainly) I got multiple windows that use this source in this way.

private void UserControl_Loaded(object sender, RoutedEventArgs e)
{
    lsvallthepeople.ItemSource = somerandomclasss.thepeoplelist;
}



在ListView显示,那么所有的信息的人。
然而,这较无的MVVM方式我敢打赌,我还没有发现没有一个公共静态的ObservableCollection工作的好方法,但存在这样的情况,你有一个列表视图,您可以编辑的人一个窗口,他们得到。更新了SQL数据库和PersonViewModel(即得到了IN​​otifyPropertyChanged的)

The listview displays then all the people with the info. However this isn t the MVVM way I bet, and I haven't found a good way to work without a public static ObservableCollection, however there is a window where you got a listview where you can edit the persons, they get updated in the SQL database and in the PersonViewModel (that got the INotifyPropertyChanged).

如果您需要更多的信息,随意问OFC。)

If you need any more information feel free to ask ofc :).

推荐答案

您可以使用静态绑定来绑定你的的ItemsSource ,而不是在你的设置它的代码隐藏手动

You can either use a Static binding to bind your ItemsSource instead of setting it in your code-behind manually

<ListBox ItemsSource="{Binding Source={
    x:Static local:somerandomclasss.thepeoplelist}}" ... />

或暴露从您的视图模型返回集合属性,并绑定到该属性。

or expose a property which returns the collection from your ViewModel and bind to that property

public class MyViewModel
{
    public ObservableCollection<PersonViewModel> PersonList
    {
        get { return somerandomclasss.thepeoplelist; }
    }

    ...
}

<ListBox ItemsSource="{Binding PersonList}" ... />

这篇关于我怎么能代替静态的ObservableCollection所以accesable上的MVVM方式,所有的窗口的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆