.net 如何让 DockPanel 填充可用空间
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1080479/
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
How to make DockPanel fill available space
提问by Richard McGuire
I'm trying the content of a shopping cart in an ItemsControl(ListBox). To do so, I've created the following DataTemplate:
我正在尝试在ItemsControl(ListBox). 为此,我创建了以下内容DataTemplate:
<DataTemplate x:Key="Templates.ShoppingCartProduct"
DataType="{x:Type viewModel:ProductViewModel}">
<DockPanel HorizontalAlignment="Stretch">
<TextBlock DockPanel.Dock="Left"
Text="{Binding Path=Name}"
FontSize="10"
Foreground="Black" />
<TextBlock DockPanel.Dock="Right"
Text="{Binding Path=Price, StringFormat=\{0:C\}}"
FontSize="10"
Foreground="Black" />
</DockPanel>
</DataTemplate>
When the items are displayed in my shopping cart however, the Name and Price TextBlocksare sitting right beside one another, and there is an extremely large amount of whitespace on the right hand side.
然而,当商品显示在我的购物车中时,名称和价格TextBlocks并排排列,右侧有大量空白。
Was wondering what the best method to force the DockPanelto stretch to fill all the space made available by the ListItemwas?
想知道强制DockPanel拉伸以填充所有可用空间的最佳方法ListItem是什么?
回答by Thomas Levesque
Bind the Widthof the DockPanelto the ActualWidthof the ListBoxItem:
绑定Width的DockPanel到ActualWidth的ListBoxItem:
<DockPanel Width="{Binding ActualWidth, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type ListBoxItem}}}">
...
Another option: you could just redefine the ItemContainerStyleso that the ListBoxItemis stretched horizontally:
另一种选择:您可以重新定义ItemContainerStyle以便ListBoxItem水平拉伸:
<ListBox.ItemContainerStyle>
<Style TargetType="ListBoxItem">
<Setter Property="HorizontalContentAlignment" Value="Stretch"/>
</Style>
</ListBox.ItemContainerStyle>
回答by NielW
The nice thing about dock panels is they already fill all the available space. LastChildFill is true by default (but I set it below for clarity), so just don't set the DockPanel attribute on the last child, and it will fill the available space.
停靠面板的好处是它们已经填满了所有可用空间。LastChildFill 默认为 true(但为了清楚起见,我在下面设置了它),所以不要在最后一个孩子上设置 DockPanel 属性,它将填充可用空间。
<DockPanel HorizontalAlignment="Stretch" LastChildFill="true">
<TextBlock DockPanel.Dock="Left"
Text="{Binding Path=Name}"
FontSize="10"
Foreground="Black" />
<TextBlock
Text="{Binding Path=Price, StringFormat=\{0:C\}}"
FontSize="10"
Foreground="Black" />
</DockPanel>
回答by Sergey Aldoukhov
DockPanels are evil. Temptation to use StackPanel/DockPanelcombination for complex layouts leads to "layout dead ends". Use a Grid:
DockPanels是邪恶的。StackPanel/DockPanel对复杂布局使用组合的诱惑会导致“布局死胡同”。使用网格:
<Grid>
<TextBlock HorizontalAlignment="Left"
...
<TextBlock HorizontalAlignment="Right"
...
/Grid>
I use Grids almost exclusively, using a separate grid for each block of elements that "belong together"
我Grid几乎只使用s,为“属于一起”的每个元素块使用单独的网格

