WPF ListBoxItem 不会拉伸到最大宽度
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18826404/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
WPF ListBoxItem does not stretch to the maximum width
提问by William
I am new to WPF. I am trying to make the first column in the OrderItemsTmpl template to stretch to the maximum width that is available, but it does not work, the width only up to the width of the text inside. I don't want to use an absolute value. How to solve this ? Thanks
我是 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>
ListBox that content the item
包含项目的列表框
<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>
UPDATEI realized because of <Style x:Key="SheetListStyle">I am using WrapPanel, it won't stretch to max width even you apply HorizontalAlignment="Stretch"
I have to use other panel instead, but anybody know what could be used ?
更新我意识到因为<Style x:Key="SheetListStyle">我使用的是 WrapPanel,它不会拉伸到最大宽度,即使你应用 HorizontalAlignment="Stretch" 我必须使用其他面板,但有人知道可以使用什么吗?
采纳答案by Eli Arbel
The problem is in the ListBox's control template. You're using a WrapPaneland you're aligning it to the left. Use a StackPaneland allow it to stretch (the default):
问题出在ListBox的控制模板中。您正在使用 aWrapPanel并将其与左侧对齐。使用 aStackPanel并允许它拉伸(默认):
<ControlTemplate TargetType="{x:Type ListBox}">
<Border
CornerRadius="8"
BorderThickness="2">
<ScrollViewer>
<StackPanel
IsItemsHost="True"
Orientation="Vertical" />
</ScrollViewer>
</Border>
</ControlTemplate>
Alternatively, you could use the ItemsPresentercontrol instead of the panel directly. ListBoxuses a VirtualizingStackPanelby default that would also give you virtualization (which you've disabled).
或者,您可以ItemsPresenter直接使用控件而不是面板。默认情况下ListBox使用一个VirtualizingStackPanel也可以为您提供虚拟化(您已禁用)。
回答by Sheridan
There is a property on most collection controls that makes a whole row selectable and I suspect that you mightneed to have it set. If you haven't set the HorizontalContentAlignmentproperty to Stretch, then thatis your problem.
大多数集合控件上都有一个属性可以选择整行,我怀疑您可能需要设置它。如果您尚未将该HorizontalContentAlignment属性设置为Stretch,那么这就是您的问题。
Ahhh... I've justnoticed that you didset this property.
啊...我刚刚注意到你确实设置了这个属性。
Ok, let's have another go... I can see that you are using a StackPanelinside your OrderItemsTmplDataTemplate. These controls don't have any effect on the Widthof their children, so I think that thatis your problem. Try changing it to another Gridcontrol instead.
好的,让我们再试一次...我可以看到您StackPanel在OrderItemsTmplDataTemplate. 这些控制Width对他们的孩子没有任何影响,所以我认为那是你的问题。尝试将其更改为另一个Grid控件。

