C# wpf方形可调整大小网格
我正在尝试创建一个照片库组件,该组件由C# wpf方形可调整大小网格,c#,wpf,C#,Wpf,我正在尝试创建一个照片库组件,该组件由ScrollViewer内部的网格组成。它应该可以调整大小,但照片单元必须保持方形,垂直间隙应与水平间隙相同。因此,我尝试将行大小绑定到列的实际大小: <ScrollViewer> <Grid ShowGridLines="True"> <Grid.ColumnDefinitions> <ColumnDefinition Name="column
ScrollViewer
内部的网格组成。它应该可以调整大小,但照片单元必须保持方形,垂直间隙应与水平间隙相同。因此,我尝试将行大小绑定到列的实际大小:
<ScrollViewer>
<Grid ShowGridLines="True">
<Grid.ColumnDefinitions>
<ColumnDefinition Name="column" Width="216*"/>
<ColumnDefinition Name="gap" Width="4*"/>
<ColumnDefinition Width="216*"/>
<ColumnDefinition Width="4*"/>
<ColumnDefinition Width="216*"/>
<ColumnDefinition Width="4*"/>
<ColumnDefinition Width="216*"/>
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="{Binding ElementName=column, Path=ActualWidth}"/>
<RowDefinition Height="{Binding ElementName=gap, Path=ActualWidth}"/>
<RowDefinition Height="{Binding ElementName=column, Path=ActualWidth}"/>
<RowDefinition Height="{Binding ElementName=gap, Path=ActualWidth}"/>
<RowDefinition Height="{Binding ElementName=column, Path=ActualWidth}"/>
<RowDefinition Height="{Binding ElementName=gap, Path=ActualWidth}"/>
<RowDefinition Height="{Binding ElementName=column, Path=ActualWidth}"/>
</Grid>
</ScrollViewer>
但是生成的网格看起来像
我做错了什么?我应该怎么做才能使单元格始终为正方形?我认为网格并不能很好地控制这一点,因为如果您编写照片库组件,则需要在调整大小后包装项目
将列表框与自定义样式一起使用的最佳方法(将WrapPanel用作ItemsHost元素)。
列表框:
ColumnDefinition的ActualWidth不是依赖项属性,因此绑定到它没有意义,因为该值始终为0
<ListBox
Style="{StaticResource PhotoListStyle}"
SelectionMode="Extended"
ItemsSource="{Binding}"
</ListBox>
<Style TargetType="{x:Type ListBox}" x:Key="PhotoListStyle">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type ListBox}" >
<WrapPanel Margin="5" IsItemsHost="True" Orientation="Horizontal"
ItemHeight="{Binding ElementName=32}"
ItemWidth="{Binding ElementName=32"
VerticalAlignment="Top" HorizontalAlignment="Stretch" />
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>