WPF Vertical WrapPanel stretch to fill space - wpf

I want to build a WrapPanel with some elements, e.g. labels, and I while they are all beneath on another, I want the to fill the entire horizontal space.
As far as I can tell, the WrapPanel fills the space itself, but the elements themselves don't.
Does anyone have an idea/ solution how I could achieve this? It should look this way.

use this Code And Replace your Elements with Labels.
<WrapPanel Orientation="Vertical">
<Label Background="Blue" Content="Toggle" FontWeight="Bold"/>
<Label Background="Red" Content="Toggle" FontWeight="Bold"/>
<Label Background="Brown" Content="Toggle" FontWeight="Bold"/>
<Label Background="WhiteSmoke" Content="Toggle" FontWeight="Bold"/>
<Label Background="Yellow" Content="Toggle" FontWeight="Bold"/>
</WrapPanel>
After Resizing
Before Resizing

Related

Adding two TextBlocks in one column of the WPF project

I want to add two TextBlocks in one column of my WPF project. I use the following codes but They locate on eachother now. How may I insert them after each other (not on each other)? Please note that I don't want to put them in two seperate columns.
<TextBlock Grid.Row="6" Grid.Column="1" x:Name="FirstName" Margin="10,10,0,0"/>
<TextBlock Grid.Row="6" Grid.Column="1" x:Name="LastName" Margin="10,10,0,0" Grid.ColumnSpan="2"/>
You can use a WrapPanel to host the TextBlocks horizontally:
<WrapPanel Grid.Row="6" Grid.Column="1" Grid.ColumnSpan="2">
<TextBlock x:Name="FirstName" Margin="10,10,0,0" Text="FirstName" />
<TextBlock x:Name="LastName" Margin="10,10,0,0" Text="LastName" />
</WrapPanel>

WPF. How align two labels has different size on baseline in xaml

I have problem with align two labels who has different size and are neighbors. I have to show variable as big size and units as small. But because of the different position of the font baseline, the texts are not located on the same line. Look at my xaml:
<StackPanel VerticalAlignment="Bottom" Orientation="Horizontal">
<Label Content="5" FontSize="70" FontWeight="DemiBold" Foreground="White" Padding="0" Margin="0" BorderBrush="White" BorderThickness="1" />
<Label Content="s" FontSize="14" Foreground="White" Padding="0" Margin="0" BorderBrush="White" BorderThickness="1" VerticalAlignment="Bottom" />
</StackPanel>
I see solution of this problem: I can pick up margin or padding, but it is not good decision. In addition, I can not remove the upper gap of a larger text. May be exist more elegant way to deside this problem. Please, show me this, if somebody know, or tell, that is isn't possible, and I will pick up margin or padding. I was looking for a solution for a long time. The most similar here: WPF: Aligning the base line of a Label and its TextBox
But my elements has different size and this way is bad for me.
Thanks for any help!
If you are not required to use Labels, you can achieve baseline alignment by using Runs inside a TextBlock.
<TextBlock VerticalAlignment="Bottom">
<Run Text="5" FontSize="70" FontWeight="DemiBold" Foreground="White" />
<Run Text="s" FontSize="14" Foreground="White" />
</TextBlock>
You should use that margins:
<StackPanel VerticalAlignment="Bottom" Orientation="Horizontal">
<Label Content="5" FontSize="70" FontWeight="DemiBold" Foreground="White" Padding="0" Margin="0" BorderBrush="White" BorderThickness="1" />
<Label Content="s" FontSize="14" Foreground="White" Padding="0" Margin="0 0 0 12" BorderBrush="White" BorderThickness="1" VerticalAlignment="Bottom" />
</StackPanel>

WPF: 2 different label sizes in particular order alignment

Please take a look at this picture:
I try to create same label with 2 sizes:
<Grid>
<Label Content="11" HorizontalAlignment="Center" VerticalAlignment="Center" FontSize="50" />
<Label Content="%" HorizontalAlignment="Right" FontSize="20"
VerticalAlignment="Top"/>
</Grid>
But currently this is not like the picture:
To have such a result, your grid must be really small. I see at least 3 solutions to your issue:
1- Make your grid a bit bigger until it fits.
2- Put 2 columns in your grid, you'd put the number on the left column and the % on the right columns.
3- You could use a StackPanel like this:
<Grid>
<StackPanel Orientation="Horizontal">
<Label Content="11" FontSize="50" Padding="0"/>
<Label Content="%" FontSize="20" VerticalAlignment="Top" Padding="0,10,5,5"/>
</StackPanel>
</Grid>
EDIT:
<Grid>
<StackPanel Orientation="Veritical">
<StackPanel Orientation="Horizontal">
<Label Content="11" FontSize="50" Padding="0"/>
<Label Content="%" FontSize="20" VerticalAlignment="Top" Padding="0,10,5,5"/>
</StackPanel>
<Label Content="Storage">
</StackPanel>
</Grid>

What XAML control should I use to keep all child elements within its borders in WPF?

I'm having a UI design issue, this is what I want the contents of my app window to shrink to fit the smallest dimension of the window.
Using the XAML below, when the window is too narrow the contents are shrunk to fit. Perfect. My problem is when the window is too short, the contents fall out the bottom, like this:
<ItemsControl ItemsSource="{Binding GroupStatsDisplayList}">
<ItemsControl.ItemsPanel>
<ItemsPanelTemplate>
<StackPanel Background="Red" Margin="5" />
</ItemsPanelTemplate>
</ItemsControl.ItemsPanel>
<ItemsControl.ItemTemplate>
<DataTemplate>
<Viewbox Margin="4" VerticalAlignment="Stretch" HorizontalAlignment="Stretch" Width="Auto" Height="Auto" >
<ListBox>
<StackPanel Orientation="Horizontal" Margin="5" Background="Blue">
<Label Content="{Binding GroupID}" FontWeight="Bold"/>
<Label Content="{Binding GroupName}" Width="100" FontWeight="Bold" />
<Label Content="{Binding CallsInQueue}" FontWeight="Bold" />
<Label Content="{Binding TSF}" FontWeight="Bold" />
</StackPanel>
</ListBox>
</Viewbox>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
I have read in answers to similar questions that the ItemsControl uses a StackPanel as its default itemspanel and that the StackPanel does behave the way I'm seeing. A grid has been recommended to overcome issues like mine.
So I tried telling my ItemsControl to use a Grid:
<ItemsControl.ItemsPanel>
<ItemsPanelTemplate>
<Grid Background="Red" Margin="5" />
</ItemsPanelTemplate>
</ItemsControl.ItemsPanel>
Better because the contents do resize when constrained in either direction. Worse because it seems like there is only one ViewBox->ListBox element that gets updated with the last item in my collection (I can see the three items in the collection cycle through the display as the app starts up). I don't see all items in my collection on screen, only the last one.
I also read that a DockPanel could save me...
<ItemsControl.ItemsPanel>
<ItemsPanelTemplate>
<DockPanel Background="Red" Margin="5" />
</ItemsPanelTemplate>
</ItemsControl.ItemsPanel>
That's when things got crazy and I decided to write this question. Now all items in my collection are there, but they appear one by one when I expand the window horizontally, each new item appears as the existing ones expand to the vertical extent of the window.
How do I get a layout that looks like the first image but will shrink to fit within the smallest dimension of the window?
Why do you have a ListBox in your DataTemplate, when you have only one hardcoded item the StackPanel? To clarify that: The ItemTemplate defines how you want one item to appear in your items Collection. For Example you could create an ItemTemplate which shows an Album Cover on the left, the Artist Name next to it and on the bottom a Star Rating.
A Grid should not be used as an ItemsPanel, because for that you would need to supply a dynamic Grid/Col Definitions collection.
Using a ViewBox is my best advice. But not in the ItemTemplate, this would only size one children, a viewbox around the whole itemscontrol.
This is the XAML that I used based on the answer from dowhilefor.
<Viewbox Margin="3" >
<ItemsControl ItemsSource="{Binding GroupStatsDisplayList}">
<ItemsControl.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal" Margin="1" Background="LightGreen">
<Label Content="{Binding GroupID}" FontWeight="Bold"/>
<Label Content="{Binding GroupName}" Width="100" FontWeight="Bold" />
<Label Content="{Binding CallsInQueue}" FontWeight="Bold" />
<Label Content="{Binding TSF}" FontWeight="Bold" />
</StackPanel>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
</Viewbox>
I haven't done WPF in a while, and probably it's not considered very good practice, but maybe you could just use a scaletransform on your entire gui ?
I always liked playing around with them.

Width of child control should match width of parent container

I'm pretty new to WPF. I often find my self struggling with getting a bunch of child controls combined width to match a given parent container.. As in the following:
<ListBox x:Name="BrugereListBox" Grid.Column="0" Grid.Row="3" DataContext="{DynamicResource Brugere}"
ItemsSource="{Binding}"
PreviewMouseLeftButtonDown="BrugereListBox_MouseLeftButtonDown"
PreviewMouseMove="BrugereListBox_PreviewMouseMove"
>
<ListBox.ItemTemplate >
<DataTemplate >
<Border BorderBrush="Black" BorderThickness="1" Margin="2">
<StackPanel Orientation="Vertical" IsEnabled="True"
PreviewMouseLeftButtonDown="StackPanel_PreviewMouseLeftButtonDown">
<StackPanel Orientation="Horizontal" >
<Label>Navn</Label>
<TextBox Text="{Binding Name}"></TextBox>
</StackPanel>
<StackPanel Orientation="Horizontal">
<Label>Password</Label>
<TextBox Text="{Binding Password}"></TextBox>
</StackPanel>
<StackPanel Orientation="Horizontal">
<Label>Email</Label>
<TextBox Text="{Binding Email}"></TextBox>
</StackPanel>
</StackPanel>
</Border>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
In this case I'm interested in getting the width of the children of the stackpanel:
<StackPanel Orientation="Horizontal" >
<Label>Navn</Label>
<TextBox Text="{Binding Name}"></TextBox>
</StackPanel>
to match the width of
<ListBox x:Name="BrugereListBox"
Any generic solution to scenarios like this?
turns out to be a dupe of:
wpf border control to span the width of listboxItem
and the answer given there:
This is probably more to do with the
ListBoxItems themselves not taking up
the full width of the ListBox. Add the
HorizontalContentAlignment="Stretch"
attribute to your ListBox and see if
it stretches the individual items to
fill the width.
so change to:
<ListBox x:Name="BrugereListBox" Grid.Column="0" Grid.Row="3" DataContext="{DynamicResource Brugere}"
ItemsSource="{Binding}"
PreviewMouseLeftButtonDown="BrugereListBox_MouseLeftButtonDown"
PreviewMouseMove="BrugereListBox_PreviewMouseMove"
HorizontalContentAlignment="Stretch"
>
but as Kent says, using a Grid would be better in my opinion.
Use the right panel and you'll be fine. StackPanel makes its own decisions about its width/height depending on its orientation and its children. Use a Grid and it will just take up the space it is given - no more, no less.

Resources