gpt4 book ai didi

c# - ObservableCollection 的网格 ObservableCollection

转载 作者:太空宇宙 更新时间:2023-11-03 11:22:35 25 4
gpt4 key购买 nike

这是一个有趣的案例,我无法在网上找到任何信息。我正在尝试创建一个网格,需要将 ObservableCollection 的 ObservableCollection 绑定(bind)到它。想象这样一个模型:

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 中的一个项目的 CollectionTitle。对于第二列,我希望每一行都包含与相应集合对象关联的 MyCollectionItems 项集。

从上面的代码:

  1. 集合为“c”
  2. 作为“我”的项目
+---------------------------------------------------------------------------------------------++        |       Column 0          |                        Column 1                          |+---------------------------------------------------------------------------------------------|+ Row 0  |  c[0].CollectionTitle   | c[0].i[0].ItemTitle ...i[1].ItemTitle ... i[2].ItemTitle |+ Row 1  |  c[1].CollectionTitle   | c[1].i[0].ItemTitle ...i[1].ItemTitle ... i[2].ItemTitle |+        |                         |                                                          |+ ...                                                                                         |+---------------------------------------------------------------------------------------------+

如果我有一组静态的 MyNewCollection 对象,这会很容易,但由于它可以增长到无穷大,我需要创建一个新的 ObservableCollection 的 MyNewCollection 对象,这就是我在理解如何做时遇到麻烦的地方这与 WPF。任何帮助将不胜感激。

谢谢。

最佳答案

这是使用 ItemsControl 的一种方法,其中每一行都包含另一个 ItemsControl。

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
}

关于c# - ObservableCollection 的网格 ObservableCollection,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10463435/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com