Resizing Listbox Contents according to Listbox dimensions

左心房为你撑大大i 提交于 2019-12-10 17:45:03

问题


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?


回答1:


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"/>

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

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>


来源:https://stackoverflow.com/questions/2323063/resizing-listbox-contents-according-to-listbox-dimensions

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!