Nested controls structure - in XAML or C#?

时光毁灭记忆、已成空白 提交于 2019-12-12 17:26:40

问题


I want to create a structure which consists of quite some elements, the basic layout of it goes like that:

<UniformGrid>
  <Border>        // This one is 9x
    <UniformGrid> // This one is 9x, each as a child of the border
      <Border>    // This one is again 9x, so at total 9x9 = 81 of these
        <TextBox> // Same as above, each one as a child of the Border
        .....
        </TextBox>
      </Border>
    </UniformGrid>
</UniformGrid>

So, having that many number of controls, I was wondering what solution is more elegant and proper:

1: XAML

So the whole design is done in XAML, which is quite some writing, and all the controls are manually setup there

2: C# Code

So just the main UniformGrid and the 9 smaller Uniform grids are created using XAML, then all the other stuff is created dynamically, written in C#. Also if this would be the choice, then please show me the way how to add a child to a border from the code side. As for now, this was my main way of going and I came up with that:

    private void InitializeCells()
    {
        for (int i = 1; i <= 9; i++)
        {
            object foundControl = sudokuGrid.FindName("cellBorder" + i.ToString());
            Border foundGridControl = (Border)foundControl;
            for (int j = 1; j <= 9; j++)
            {
                TextBox cell = new TextBox();
                cell.MaxLength = 1;
                cell.FontSize = 30;
                cell.Name = "cell" + j.ToString();
                cell.VerticalContentAlignment = VerticalAlignment.Center;
                cell.HorizontalContentAlignment = HorizontalAlignment.Center;
                // HOW TO ADD CHILDREN????
            }
        }
    }
    private void InitializeCellBorders()
    {
        for (int i = 1; i <= 9; i++)
        {
            object foundControl = sudokuGrid.FindName("block" + i.ToString());
            UniformGrid foundGridControl = (UniformGrid)foundControl;
            for (int j = 1; j <= 9; j++)
            {
                Border cellBorder = new Border();
                cellBorder.Name = "cellBorder" + j.ToString();
                cellBorder.BorderBrush = Brushes.DodgerBlue;
                foundGridControl.Children.Add(cellBorder);
            }
        }
    }

3: Mixture

Some kind of different mixture of C# and XAML code, which I havent come up with yet :).


回答1:


None of them. Use an ItemsControl or one of it's derivatives:

<ItemsControl ItemsSource="{Binding SomeCollectionOfViewModel}">
   <ItemsControl.ItemsPanel>
      <ItemsPanelTemplate>
         <UniformGrid/>
      </ItemsPanelTemplate>
   </ItemsControl.ItemsPanel>
   <ItemsControl.ItemTemplate>
       <DataTemplate>
          <ItemsControl ItemsSource="{Binding SomeCollection}">   <!-- Nested Content -->
              ...
       </DataTemplate>
   <ItemsControl.ItemTemplate>
</ItemsControl>


来源:https://stackoverflow.com/questions/17392546/nested-controls-structure-in-xaml-or-c

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!