Width of child control should match width of paren

2019-07-14 22:29发布

问题:

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?

回答1:

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.



回答2:

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.