拉伸ListBoxItem的Border的宽度

时间:2012-10-27 09:29:22

标签: c# wpf xaml

我写了一些名为Employe的课程。 Employees集合我设置为ListBox WPF控件的源。我为ItemTemplate编写了这样的模板:

<ResourceDictionary>
    <DataTemplate x:Key="tmpEmploye">
        <Border BorderThickness="3" BorderBrush="Gray" CornerRadius="5" 
                HorizontalAlignment="Stretch" VerticalAlignment="Stretch">
            <StackPanel Orientation="Vertical">
                <StackPanel Orientation="Horizontal">
                    <TextBlock Text="{Binding Path=Surname}" 
                               HorizontalAlignment="Stretch" Margin="2" 
                               FontWeight="Bold" TextWrapping="Wrap"/>
                    <TextBlock Text="{Binding Path=Name}" 
                               HorizontalAlignment="Stretch" Margin="2"/>
                    <TextBlock Text="{Binding Path=Patronymic}" 
                               HorizontalAlignment="Stretch" Margin="2" 
                               TextWrapping="Wrap"/>
                </StackPanel>
                <TextBlock Text="{Binding Path=Post}" Foreground="Gray" 
                           HorizontalAlignment="Stretch" Margin="2" 
                           FontStyle="Italic" TextWrapping="Wrap"/>
            </StackPanel>
        </Border>                               
    </DataTemplate>            
</ResourceDictionary>

每个项目都有边框。必须根据ListBox的宽度扩展边框。我为边框设置了HorizontalAlignment="Stretch",但它并没有像我想要的那样发生。

enter image description here

我该如何纠正?

1 个答案:

答案 0 :(得分:4)

试试这个:

<ListBox Name="lbEmployees" ItemTemplate="{StaticResource tmpEmploye}"
                 HorizontalContentAlignment="Stretch"
                 />