如何绑定一个ListView被保存在WPF单一视图模型多个集合? [英] How to bind a ListView to multiple collections stored in a single ViewModel in WPF?

查看:119
本文介绍了如何绑定一个ListView被保存在WPF单一视图模型多个集合?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有2个收藏我要绑定到一个单独的 GridViewColumn 的ListView

I have 2 collections I want to bind to a separate GridViewColumn in a ListView:

public class EffectView : INotifyPropertyChanged
{

    ObservableCollection<Effect> effects;
    public ObservableCollection<Effect> Effects
    {
        get { return this.effects; }
        set
        {
            this.effects = value;
            this.RaisePropertyChanged ( "Effects" );
        }
    }

    ObservableCollection<EffectDescription> descriptions;
    public ObservableCollection<EffectDescription> Descriptions
    {
        get { return this.descriptions; }
        set
        {
            this.descriptions = value;
            this.RaisePropertyChanged ( "Descriptions" );
        }
    }
}

我可以做到这一点:

I can do this:

<ListView ItemsSource="{Binding EffectView.Effects}">
    <ListView.View>
        <GridView>
            <GridViewColumn Width="Auto"
                            DisplayMemberBinding="{Binding Name}"
                            Header="Name" />

            <GridViewColumn Width="Auto"
                            DisplayMemberBinding="{Binding Opacity}"
                            Header="Opacity" />

            <GridViewColumn Width="Auto"
                            DisplayMemberBinding="{Binding ?}"
                            Header="Description" />
        </GridView>
    </ListView.View>
</ListView>

但后来一切都作用于 EffectView.Effects ,但我想要的默认范围是 EffectView 所以我可以轻松地分配多个集合到的ListView

But then everything is scoped to EffectView.Effects, but I want the default scope to be EffectView so I can easily assign multiple collections to the ListView.

是这样的:

<ListView ItemsSource="{Binding EffectView}">
    <ListView.View>
        <GridView>
            <GridViewColumn Width="Auto"
                            DisplayMemberBinding="{Binding Effects Path=Name}"
                            Header="Name" />

            <GridViewColumn Width="Auto"
                            DisplayMemberBinding="{Binding Effects Path=Opacity}"
                            Header="Opacity" />

            <GridViewColumn Width="Auto"
                            DisplayMemberBinding="{Binding Descriptions Path=Usage}"
                            Header="Description" />
        </GridView>
    </ListView.View>
</ListView>

什么办法做到这一点?

Any way to accomplish this?

推荐答案

的ItemsSource 一个ListView是的集合的,其项目将出现在列表。所以,想了一会儿什么你问ListView中要做到:

The ItemsSource of a ListView is the collection whose items will appear in the list. So think for a moment about what you're asking the ListView to do:

  • 作为源收集,使用的东西是不是集合,而是包含它们
  • 对于列表中的每一行,显示从一个集合中的项,并从其他集合中的项目

注意那第二点:每一行必须显示从活动收集了一些项目,有些项目的描述集合。

Pay attention to that second point: every row must display some item from the Events collection and some item from the Descriptions collection.

将它挑选每一个项目?是什么在两个集合的项目之间的关系?

它的出现的,你真正需要的是对象的集合,其中包含的两个的事件和说明。然后,您可以绑定到该集合,以显示两个实体的元素。粗略地说,是这样的:

It appears that what you really need is a collection of objects that contains both an Event and a Description. You can then bind to that collection to display elements of both entities. Roughly, something like this:

public class EffectView : INotifyPropertyChanged
{

    ObservableCollection<EffectsAndDescriptions> effects;
    public ObservableCollection<EffectAndDescriptions> Effects
    {
        get { return this.effects; }
        set
        {
            this.effects = value;
            this.RaisePropertyChanged ( "EffectsAndDescriptions" );
        }
    }

}

internal class EffectsAndDescriptions
{
    public Effect Effect { get; set; }
    public Description Description { get; set; }
}

现在,您可以绑定到EffectsAndDescriptions集合(注意,这个假设的DataContext的ListView的的的是 EffectView

Now you can bind to the EffectsAndDescriptions collection (note this assumes the DataContext of the ListView's parent is EffectView)

<ListView ItemsSource="{Binding EffectsAndDescriptions}">
    <ListView.View>
        <GridView>
            <GridViewColumn Width="Auto"
                            DisplayMemberBinding="{Binding Effect.Name}"
                            Header="Name" />

            <GridViewColumn Width="Auto"
                            DisplayMemberBinding="{Binding Effect.Opacity}"
                            Header="Opacity" />

            <GridViewColumn Width="Auto"
                            DisplayMemberBinding="{Binding Description.Usage}"
                            Header="Description" />
        </GridView>
    </ListView.View>
</ListView>

这篇关于如何绑定一个ListView被保存在WPF单一视图模型多个集合?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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