【发布时间】:2013-07-05 16:21:35
【问题描述】:
所以,我已经发布了一个关于 WPF 中嵌套控件结构的问题,在这里: Nested controls structure - in XAML or C#? 我收到了如下解决方案:
<ItemsControl ItemsSource="{Binding SomeCollectionOfViewModel}">
<ItemsControl.ItemsPanel>
<ItemsPanelTemplate>
<UniformGrid/>
</ItemsPanelTemplate>
</ItemsControl.ItemsPanel>
<ItemsControl.ItemTemplate>
<DataTemplate>
<ItemsControl ItemsSource="{Binding SomeCollection}"> <!-- Nested Content -->
...
</DataTemplate>
<ItemsControl.ItemTemplate>
</ItemsControl>
我已经使用了该解决方案,并提出以下建议:
<!-- The UniformGrids - boxes -->
<ItemsControl ItemsSource="{Binding UniformGridCollection}">
<ItemsControl.ItemsPanel>
<ItemsPanelTemplate>
<UniformGrid />
</ItemsPanelTemplate>
</ItemsControl.ItemsPanel>
<ItemsControl.ItemTemplate>
<DataTemplate>
<!-- The TextBoxes - Cells -->
<ItemsControl ItemsSource="{Binding TextBoxCollection}">
<ItemsControl.ItemsPanel>
<ItemsPanelTemplate>
<UniformGrid />
</ItemsPanelTemplate>
</ItemsControl.ItemsPanel>
</ItemsControl>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
这里是 C# 方面:
public partial class MainWindow : Window
{
private readonly ObservableCollection<UniformGrid> uniformGridCollection = new ObservableCollection<UniformGrid>();
public ObservableCollection<UniformGrid> UniformGridCollection { get { return uniformGridCollection; } }
private readonly ObservableCollection<UniformGrid> textBoxCollection = new ObservableCollection<UniformGrid>();
public ObservableCollection<UniformGrid> TextBoxCollection { get { return textBoxCollection; } }
public MainWindow()
{
InitializeComponent();
for (int i = 1; i <= 9; i++)
{
UniformGrid box = new UniformGrid();
UniformGridCollection.Add(box);
UniformGrid cell = new UniformGrid();
TextBoxCollection.Add(cell);
}
DataContext = this;
}
}
但不知何故,“单元格” - 统一网格内的文本框不会创建。相反,我只得到 9 个统一网格包含在一个大统一网格中(在 Paneltemplate 中指定)。 我用 Snoop v 2.8.0 检查了 prgram: http://i40.tinypic.com/htzo5l.jpg
问题是:谁能告诉我,为什么没有创建内部结构(文本框)?
【问题讨论】:
-
你的集合不应该是 UI 元素类型,而是 ViewModel 类型。
-
您能具体说明一下吗?
-
对于未来的谁,如果图片链接说自己不可用,请检查:web.archive.org/web/20190825224312/http://i40.tinypic.com/…
标签: c# wpf xaml grid itemscontrol