我有一个已分配给 Grid.Column 0 的 ListBox 控件,该控件的宽度定义为“*”值,但是在渲染时有大量空间未使用。
我还注意到 ListBox 控件本身周围有某种边框,但我没有在标记中添加边框。
我的用户界面(以红色标记的关注区域):
我的标记:
<Window.Resources>
<DataTemplate x:Key="GameImagesTemplate" >
<StackPanel Orientation="Vertical">
<Image Source="{Binding FileInfo.FullName}" Margin="8,8,8,8" Height="70" Width="70" />
<Label Content="{Binding Name}" Width="70" />
</StackPanel>
</DataTemplate>
<DataTemplate x:Key="GameTemplate">
<StackPanel>
<Label Content="{Binding Name}" Background="Gray" FontSize="16" />
<ListBox x:Name="imageContent" ItemsSource="{Binding FileList}" ItemTemplate="{StaticResource GameImagesTemplate}" ScrollViewer.HorizontalScrollBarVisibility="Disabled" >
<ListBox.ItemsPanel>
<ItemsPanelTemplate>
<WrapPanel Orientation="Horizontal" IsItemsHost="True" />
</ItemsPanelTemplate>
</ListBox.ItemsPanel>
</ListBox>
</StackPanel>
</DataTemplate>
</Window.Resources>
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*"/>
<ColumnDefinition Width="Auto"/>
</Grid.ColumnDefinitions>
<ListBox x:Name="ContentList" ItemTemplate="{StaticResource GameTemplate}" Grid.Column="0" ScrollViewer.HorizontalScrollBarVisibility="Disabled" />
<StackPanel Grid.Column="1" Background="DarkGray">
<Button Click="OnLoad">_Load</Button>
<Button Click="OnSave">_Save</Button>
<Button Click="OnAdd">_Add</Button>
<Button Click="OnDelete">_Delete</Button>
</StackPanel>
</Grid>
我将如何解决提出的这两个问题。这是 ListBox 控件的默认行为吗?
非常感谢
最佳答案
是的,这是默认行为。
在对齐的情况下,看起来每个 ListBoxItem
中都有一个 WrapPanel
,但它没有足够的空间在第 1 行放置另一个项目。剩余空间未使用,因为 ListBox
上的 HorizontalContentAlignment
设置默认为 Left。此设置又由默认的 ListBoxItem
绑定(bind)。在 ListBox
上设置 HorizontalContentAlignment="Stretch"
应该可以解决这个问题。
外边框来自 BorderBrush
和 BorderThickness
的默认设置。设置 BorderThickness="0"
将完全消除它。
还有一些其他默认的填充
设置,可以在默认样式和模板中添加一些间距。如果您想了解更多信息,请在 Blend 中将 ListBox
添加到项目中,并复制其默认的 Template
和 ItemContainerStyle
并查看XAML。如果您不需要选择行为,还可以考虑使用基本 ItemsControl
,因为它没有任何此类默认设置。
关于c# - ListBox 不使用 Grid.Column 中的所有可用空间 - WPF,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14820941/