Xaml 在ItemsControl中拉伸按钮

Xaml 在ItemsControl中拉伸按钮,xaml,user-interface,uwp,Xaml,User Interface,Uwp,我有一组数据项,这些数据项应该在一个页面上以行的形式表示 <ItemsControl ItemsSource="{x:Bind ViewModel.PresetsSecondLine, Mode=OneWay}" Visibility="{x:Bind ViewModel.IsExpanded, Converter={StaticResource BooleanToVisibilityConverter}, Mode=OneWay}"

我有一组数据项,这些数据项应该在一个页面上以行的形式表示

<ItemsControl ItemsSource="{x:Bind ViewModel.PresetsSecondLine, Mode=OneWay}"
              Visibility="{x:Bind ViewModel.IsExpanded, Converter={StaticResource BooleanToVisibilityConverter}, Mode=OneWay}"
              VerticalAlignment="Stretch"
              HorizontalAlignment="Stretch"
              Margin="0 5">
    <ItemsControl.ItemsPanel>
        <ItemsPanelTemplate>
            <WrapGrid Orientation="Horizontal"
                      HorizontalChildrenAlignment="Stretch"
                      MaximumRowsOrColumns="4" />
        </ItemsPanelTemplate>
    </ItemsControl.ItemsPanel>

    <ItemsControl.ItemTemplate>
        <DataTemplate x:DataType="entities:Preset">
            <controls:PresetButtonControl Preset="{x:Bind}"
                                          VerticalAlignment="Stretch"
                                          Width="290"
                                          Margin="5"
                                          Style="{StaticResource DashboardButtonStyle}"
                                          HorizontalAlignment="Stretch" />
        </DataTemplate>
    </ItemsControl.ItemTemplate>

    <ItemsControl.ItemContainerStyle>
        <Style TargetType="ContentPresenter">
            <Setter Property="HorizontalAlignment" Value="Stretch"/>
        </Style>
    </ItemsControl.ItemContainerStyle>
</ItemsControl>

集合可能包含一行中显示的0到4个项目。如果有4项,则它们应填充整行。当项目少于4个时,应按比例显示:

我用
UniformGrid
找到了解决此类问题的方法,不幸的是
UniformGrid
在UWP中不再受支持。

1)第一个简单的解决方案是使用与UniformGrid非常相似的ViewBox。您可以按如下方式使用:

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

2) 或者可以选择VariableSizedWrapGrid,它是一个布局面板,支持按行和列排列子元素。每个子元素可以跨越多个行和列。您可以在MSDN上找到详细信息。下面是一个简单的例子

<VariableSizedWrapGrid Orientation="Horizontal" MaxWidth="150" >
       <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"/>
</VariableSizedWrapGrid >

实际上,对于自适应UI,还有更多其他方法。但上述两项似乎更为直截了当和简单

如果有任何不清楚的地方,请告诉我。

1)第一个简单的解决方案是使用与UniformGrid非常相似的ViewBox。您可以按如下方式使用:

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

2) 或者可以选择VariableSizedWrapGrid,它是一个布局面板,支持按行和列排列子元素。每个子元素可以跨越多个行和列。您可以在MSDN上找到详细信息。下面是一个简单的例子

<VariableSizedWrapGrid Orientation="Horizontal" MaxWidth="150" >
       <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"/>
</VariableSizedWrapGrid >

实际上,对于自适应UI,还有更多其他方法。但上述两项似乎更为直截了当和简单


如果有任何不清楚的地方,请告诉我。

如果我正确理解您的预期结果,您可以使用物品容器上显示的相同概念。如果我正确理解您的预期结果,您可以使用物品容器上显示的相同概念。