Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

WPF itemscontrol child alignment

I have a WPF MVVM application with an itemscontrol. The number of horizontal child items is influenced by the itemscontrol width. There is only 1 thing i haven't solved. I'm trying to align the child elements in a way that there are always centered. I have made quick pictures in paint to demonstrate what i mean.

How it's now: Itemscontrol image layout fault

If the width further inceares then a forth item will be added horizontally. This function needs to stay.

How I would like to see it: Itemscontrol image layout wish

If there is enough room for a forth item then it needs to be added. I'm thinking that the answer could be a simple XAML property. Anybody an idea?

like image 601
RolandMakkelie Avatar asked Apr 30 '15 18:04

RolandMakkelie


1 Answers

Put the ItemsControl in a Grid and set its HorizontalAlignment to Center:

<Grid>
    <ItemsControl ItemsSource="{Binding Images}" HorizontalAlignment="Center">
        <ItemsControl.ItemsPanel>
            <ItemsPanelTemplate>
                <WrapPanel Orientation="Horizontal"/>
            </ItemsPanelTemplate>
        </ItemsControl.ItemsPanel>
        <ItemsControl.ItemTemplate>
            <DataTemplate>
                <Image Source="{Binding}" .../>
            </DataTemplate>
        </ItemsControl.ItemTemplate>
    </ItemsControl>
</Grid>
like image 181
Clemens Avatar answered Oct 05 '22 23:10

Clemens