我正在编写这个儿童游戏(内存),并有一个瓦片列表(列表),它绑定到一个wrappanel内的项目控件。现在我有22块瓷砖,它们在中心排成两排。
我真正想要的是将它安排在屏幕中心的5x5矩阵中,因此它会随着瓷砖的数量而缩放。我无法正确显示瓷砖,使用均匀网格时,尺寸非常小,位于屏幕的左上角。当我设置列和行属性时,它会显示出来,就好像它在边界之外。有人可以帮忙吗?
XAML:
<Window x:Class="MemoryWPF.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525">
<Window.Resources>
<Style TargetType="Button" x:Key="TransparentButton">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="Button">
<Border Background="Transparent">
<ContentPresenter/>
</Border>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</Window.Resources>
<UniformGrid Columns="5" Rows="5">
<UniformGrid.Background>
<ImageBrush x:Name="backBrush"/>
</UniformGrid.Background>
<ItemsControl ItemsSource="{Binding Tiles}" VerticalAlignment="Center" HorizontalAlignment="center" Margin="100">
<ItemsControl.ItemTemplate>
<DataTemplate>
<Button Style="{StaticResource TransparentButton}" BorderThickness="0" Padding="-4" Command="{Binding TurnTileCommand}" Opacity="{Binding OpacityVal}" Margin="10">
<Image Width="150" Height="150" Source="{Binding ImageUri}"/>
</Button>
</DataTemplate>
</ItemsControl.ItemTemplate>
<ItemsControl.ItemsPanel>
<ItemsPanelTemplate>
<WrapPanel/>
</ItemsPanelTemplate>
</ItemsControl.ItemsPanel>
</ItemsControl>
<TextBlock Text="{Binding AmountTilesLeft}" VerticalAlignment="Bottom" FontSize="15"/>
</UniformGrid>
</Window>
答案 0 :(得分:18)
您将ItemsControl
放在UniformGrid
中(这就是控件太小的原因),但统一网格应该在 ItemsControl
内它的ItemsPanel
(通常是WrapPanel
)。
<ItemsControl ItemsSource="{Binding Tiles}" VerticalAlignment="Center" HorizontalAlignment="center" Margin="100">
<ItemsControl.ItemTemplate>
<DataTemplate>
<Button Style="{StaticResource TransparentButton}" BorderThickness="0" Padding="-4" Command="{Binding TurnTileCommand}" Opacity="{Binding OpacityVal}" Margin="10">
<Image Width="150" Height="150" Source="{Binding ImageUri}"/>
</Button>
</DataTemplate>
</ItemsControl.ItemTemplate>
<ItemsControl.ItemsPanel>
<ItemsPanelTemplate>
<UniformGrid Columns="5" Rows="5">
<UniformGrid.Background>
<ImageBrush x:Name="backBrush"/>
</UniformGrid.Background>
</UniformGrid>
</ItemsPanelTemplate>
</ItemsControl.ItemsPanel>
</ItemsControl>