如何使DockPanel中填充可用空间 [英] How to make DockPanel fill available space

查看:651
本文介绍了如何使DockPanel中填充可用空间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想要的购物车中的的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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆