根据列表框尺寸调整列表框内容的大小 [英] Resizing Listbox Contents according to Listbox dimensions

查看:95
本文介绍了根据列表框尺寸调整列表框内容的大小的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试根据列表框本身调整列表框内容的大小.这是在WPF中完成的.

I am attempting to resize the contents of my listbox according to the listbox itself. This is being done in WPF.

关于这怎么可能的任何想法?

Any ideas on how this might be possible?

推荐答案

我假设当您说调整大小"时,意味着要在两个方向上拉伸项目.要获取默认的ListBox并水平拉伸项目,您需要做的是:

I assume when you say "resize" you mean that you want to stretch the items in both directions. To take a default ListBox and stretch the items horizontally all you need is:

<ListBox HorizontalContentAlignment="Stretch"/>

默认值为左",因此所有ListBoxItem最终都被推到左侧,并根据其内容单独调整大小.

The default is Left so all the ListBoxItems end up pushed to the left and sized individually based on their content.

垂直拉伸需要摆脱用于布局项目的StackPanel,因为它没有沿方向"调整其子级大小的概念.最简单的方法是使用UniformGrid,但您可能希望根据自定义项目相对于彼此的大小来进行更多自定义.您还需要对VerticalContentAlignment设置(默认情况下为Center)执行相同的操作.因此,这是一种可以双向拉伸物品的物品:

Vertical stretching requires getting rid of the StackPanel used to do layout for the items because it has no concept of resizing its children in the direction of Orientation. The simplest thing to use is a UniformGrid but you might want something more custom depending on how you want the items to size relative to each other. You'll also need to do the same thing with the VerticalContentAlignment setting (Center by default). So here's one that will stretch items both ways:

<ListBox HorizontalContentAlignment="Stretch" VerticalContentAlignment="Stretch">
    <ListBox.ItemsPanel>
        <ItemsPanelTemplate>
            <UniformGrid Columns="1"/>
        </ItemsPanelTemplate>
    </ListBox.ItemsPanel>
</ListBox>

这篇关于根据列表框尺寸调整列表框内容的大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆