【问题标题】:Using uniformgrid to arrange buttons in a square使用uniformgrid将按钮排列成正方形
【发布时间】:2012-01-04 19:17:11
【问题描述】:

我正在编写这个儿童游戏(内存)并有一个瓷砖列表(列表),我将其绑定到包装面板内的项目控件。现在我有 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>

【问题讨论】:

    标签: wpf xaml layout itemscontrol


    【解决方案1】:

    您将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>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-04-02
      • 2010-09-25
      • 1970-01-01
      • 1970-01-01
      • 2013-11-07
      相关资源
      最近更新 更多