【问题标题】:C# WPF managing nested structures using ItemsControlC# WPF 使用 ItemsControl 管理嵌套结构
【发布时间】: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

问题是:谁能告诉我,为什么没有创建内部结构(文本框)?

【问题讨论】:

标签: c# wpf xaml grid itemscontrol


【解决方案1】:

您没有在任何地方定义任何TextBoxes,这就是为什么您在视觉树中没有得到任何TextBoxes:

<Window x:Class="MiscSamples.NestedItemsControls"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="NestedItemsControls" Height="300" Width="300">
    <ItemsControl ItemsSource="{Binding Level1}">
        <ItemsControl.ItemsPanel>
            <ItemsPanelTemplate>
                <UniformGrid/>
            </ItemsPanelTemplate>
        </ItemsControl.ItemsPanel>
        <ItemsControl.ItemTemplate>
            <DataTemplate>
                <ItemsControl ItemsSource="{Binding Level2}">
                    <ItemsControl.ItemTemplate>
                        <DataTemplate>
                            <TextBox Text="{Binding Value}"/> <!-- You Are missing this! -->
                        </DataTemplate>
                    </ItemsControl.ItemTemplate>
                </ItemsControl>
            </DataTemplate>
        </ItemsControl.ItemTemplate>
    </ItemsControl>
</Window>

另外,正如评论中提到的,您的 Collections 应该是 ViewModel 或 Model 类型,而不是 UI 类型:

视图模型:

public class NestedItemsViewModel
{
    public List<Level1Item> Level1 { get; set; }
}

public class Level1Item
{
    public List<Level2Item> Level2 { get; set; }
}

public class Level2Item
{
    public string Value { get; set; }
}

代码背后:

public partial class NestedItemsControls : Window
{
    public NestedItemsControls()
    {
        InitializeComponent();

        DataContext = new NestedItemsViewModel()
                          {
                              Level1 = Enumerable.Range(0, 10)
                                                 .Select(l1 => new Level1Item()
                                                    {
                                                        Level2 = Enumerable.Range(0, 10)
                                                                           .Select(l2 => new Level2Item { Value = l1.ToString() + "-" + l2.ToString() })
                                                                           .ToList()
                                                    })
                                                  .ToList()
                          };
    }
}

请注意,如果您希望这些集合在运行时动态更改,则必须将 Lists 更改为 ObservableCollections。

还要注意你必须正确实现INotifyPropertyChanged

【讨论】:

  • 非常感谢,非常感谢您的帮助!
猜你喜欢
  • 1970-01-01
  • 2015-12-11
  • 1970-01-01
  • 2014-10-12
  • 1970-01-01
  • 1970-01-01
  • 2011-03-14
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多