如何实现StackPanel和UniformGrid行为的混合?

And*_*aló 6 c# wpf xaml

我想将元素放在一个控件中,以便它们最初适合这样(如StackPanel水平方向):

在此输入图像描述

但是,当元素堆叠到它们不再适合屏幕时,我希望它们开始表现得像这样:

在此输入图像描述

UniformGrid:随着元素数量的增加,它们变得越来越小.

什么是最好的或任何方式来做到这一点?由于外部控件的大小是可变的(取决于用户的屏幕大小),我不能"破解"它使用一个或另一个组件,具体取决于元素的数量,因为我无法预测"打破"所需的数量屏幕.

Chr*_* W. 4

最简单/最快的路线,将其放入一个ViewBox设置中Stretch="Uniform"来为您完成,如下所示,只需添加/减去矩形,您就会得到这个概念...

<Viewbox Stretch="Uniform" MaxHeight="60" MaxWidth="200">
    <StackPanel Orientation="Horizontal">
         <Rectangle Fill="Red" Margin="5" Height="50" Width="50"/>
         <Rectangle Fill="Red" Margin="5" Height="50" Width="50"/>
         <Rectangle Fill="Red" Margin="5" Height="50" Width="50"/>
         <Rectangle Fill="Red" Margin="5" Height="50" Width="50"/>
         <Rectangle Fill="Red" Margin="5" Height="50" Width="50"/>
         <Rectangle Fill="Red" Margin="5" Height="50" Width="50"/>
         <Rectangle Fill="Red" Margin="5" Height="50" Width="50"/>
         <Rectangle Fill="Red" Margin="5" Height="50" Width="50"/>
         <Rectangle Fill="Red" Margin="5" Height="50" Width="50"/>
         <Rectangle Fill="Red" Margin="5" Height="50" Width="50"/>
     </StackPanel>
</Viewbox>
Run Code Online (Sandbox Code Playgroud)

希望这可以帮助。干杯