如何使WPF数据模板填充列表框的整个宽度?

Eri*_*ins 61 wpf layout listbox datatemplate

我有一个ListBox DataTemplateWPF.我希望一个项目紧贴在左侧ListBox,另一个项目紧靠右侧,但我无法弄清楚如何做到这一点.

到目前为止,我有一个Grid有三列,左边和右边的列有内容,中心是占位符,宽度设置为"*".我哪里错了?

这是代码:

<DataTemplate x:Key="SmallCustomerListItem">
    <Grid HorizontalAlignment="Stretch">
        <Grid.RowDefinitions>
            <RowDefinition/>
        </Grid.RowDefinitions>
        <Grid.ColumnDefinitions>
            <ColumnDefinition/>
            <ColumnDefinition Width="*"/>
            <ColumnDefinition/>
        </Grid.ColumnDefinitions>
        <WrapPanel HorizontalAlignment="Stretch" Margin="0">
            <!--Some content here-->
            <TextBlock Text="{Binding Path=LastName}" TextWrapping="Wrap" FontSize="24"/>
            <TextBlock Text=", " TextWrapping="Wrap" FontSize="24"/>
            <TextBlock Text="{Binding Path=FirstName}" TextWrapping="Wrap" FontSize="24"/>

        </WrapPanel>
        <ListBox ItemsSource="{Binding Path=PhoneNumbers}" Grid.Column="2" d:DesignWidth="100" d:DesignHeight="50"
     Margin="8,0" Background="Transparent" BorderBrush="Transparent" IsHitTestVisible="False" HorizontalAlignment="Stretch"/>
    </Grid>
</DataTemplate>
Run Code Online (Sandbox Code Playgroud)

Eri*_*ins 140

我还必须设置:

HorizontalContentAlignment="Stretch"
Run Code Online (Sandbox Code Playgroud)

在包含ListBox.

  • 对Windows Phone用户的说明 - 这不起作用; 该属性被ListBox的ItemContainerStyle覆盖.为了使其有效,请参阅Gabriel Mongeon的答案:http://stackoverflow.com/questions/838828/how-to-get-a-listbox-itemtemplate-to-stretch-horizo​​ntally-the-full-width-of-the (7认同)
  • Ta pal.用Google搜索帮助,这是第一个链接.好一个 :) (5认同)

小智 24

<Grid.Width>
    <Binding Path="ActualWidth" 
             RelativeSource="{RelativeSource Mode=FindAncestor, AncestorType={x:Type ScrollContentPresenter}}" />
</Grid.Width>
Run Code Online (Sandbox Code Playgroud)