问题描述
我想创建一个ItemsControl
,其中子项像WrapPanel
那样放置,但是子项应占用尽可能多的空间.因此,当窗口大小变大或变小时,子项应根据一定的宽高比进行拉伸.当从ItemsControl
的ItemsSource
中添加或删除子项时,WrapPanel
应该在项目之间适当放置换行符,以保持子项的宽高比.
以下是我到目前为止的内容.是否可以在Xaml中执行此操作?还是应该为此创建自定义控件?预先感谢!
I'd like to create an ItemsControl
where child items are placed like a WrapPanel
, but child Items should take as much space as it can. So that when the window size gets larger or smaller, the child items should stretch according to a certain width:height ratio. When the child items get added or removed from the ItemsControl
's ItemsSource
, the WrapPanel
should place linebreaks among items appropriately to keep child item's width:height ratio.
Below is what I have so far. Is it possible to do this in Xaml? or should I create a custom control for this? Thanks in advance!
<Window>
<Grid>
<ItemsControl ItemsSource="{Binding DataCollection}">
<ItemsControl.ItemsPanel>
<ItemsPanelTemplate>
<WrapPanel Orientation="Vertical"/>
</ItemsPanelTemplate>
</ItemsControl.ItemsPanel>
<ItemsControl.ItemTemplate>
<DataTemplate>
<Border BorderBrush="Black" BorderThickness="1" HorizontalAlignment="Stretch">
<StackPanel Orientation="Horizontal" >
<TextBlock TextWrapping="Wrap" Text="{Binding Name}" />
<TextBlock TextWrapping="Wrap" Text="{Binding Value}"/>
<TextBlock TextWrapping="Wrap" Text="{Binding Time, StringFormat='hh:mm:ss' }"/>
</StackPanel>
</Border>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
</Grid>
</Window>
推荐答案
使用UniformGrid
而不是WrapPanel
.只需使用Columns
属性设置所需的列数,它就会为您提供所需的结果.
Use a UniformGrid
rather than a WrapPanel
. Just set the number of columns you want with the Columns
property, and it should give you the desired result.
这篇关于如何使WPF包装面板子项拉伸?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!