WPF UniformGrid 动态行高

MyN*_*ame 5 c# wpf height uniformgrid

我编写了一个显示一些图像的小应用程序。为此,我使用了一个UniformGrid. 我的xaml代码:

<ScrollViewer>
  <ItemsControl ItemsSource="{Binding Images}">
    <ItemsControl.ItemsPanel>
      <ItemsPanelTemplate>
        <UniformGrid Name="unifomGrid" VirtualizingPanel.IsVirtualizing="True" VirtualizingPanel.VirtualizationMode="Recycling" VirtualizingPanel.IsVirtualizingWhenGrouping="True" IsItemsHost="True" Loaded="unifomGrid_Loaded" />
      </ItemsPanelTemplate>
    </ItemsControl.ItemsPanel>
    <ItemsControl.ItemTemplate>
      <DataTemplate>
        <materialDesign:Card Padding="32px" Margin="8px">
          <StackPanel>
            <StackPanel>
              <Image Source="{Binding Path, Mode=OneWay}" />
            </StackPanel>
          </StackPanel>
        </materialDesign:Card>
      </DataTemplate>
    </ItemsControl.ItemTemplate>
  </ItemsControl>
</ScrollViewer>
Run Code Online (Sandbox Code Playgroud)

结果如下:

在此输入图像描述

我的问题是,行高UniformGrid不是动态的。是否有可能每一行都有自己的行高?目前每行的高度是 内最高元素的高度UniformGrid

Vas*_*ash 0

这是一条不同的路线,您可以看看。它使用网格,并使用GridHelpers类来允许您执行所需的操作,因为每行的默认高度设置为“自动”。

 <ScrollViewer>
            <ItemsControl ItemsSource="{Binding Images}">
                <ItemsControl.Template>
                    <ControlTemplate>
                        <Grid
                            IsItemsHost="True"
                            Loaded="Grid_Loaded"
                            GridHelpers.RowCount="{Binding Path=Items.Count, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type ItemsControl}}}">
                        </Grid>
                    </ControlTemplate>
                </ItemsControl.Template>
                <ItemsControl.ItemTemplate>
                    <DataTemplate>
                        <materialDesign:Card Padding="32px" Margin="8px">
                            <StackPanel>
                                <StackPanel>
                                    <Image Source="{Binding Path, Mode=OneWay}" />
                                </StackPanel>
                            </StackPanel>
                        </materialDesign:Card>
                    </DataTemplate>
                </ItemsControl.ItemTemplate>
            </ItemsControl>
   </ScrollViewer>
Run Code Online (Sandbox Code Playgroud)