Search code examples
wpfuniformgrid

In WPF, is it possible to specify the item's cell location within a UniformGrid panel?


I have designed the following WPF window:

<Window x:Class="Boris.Gui.Wpf.DragDropWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Width="286" Height="350" ResizeMode="CanMinimize" Title="Drag and Drop">

    <Window.Resources>

        <!-- Sorted matches list panel -->
        <ItemsPanelTemplate x:Key="SortedMatchesListPanel">
            <UniformGrid Rows="3" Columns="4" />
        </ItemsPanelTemplate>

    </Window.Resources>

    <Grid>

        <ListBox 
            Name="SortedMatchesListBox" 
            Height="180" 
            Margin="12,85,12,0" 
            HorizontalAlignment="Stretch" 
            VerticalAlignment="Top" 
            ItemsPanel="{StaticResource ResourceKey=SortedMatchesListPanel}" />

    </Grid>

</Window>

When run, the SortedMatchesListBox list is populated with the items, and it looks like this:

alt text

My question is: Is it possible to specify the cell location of the list items in the UniformGrid items panel? For example, is it possible to place the highlighted Team 2 vs Team 5 item underneath the Team 1 vs Team 5, i.e. move it to the next cell on the right, and to preserve the blank space where it currently resides? It should look like this:

alt text

Right now, I am using a trick of adding a blank item to the SortedMatchesListBox.ItemsSource to make it happen, but what I'd really like is to be able to specify the item's cell location in the items panel. Thanks for the help.


Solution

  • Yeah I guess that's how you could do it with a UniformGrid. It just takes in child elements and slots it into the next available position a 'n X n matrix'

    If you want exact cell locations use a Grid with uniform row and column widths, which allows you to specify exactly which cell a child element is placed.