Align WPF Controls
I have an MVVM WPF application with itemscontrol. The number of horizontal children is affected by the width of the elements. There is only one thing I haven't decided. I am trying to align children in such a way that they are always centered. I took quick pictures in paint to demonstrate what I mean.
How is it now:
If the width strengthens further, the fourth element will be added horizontally. This function should remain.
How I would like to see this:
If there is enough space for the fourth item, add it. I think the answer could be a simple XAML property. Anyone have an idea?
+3
source to share
1 answer
Place an ItemsCetrol in the grid and set it 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>
+4
source to share