c# - ListBox 不使用 Grid.Column 中的所有可用空间 - WPF

标签 c# wpf .net-4.0 listbox grid-layout

我有一个已分配给 Grid.Column 0 的 ListBox 控件,该控件的宽度定义为“*”值,但是在渲染时有大量空间未使用。

我还注意到 ListBox 控件本身周围有某种边框,但我没有在标记中添加边框。

我的用户界面(以红色标记的关注区域):

enter image description here

我的标记:

<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 上的 Horizo​​ntalContentAlignment 设置默认为 Left。此设置又由默认的 ListBoxItem 绑定(bind)。在 ListBox 上设置 Horizo​​ntalContentAlignment="Stretch" 应该可以解决这个问题。

外边框来自 BorderBrushBorderThickness 的默认设置。设置 BorderThickness="0" 将完全消除它。

还有一些其他默认的填充设置,可以在默认样式和模板中添加一些间距。如果您想了解更多信息,请在 Blend 中将 ListBox 添加到项目中,并复制其默认的 TemplateItemContainerStyle 并查看XAML。如果您不需要选择行为,还可以考虑使用基本 ItemsControl,因为它没有任何此类默认设置。

关于c# - ListBox 不使用 Grid.Column 中的所有可用空间 - WPF,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14820941/

相关文章:

c# - 未调用 ASP.NET 组合 selectedIndexChanged 事件

c# - 如何在使用 WCF 服务的 WPF 应用程序中集成 LDAP

c# - 检查特定网站上的大代理列表的最快方法是什么?

c# - 在运行时更改表达式委托(delegate)主体?

c# - 列表框滚动

c# - 在 C# 中使用 C++ DLL

wpf - WPF是否可以在XP和Vista上运行?

c# - 将 FlowDocument 转换为 PDF 的最佳方式是什么

c# - 我可以阻止 .net 4.0 对单引号进行编码吗?

c# - CheckedListBox 控件是否有 "DisplayMember"和 "ValueMember"之类的属性? C# 窗体