79241851

Date: 2024-12-01 15:16:30
Score: 1
Natty:
Report link

As suggested in comments by @GerrySchmitz I have used a combination of UniformGrid and ItemsControl. Then Bind the multidimensional list using a simple FlattenListConverter. As the name suggests the converter will flatten the multidimensional list into list of items, this will be fed by ItemsControl to the UniformGrid and will be evenly distributed using the uniformity provided by UniformGrid, as previewed bellow.

View.xaml

<UserControl x:Class="Views.BoardView"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
             xmlns:local="clr-namespace:Views"
             xmlns:viewmodels="clr-namespace:ViewModels"
             xmlns:converters="clr-namespace:Converters"
             xmlns:core="clr-namespace:System;assembly=mscorlib"
             mc:Ignorable="d"
             Loaded="UserControl_Loaded"
             d:DesignHeight="400"
             d:DesignWidth="400"
             d:Background="{StaticResource BoardBackground}"
             d:DataContext="{d:DesignInstance Type=viewmodels:BoardViewModel}">
    <UserControl.Resources>
        <converters:FlattenListConverter x:Key="FlattenListConverter" />
    </UserControl.Resources>
    <Border Grid.Column="1"
            Background="{Binding Background, RelativeSource={RelativeSource Self}}"
            VerticalAlignment="Stretch"
            HorizontalAlignment="Stretch">
        <ItemsControl ItemsSource="{Binding Board, Converter={StaticResource FlattenListConverter}}">
            <ItemsControl.ItemsPanel>
                <ItemsPanelTemplate>
                    <UniformGrid x:Name="uniGridBoard"
                                 Background="Transparent">
                    </UniformGrid>
                </ItemsPanelTemplate>
            </ItemsControl.ItemsPanel>
        </ItemsControl>
    </Border>
</UserControl>

FlattenListConverter.cs

using BoardLogic;
using System;
using System.Collections.ObjectModel;
using System.Globalization;
using System.Windows.Data;
using System.Linq;

namespace Converters;

public class FlattenListConverter : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
    {
        if (value is not ObservableCollection<ObservableCollection<Cell>> items)
            return null!;
        return items.SelectMany(items => items);
    }

    public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
    {
        throw new NotImplementedException();
    }
}
Reasons:
  • Long answer (-1):
  • Has code block (-0.5):
  • User mentioned (1): @GerrySchmitz
  • Self-answer (0.5):
  • Low reputation (1):
Posted by: ycsvenom