来自MVVM的CollectionViewSource

时间:2014-12-03 09:02:31

标签: c# xaml

如果我在观点中有这样的话:

<UserControl.Resources>
        <app:People x:Key="vm"></app:People> <--nvm this for now...

        <CollectionViewSource   x:Name="cvs"
                                IsSourceGrouped="True"
                                ItemsPath="Items" />
    </UserControl.Resources>

然后我可以在代码隐藏中执行此操作:

private IList<Person> people;
private IList<Group<object, Person>> groupedPeople;

public People()
{
    this.InitializeComponent();
    people = Person.CreatePeople(200).ToList();
    groupedPeople = (from person in people
                     group person by person.City into g
                     orderby g.Key
                     select new Group<object, Person>
                     {
                         Key = g.Key.ToString(),
                         Items = g.ToList()
                     }).ToList();
    cvs.Source = groupedPeople;
}

这会将gropuedPeople的列表分配给cvs。 我想将此代码移动到我的viewmodel。

我的虚拟机已正确附加到视图:<app:People x:Key="vm"></app:People>

以下是PeopleVM的ctor:

public PeopleVm()
        {
            people = Person.CreatePeople(200).ToList();
            groupedPeople = (from person in people
                             group person by person.City into g
                             orderby g.Key
                             select new Group<object, Person>
                             {
                                 Key = g.Key.ToString(),
                                 Items = g.ToList()
                             }).ToList();
            cvs.Source = groupedPeople;
        }

这不起作用,因为此处cvs无法解决。 希望我在这里足够清楚。谢谢!

0 个答案:

没有答案