这是一个包含简单列表的窗口:

代码很简单:
<Window x:Class="Wpf_List.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:sys="clr-namespace:System;assembly=mscorlib"
Title="MainWindow" Height="350" Width="525">
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*"/>
<ColumnDefinition Width="Auto"/>
<ColumnDefinition Width="*"/>
</Grid.ColumnDefinitions>
<Border BorderBrush="Black" BorderThickness="2" Grid.Column="1">
<ListBox>
<ListBox.ItemTemplate>
<DataTemplate>
<!--HorizontalAlignment="Stretch" below has no effect :(-->
<Border CornerRadius="5" BorderBrush="DarkRed" SnapsToDevicePixels="True" BorderThickness="1"
Margin="1" Padding="4,2"
HorizontalAlignment="Stretch"
>
<TextBlock Text="{Binding}"/>
</Border>
</DataTemplate>
</ListBox.ItemTemplate>
<sys:String>First</sys:String>
<sys:String>Second</sys:String>
<sys:String>Some item with a long name</sys:String>
</ListBox>
</Border>
</Grid>
</Window>
Run Code Online (Sandbox Code Playgroud)
每个项目的宽度不同.看起来像"自动".我尝试了各种各样的方法,但没有找到一个让物品水平拉伸而不用硬编码宽度.
我如何能?
arc*_*aut 14
ListBox有一个名为的财产HorizontalContentAlignment.我认为设置它Stretch会做到这一点.虽然如果您重新模板化ListBox和/或更改它,请ItemContainerStyle确保在必要时(通过TemplateBindings)应用此属性的值.
| 归档时间: |
|
| 查看次数: |
2370 次 |
| 最近记录: |