如何使DockPanel中填充可用空间 [英] How to make DockPanel fill available space
问题描述
我想要的购物车中的的ItemsControl(列表框)的含量
。要做到这一点,我已经创建了以下的DataTemplate
:
I'm trying the content of a shopping cart in an ItemsControl(ListBox)
. To do so, I've created the following 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>
在该项目的显示在我的购物车但是,名称和价格的TextBlocks
坐在旁边彼此,上有一个非常大的量空白右手边。
When the items are displayed in my shopping cart however, the Name and Price TextBlocks
are sitting right beside one another, and there is an extremely large amount of whitespace on the right hand side.
想知道什么给力的最好方法 DockPanel中
来拉伸以填充全部由列表项
是?
Was wondering what the best method to force the DockPanel
to stretch to fill all the space made available by the ListItem
was?
推荐答案
绑定 DockPanel中
的宽度
来在 ActualWidth的
的 ListBoxItem的
:
<DockPanel Width="{Binding ActualWidth, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type ListBoxItem}}}">
...
另一种选择:你可以只是重新定义 ItemContainerStyle
,使 ListBoxItem的
的水平拉伸:
<ListBox.ItemContainerStyle>
<Style TargetType="ListBoxItem">
<Setter Property="HorizontalContentAlignment" Value="Stretch"/>
</Style>
</ListBox.ItemContainerStyle>
这篇关于如何使DockPanel中填充可用空间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!