将ObservableCollections的ObservableCollection绑定到TabControl TabItems中的嵌套ListView

时间:2013-10-06 10:34:46

标签: wpf binding nested itemssource

不是像下面第一个TabControl中那样显式地为每个ObservableCollections声明一个选项卡,而是需要它们像第二个TabControl一样动态生成,并将嵌套ListView的ItemsSource设置为每个嵌套的ObservableCollections。

换句话说:为什么第二个TabControl中的嵌套ListViews的ItemSource绑定不起作用?有没有办法将嵌套的ObservableCollection的索引设置为包含ObservableCollection的索引?

或者:如何使第二个动态TabControl和嵌套的ListView看起来像第一个静态TabControl和嵌套的ListViews?

using System.Collections.ObjectModel;
using System.Windows;

namespace GridViewColumns2
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();

            ViewModel viewModel = new ViewModel();

            viewModel.ThingCollections = new ObservableCollection<ThingCollection>();

            viewModel.ThingCollections.Add(new ThingCollection { Name = "One" });
            viewModel.ThingCollections[0].Things = new ObservableCollection<Thing>();
            viewModel.ThingCollections[0].Things.Add(new Thing { Name = "One.One" });
            viewModel.ThingCollections[0].Things.Add(new Thing { Name = "One.Two" });
            viewModel.ThingCollections[0].Things.Add(new Thing { Name = "One.Three" });


            viewModel.ThingCollections.Add(new ThingCollection { Name = "Two" });
            viewModel.ThingCollections[1].Things = new ObservableCollection<Thing>();
            viewModel.ThingCollections[1].Things.Add(new Thing { Name = "Two.One  " });
            viewModel.ThingCollections[1].Things.Add(new Thing { Name = "Two.Two" });
            viewModel.ThingCollections[1].Things.Add(new Thing { Name = "Two.Three" });

            DataContext = viewModel;
        }
    }

    public class ViewModel
    {
        public ObservableCollection<ThingCollection> ThingCollections { get; set; }
    }

    public class ThingCollection
    {
        public string Name { get; set; }

        public ObservableCollection<Thing> Things { get; set; }
    }

    public class Thing
    {
        public string Name { get; set; }
    }
}



<Grid>
    <Grid.ColumnDefinitions>
        <ColumnDefinition Width="*" />
        <ColumnDefinition Width="*" />
    </Grid.ColumnDefinitions>
    <TabControl>
        <TabItem>
            <TabItem.Header>One</TabItem.Header>
            <TabItem.Content>
                <ListView ItemsSource="{Binding Path=ThingCollections[0].Things}">
                    <ListView.View>
                        <GridView>
                            <GridViewColumn Width="120" Header="Name" DisplayMemberBinding="{Binding Name}" />
                        </GridView>
                    </ListView.View>
                </ListView>
            </TabItem.Content>
        </TabItem>
        <TabItem>
            <TabItem.Header>Two</TabItem.Header>
            <TabItem.Content>
                <ListView ItemsSource="{Binding Path=ThingCollections[1].Things}">
                    <ListView.View>
                        <GridView>
                            <GridViewColumn Width="120" Header="Name" DisplayMemberBinding="{Binding Name}" />
                        </GridView>
                    </ListView.View>
                </ListView>
            </TabItem.Content>
        </TabItem>
    </TabControl>

    <TabControl Grid.Column="1" ItemsSource="{Binding Path=ThingCollections}">
        <TabControl.ItemTemplate>
            <DataTemplate>
                <TextBlock Text="{Binding Name}" />
            </DataTemplate>
        </TabControl.ItemTemplate>
        <TabControl.ContentTemplate>
            <DataTemplate>
                <ListView ItemsSource="{Binding Path=ThingCollections[0].Things}">
                    <ListView.View>
                        <GridView>
                            <GridViewColumn Width="120" Header="Name" DisplayMemberBinding="{Binding Name}" />
                        </GridView>
                    </ListView.View>
                </ListView>
            </DataTemplate>
        </TabControl.ContentTemplate>
    </TabControl>


</Grid>

2 个答案:

答案 0 :(得分:3)

当你在DataTemplate时,DataContext已经是项目的那个(在你的情况下,是ThingCollection类。

所以而不是:

<ListView ItemsSource="{Binding Path=ThingCollections[0].Things}">

使用:

<ListView ItemsSource="{Binding Path=Things}">

要诊断将来的绑定错误,请按F5(开始调试),然后查看输出窗口。所有绑定错误都将记录在那里。在这种情况下,它会表示它无法在对象ThingCollections中找到名为ThingCollection的属性。

答案 1 :(得分:1)

在BindingPath中删除ThingCollections[0].。将为您设置DataContext到正确的ThingCollection。

<ListView ItemsSource="{Binding Path=Things}">