gpt4 book ai didi

wpf - 使用uniformgrid将按钮排列成正方形

转载 作者:行者123 更新时间:2023-12-04 16:29:49 26 4
gpt4 key购买 nike

我正在编写这个 child 游戏(内存)并有一个瓷砖列表(列表),我将其绑定(bind)到包装面板内的项目控件。现在我有 22 block 瓷砖,它们在中间排成两排。

我真正想要的是将它排列在屏幕中心的 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>

最佳答案

你把 ItemsControlUniformGrid (这就是控件如此之小的原因),但统一的网格应该在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>

关于wpf - 使用uniformgrid将按钮排列成正方形,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8243181/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com