There is an array of certain objects in a two-dimensional array, for example 5x5 There is a layout in XAML consisting of a simple Grid, also with five columns and rows. TextBox is located in each cell. How to correctly associate a two-dimensional array object with a specific TextBox in the markup?

  • And what exactly is the type of your two-dimensional array? Really string[,] ? - VladD
  • no, these are instances of some class of the type MyObject [,] - Sublihim

1 answer 1

If your data structure really consists of nested lists (for example, List<List<T>> or there at the worst T[][] ), you can use the ItemsControl recursively. (If a really rectangular array of the form T[,] , look at the end of the answer.) The trick is that either the internal or external ItemsControl should position the elements horizontally.

With this XAML:

 <ItemsControl ItemsSource="{Binding}"> <ItemsControl.Resources> <BooleanToVisibilityConverter x:Key="BVC"/> </ItemsControl.Resources> <ItemsControl.ItemTemplate> <DataTemplate> <ItemsControl ItemsSource="{Binding}"> <ItemsControl.ItemsPanel> <ItemsPanelTemplate> <StackPanel IsItemsHost="True" Orientation="Horizontal"/> </ItemsPanelTemplate> </ItemsControl.ItemsPanel> <ItemsControl.ItemTemplate> <DataTemplate> <!-- здесь код представления вашего одного элемента --> <Grid Width="50" Height="50" Background="Red"> <Grid Visibility="{Binding Converter={StaticResource BVC}}" Background="Green"/> <TextBlock Text="{Binding}" HorizontalAlignment="Center" VerticalAlignment="Center"/> </Grid> </DataTemplate> </ItemsControl.ItemTemplate> </ItemsControl> </DataTemplate> </ItemsControl.ItemTemplate> </ItemsControl> 

and here is such a data structure as a DataContext :

 bool[][] items = new [] { new[] { true, false, false, false, true }, new[] { true, true, false, false, false }, new[] { true, false, true, false, false }, new[] { true, false, false, true, false }, new[] { true, true, true, true, true } }; 

you get this mapping:

true or false and you can't figure it out


If you really have a multidimensional array ( T[,] ), set such a wrapper as the DataContext :

 class TwoDimensionnalArrayWrapper<T> : IEnumerable<IEnumerable<T>> { T[,] array; public TwoDimensionnalArrayWrapper(T[,] array) { this.array = array; } public IEnumerator<IEnumerable<T>> GetEnumerator() { var w = array.GetLength(0); for (int i = 0; i < w; i++) yield return ColumnWrapper(i); } IEnumerable<T> ColumnWrapper(int i) { var h = array.GetLength(1); for (int j = 0; j < h; j++) yield return array[i, j]; } IEnumerator IEnumerable.GetEnumerator() => GetEnumerator(); } 
  • Thank! I myself began to think about ItemsControl, I just could not figure out how to use it, in this situation. It turns out - you can recursively. - Sublihim
  • @Sublihim: Please! Glad that helped. - VladD
  • Suddenly, there was another question for this template. The fact is that if you place in the DataTemplate, for example, Image in which to place a picture large enough, then this picture will appear in its real size. How, using this template, it is possible to make so that ItemsControl occupied the size in proportion to the window, and, accordingly, the pictures were also compressed? - Sublihim
  • @Sublihim: Strange, my picture doesn’t stretch if I leave the attributes on the Grid Width="50" Height="50" . As for how to make the field stretchable, it is worth asking a separate question, there is not enough space, and the topic does not quite relate to this issue. - VladD
  • Yes, if you set a limit, then there will be specified sizes. - Sublihim