Width of child control should match width of parent container
controls, parent, width, wpf
Solution
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.
Problem
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?