2

これは私がオンラインで情報を見つけることができなかった興味深いケースです。グリッドを作成しようとしていますが、ObservableCollectionのObservableCollectionをグリッドにバインドする必要があります。次のようなモデルを想像してみてください。

public class App
{
    private ObservableCollection<MyNewCollection> collections;
}

public class MyNewCollection : DependencyObject
{
    public ObservableCollection<MyCollectionItem> items;
    // ... public properties: string CollectionTitle 
}

public class MyCollectionItem : DependencyObject 
{
    // ... public properties: string ItemTitle
}

グリッドの最初の列にコレクションオブジェクトのアイテムを一覧表示して、すべての行にコレクションObservableCollectionsのアイテムの1つからのCollectionTitleが含まれるようにします。2番目の列では、各行に、適切なコレクションオブジェクトに関連付けられたMyCollectionItemsアイテムのセットを含める必要があります。

上記のコードから:

  1. 'c'としてのコレクション
  2. 'i'としてのアイテム
+ ------------------------------------------------- -------------------------------------------- +
+ | 列0| 列1|
+ ------------------------------------------------- -------------------------------------------- |
+行0| c [0] .CollectionTitle | c [0] .i [0] .ItemTitle ... i [1] .ItemTitle ... i [2] .ItemTitle |
+行1| c [1] .CollectionTitle | c [1] .i [0] .ItemTitle ... i [1] .ItemTitle ... i [2] .ItemTitle |
+ | | |
+ ... |
+ ------------------------------------------------- -------------------------------------------- +

MyNewCollectionオブジェクトの静的なセットがあればこれは簡単でしたが、無限大になる可能性があるため、MyNewCollectionオブジェクトの新しいObservableCollectionを作成する必要があります。ここで、WPFでこれを行う方法を理解するのに問題が発生します。 。どんな助けでもありがたいです。

ありがとう。

4

2 に答える 2

2

これは、各行に別のItemsControlが含まれるItemsControlを使用する1つの方法です。

Xaml:

<Page.Resources>
    <DataTemplate x:Key="ChildItemTemplate" 
                  DataType="{x:Type Samples:NestedCollectionItem}">
        <TextBlock Text="{Binding Title}" Margin="5"/>
    </DataTemplate>
    <ItemsPanelTemplate x:Key="ChildItemPanel">
        <StackPanel Orientation="Horizontal"/>
    </ItemsPanelTemplate>
    <DataTemplate x:Key="ItemTemplate" 
                  DataType="{x:Type Samples:NestedCollectionChildViewModel}">
        <Grid>
            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="Auto" SharedSizeGroup="c1"/>
                <ColumnDefinition />
            </Grid.ColumnDefinitions>
            <TextBlock VerticalAlignment="Center"  Text="{Binding Title}"/>
            <ItemsControl 
                Grid.Column="1" 
                ItemsSource="{Binding Items}"
                ItemsPanel="{StaticResource ChildItemPanel}"
                ItemTemplate="{StaticResource ChildItemTemplate}"
                />
        </Grid>
    </DataTemplate>
</Page.Resources>

<Page.DataContext>
    <Samples:NestedCollectionRootViewModel/>
</Page.DataContext>

<Grid>
    <ItemsControl 
        Grid.IsSharedSizeScope="True"
        ItemsSource="{Binding Items}" 
        ItemTemplate="{StaticResource ItemTemplate}"/>
</Grid>

コード:

public class NestedCollectionRootViewModel
{
    public NestedCollectionRootViewModel()
    {
        Items =
            new ObservableCollection<NestedCollectionChildViewModel>
                {
                    new NestedCollectionChildViewModel
                        {
                            Title = "Item 1",
                            Items =
                                new ObservableCollection<NestedCollectionItem>
                                    {
                                        new NestedCollectionItem {Title = "One"},
                                        new NestedCollectionItem {Title = "Two"},
                                        new NestedCollectionItem {Title = "Three"},
                                        new NestedCollectionItem {Title = "Four"},
                                    }
                        },

                    new NestedCollectionChildViewModel
                        {
                            Title = "Item 2",
                            Items =
                                new ObservableCollection<NestedCollectionItem>
                                    {
                                        new NestedCollectionItem {Title = "Five"},
                                        new NestedCollectionItem {Title = "Six"},
                                    }
                        },

                };
    }

    public ObservableCollection<NestedCollectionChildViewModel> Items 
       { get; private set; }
}

public class NestedCollectionChildViewModel
{
    public string Title { get; set; }
    public ObservableCollection<NestedCollectionItem> Items { get; set; }
}

public class NestedCollectionItem
{
    public string Title { get; set; }
    // ... etc
}
于 2012-05-05T16:04:35.247 に答える
0

私はC#で簡単に作業しただけですが、正しく覚えていれば、直接バインドできない場合は、MyCollectionItem要素を処理するカスタムValueConverterクラスを作成し、それをバインドに使用する必要があります。

于 2012-05-05T16:05:49.803 に答える