我是 WPF 的新手。我正在尝试使 OrderItemsTmpl 模板中的第一列拉伸到可用的最大宽度,但它不起作用,宽度只能达到内部文本的宽度。我不想使用绝对值。如何解决这个问题?谢谢
<DataTemplate x:Key="OrderItemsTmpl">
<Grid Background="Brown" HorizontalAlignment="Stretch" >
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*"></ColumnDefinition>
<ColumnDefinition Width="auto"></ColumnDefinition>
</Grid.ColumnDefinitions>
<StackPanel Grid.Column="0" HorizontalAlignment="Stretch">
<CheckBox Content="{Binding Path=sItemName}" HorizontalContentAlignment="Stretch" ></CheckBox>
<ListBox HorizontalContentAlignment="Stretch"
ItemsSource="{Binding Path=aSinglOptns}"
Margin="20,0,0,0"
ItemTemplate="{StaticResource SinglOptnTmpl}"
Style="{StaticResource SheetListStyle}"
ItemContainerStyle="{StaticResource ListBoxItemStyle}"
>
</ListBox>
</StackPanel>
<TextBlock Grid.Column="1" Text="{Binding Path=fQty}"></TextBlock>
</DataTemplate>
<Style x:Key="ListBoxItemStyle" TargetType="{x:Type ListBoxItem}">
<Setter Property="HorizontalContentAlignment" Value="Stretch"></Setter>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="ListBoxItem">
<ContentPresenter></ContentPresenter>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
<Style x:Key="SheetListStyle" TargetType="{x:Type ListBox}">
<Setter Property="Background" Value="Aqua"></Setter>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type ListBox}">
<Border
CornerRadius="8"
BorderThickness="2">
<ScrollViewer>
<WrapPanel
IsItemsHost="True"
Orientation="Vertical"
HorizontalAlignment="Left"/>
</ScrollViewer>
</Border>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
内容项目的列表框
<Grid Background="Pink" Grid.Row="1" >
<Grid.RowDefinitions>
<RowDefinition Height="auto"></RowDefinition>
<RowDefinition Height="*"></RowDefinition>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*"></ColumnDefinition>
</Grid.ColumnDefinitions>
<TextBlock HorizontalAlignment="Center" Text="{Binding Path=sDita}" Grid.Row="0" Grid.Column="0" TextWrapping="Wrap" ></TextBlock>
<ListBox
Grid.Row="1"
Grid.Column="0"
HorizontalContentAlignment="Stretch"
Background="Aqua"
ItemsSource="{Binding Path=aOrderItems}"
Name="OrderItems"
ItemTemplate="{StaticResource OrderItemsTmpl}"
Margin="0,0,0,0"
ScrollViewer.HorizontalScrollBarVisibility="Disabled"
ScrollViewer.VerticalScrollBarVisibility="Auto"
ScrollViewer.CanContentScroll="True"
ItemContainerStyle="{StaticResource ListBoxItemStyle}"
Style="{StaticResource SheetListStyle}">
</ListBox>
</Grid>
更新
我意识到因为<Style x:Key="SheetListStyle">
我使用的是 WrapPanel,即使你应用 HorizontalAlignment="Stretch" 它也不会拉伸到最大宽度我必须改用其他面板,但有人知道可以使用什么吗?