我在 WPF 中有一个 ListBox
DataTemplate
。我希望一个项目紧靠 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>
最佳答案
我还必须设置:
HorizontalContentAlignment="Stretch"
在包含的ListBox
上。
关于wpf - 如何使 WPF 数据模板填充列表框的整个宽度?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/135375/