wpf 子控件的宽度应与父容器的宽度相匹配
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2101582/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Width of child control should match width of parent container
提问by fsl
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:
我对 WPF 很陌生。我经常发现自己很难获得一堆子控件的组合宽度以匹配给定的父容器..如下所示:
<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?
此类场景的任何通用解决方案?
回答by Kent Boogaart
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.
使用正确的面板,你会没事的。StackPanel
根据其方向和子项自行决定其宽度/高度。使用 a Grid
,它只会占用它给定的空间 - 不多也不少。
回答by Alastair Pitts
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.
这可能更多地与 ListBoxItems 本身没有占用 ListBox 的整个宽度有关。将 HorizontalContentAlignment="Stretch" 属性添加到您的 ListBox 并查看它是否会拉伸单个项目以填充宽度。
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.
但正如肯特所说,在我看来使用网格会更好。