How to create UniformGrid in XAML with autoscrolls after exceding rows limit by elements

1.2k views Asked by At

I would like to create UniformGrid which will contain WindowsFormsHosts(inside them are WinForms). 4 Hosts in every row(4 cols) and after exceding its' visible capacity new elements will be added underneath and user will be able to scroll the UniformGrid. I do not know even where to start beside defining UniFormGrid and its' column number.

 <UniformGrid x:Name="Grid"  Columns="4">

    </UniformGrid>

The behaviour I would like to get is just like in Windows' explorer if there are too many icons in the view you can scroll down.

I used UniformGrid because I needed equivalent of GridLayout() in Java. I want every added element to be the same size. I add elements from .cs.

1

There are 1 answers

2
Chris On

I'll just extend this out from the comments, depending on what size your WinForm controls are, and depending on what your desired resizing behaviour is, you might be able to get away with simply wrapping a WrapPanel in a ScrollViewer, e.g:

    <ScrollViewer>
        <WrapPanel>
            <Grid Width="100" Height="100" Background="Red"/>
            <Grid Width="100" Height="100" Background="Blue"/>
            <Grid Width="100" Height="100" Background="Yellow"/>
            <Grid Width="100" Height="100" Background="Red"/>
            <Grid Width="100" Height="100" Background="Blue"/>
            <Grid Width="100" Height="100" Background="Yellow"/>
            <Grid Width="100" Height="100" Background="Red"/>
            <Grid Width="100" Height="100" Background="Blue"/>
            <Grid Width="100" Height="100" Background="Yellow"/>
        </WrapPanel>
    </ScrollViewer>

enter image description here

For more flexibility, this answer links to a project that provides a UniformWrapPanel, and is worth a look.