How to make WPF components take up all the free space?

I have an application that repeatedly switches between usercontrols, I want them to stretch to the maximum window size, but something prevented them from doing this, taking at least a minimum amount of space.

I noticed that my components inside usercontrols are changing the way they should (i.e. the buttons locked at the bottom of the control remain in place, the control simply does not reach the bottom of the window where the buttons should end up) I did not set any size-related property properties (I tried several, but none of them had the desired effect). Therefore, I assume that the problem is with the main component that displays the controls. It is configured as follows:

<Window.Resources> <DataTemplate DataType="{x:Type vm:MessageViewModel}"> <vw:MessageView/> </DataTemplate> <DataTemplate DataType="{x:Type vm:ConnectionsViewModel}"> <vw:ConnectionView/> </DataTemplate> ... </Window.Resources> <Grid> <ItemsControl ItemsSource="{Binding ViewModels}" Margin="0,20,0,0"/> ... </Grid> 

The solution I used thanks to Erno:

Change

 <ItemsControl ItemsSource="{Binding ViewModels}" Margin="0,20,0,0"/> 

to

 <ItemsControl ItemsSource="{Binding ViewModels}" Margin="0,20,0,0"> <ItemsControl.ItemsPanel> <ItemsPanelTemplate> <Grid/> </ItemsPanelTemplate> </ItemsControl.ItemsPanel> </ItemsControl> 
+4
source share
1 answer

The ItemsControl element places items in an ItemTemplate and places them in an ItemsPanel.

The ItemsPanel can be customized by setting the ItemsPanelTemplate.

By default, the ItemsPanel is a StackPanel (vertical), and StackPanels DO NOT spread lines / stretch items vertically.

So what you can do is set another control in ItemsPanelTemplate, which stretches and aligns the elements as you wish.

+7
source

Source: https://habr.com/ru/post/1413164/


All Articles