如何使 WPF wrappanel 子项拉伸? [英] How to make WPF wrappanel child items to stretch?

查看:37
本文介绍了如何使 WPF wrappanel 子项拉伸?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想创建一个 ItemsControl,其中子项像 WrapPanel 一样放置在其中,但是子项应该尽可能多地占用空间.这样当窗口大小变大或变小时,子项应该按照一定的宽高比进行拉伸.当从 ItemsControlItemsSource 中添加或删除子项时,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 wrappanel 子项拉伸?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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