ListBox 中的项目无法正确拉伸
检查这个 xaml:
<Window
x:Class="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"
Width="300">
<ListBox HorizontalContentAlignment="Stretch"
Grid.IsSharedSizeScope="True">
<ListBox.ItemsSource>
<x:Array Type="{x:Type sys:Int32}">
<sys:Int32>25</sys:Int32>
<sys:Int32>10</sys:Int32>
<sys:Int32>50</sys:Int32>
<sys:Int32>30</sys:Int32>
<sys:Int32>80</sys:Int32>
</x:Array>
</ListBox.ItemsSource>
<ListBox.ItemContainerStyle>
<Style TargetType="ListBoxItem"
BasedOn="{StaticResource {x:Type ListBoxItem}}">
<Setter Property="HorizontalContentAlignment" Value="Stretch" />
</Style>
</ListBox.ItemContainerStyle>
<ListBox.ItemTemplate>
<DataTemplate>
<Grid Background="#FFE41515">
<Grid.ColumnDefinitions>
<ColumnDefinition SharedSizeGroup="name" Width="Auto"/>
<ColumnDefinition SharedSizeGroup="value"/>
</Grid.ColumnDefinitions>
<TextBlock Text="Value:"/>
<ProgressBar
Value="{Binding Mode=OneWay}"
Grid.Column="1"
HorizontalAlignment="Stretch"
HorizontalContentAlignment="Stretch"/>
</Grid>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
</Window>
这是输出,请注意 ProgressBar
没有拉伸,为什么?
如果项目是红色的,则意味着每个项目都填充了整个宽度,那么为什么 ProgressBar
不拉伸??
Check this xaml:
<Window
x:Class="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"
Width="300">
<ListBox HorizontalContentAlignment="Stretch"
Grid.IsSharedSizeScope="True">
<ListBox.ItemsSource>
<x:Array Type="{x:Type sys:Int32}">
<sys:Int32>25</sys:Int32>
<sys:Int32>10</sys:Int32>
<sys:Int32>50</sys:Int32>
<sys:Int32>30</sys:Int32>
<sys:Int32>80</sys:Int32>
</x:Array>
</ListBox.ItemsSource>
<ListBox.ItemContainerStyle>
<Style TargetType="ListBoxItem"
BasedOn="{StaticResource {x:Type ListBoxItem}}">
<Setter Property="HorizontalContentAlignment" Value="Stretch" />
</Style>
</ListBox.ItemContainerStyle>
<ListBox.ItemTemplate>
<DataTemplate>
<Grid Background="#FFE41515">
<Grid.ColumnDefinitions>
<ColumnDefinition SharedSizeGroup="name" Width="Auto"/>
<ColumnDefinition SharedSizeGroup="value"/>
</Grid.ColumnDefinitions>
<TextBlock Text="Value:"/>
<ProgressBar
Value="{Binding Mode=OneWay}"
Grid.Column="1"
HorizontalAlignment="Stretch"
HorizontalContentAlignment="Stretch"/>
</Grid>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
</Window>
Here's the output, notice that the ProgressBar
isn't stretched, why?
If the items are red, it means that each item fills the entire width, then why isn't the ProgressBar
stretching??
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果您只有
SharedSizeGroups
,则其余空间将永远不会被占用,它们的行为始终类似于Width="Auto"
(大小到内容),但受到最大项目的限制。由于您只有两列,您甚至不需要两个这样的组,因为第二个组在每个项目中已经具有相同的宽度,对吧?这个怎么样:
If you have only
SharedSizeGroups
the rest space will never be taken, they always behave likeWidth="Auto"
(size to content) with the restriction of the largest item. As you only have two columns you do not even need two such groups as the second will already be the same width in every item, right?How about this instead: