WPF C#从Click Event获取网格按钮的数组位置

时间:2016-11-21 23:17:08

标签: c# arrays wpf grid

我有一个窗口,我基本上是在建造贫民区的扫雷艇。我有一个网格,我将一个锯齿状阵列送入,设置好以便它适应阵列大小的任何变化(没有硬)设置值或行/列)。最重要的是,我有一个空白按钮网格,只需调整大小到下面的网格。

当你点击一个按钮时,它会隐藏显示它下面的值,我需要一些方法来返回点击按钮的位置,这样我就可以与原来的锯齿状阵列进行比较,看出这个项目是不是炸弹了。 (这也有助于我对空白瓷砖进行填充操作)。但鉴于我如何设置它,Grid.GetRow或Grid.GetColumn只返回0。

有没有人知道如何从我的设置中获取阵列位置(最好是行和列)?

XAML下面是C#点击事件。

ALTER TABLE `test`
    CHANGE COLUMN `test_b` `new_test_b` INT(11) NOT NULL AFTER `new_test_a`,
    CHANGE COLUMN `test_a` `new_test_a` INT(11) NOT NULL AUTO_INCREMENT FIRST;

单击C#中的方法

<Window x:Class="MinesweeperWPF.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="Minesweeper" SizeToContent="WidthAndHeight" Height="Auto" Width="Auto">
    <Window.Resources>
        <DataTemplate x:Key="Buttons_Template">
            <Button Content="" 
                    Height="20" 
                    Width="20" 
                    Margin="0,0,0,0" 
                    Visibility="Visible" 
                    Click="ButtonClick" 
                    MouseRightButtonUp="RightClick"/>
        </DataTemplate>
        <DataTemplate x:Key="DataTemplate_2">
            <TextBlock Text="{Binding}" 
                           Height="20" 
                           Width="20" 
                           Margin="0,0,0,0"
                           FontFamily="Rockwell"
                           HorizontalAlignment="Center"
                           VerticalAlignment="Center"
                           Padding="5"/>
        </DataTemplate>
        <DataTemplate x:Key="DataTemplate_1">
            <ItemsControl ItemsSource="{Binding}" ItemTemplate="{DynamicResource DataTemplate_2}">
                <ItemsControl.ItemsPanel>
                    <ItemsPanelTemplate>
                        <StackPanel Orientation="Horizontal"/>
                    </ItemsPanelTemplate>
                </ItemsControl.ItemsPanel>
            </ItemsControl>
        </DataTemplate>
        <DataTemplate x:Key="Buttons_Template2">
            <ItemsControl ItemsSource="{Binding}" ItemTemplate="{DynamicResource Buttons_Template}">
                <ItemsControl.ItemsPanel>
                    <ItemsPanelTemplate>
                        <StackPanel Orientation="Horizontal"/>
                    </ItemsPanelTemplate>
                </ItemsControl.ItemsPanel>
            </ItemsControl>
        </DataTemplate>
    </Window.Resources>
    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height="Auto"/>
            <RowDefinition Height="Auto"/>
            <RowDefinition Height="*"/>
        </Grid.RowDefinitions>
        <Grid>
            <TextBlock x:Name="RemainingMines" HorizontalAlignment="Left" />
            <TextBlock x:Name="Difficulty" HorizontalAlignment="Center" />
            <TextBlock x:Name="Timer" HorizontalAlignment="Right" />
        </Grid>
        <Grid Name="ResetButton" Grid.Row="1">
            <Button Name="Reset" Content="Reset"/>
        </Grid>
        <Grid Name="GridBoard" ShowGridLines="True" Grid.Row="2">
            <ItemsControl x:Name="GridItems" ItemTemplate="{DynamicResource DataTemplate_1}"/>
        </Grid>
        <Grid Name="ButtonsBoard" ShowGridLines="True" Grid.Row="2">
            <ItemsControl x:Name="ButtonItems" ItemTemplate="{DynamicResource Buttons_Template2}"/>
        </Grid>
    </Grid>
</Window>

任何帮助都会受到赞赏。

1 个答案:

答案 0 :(得分:0)

您需要对按钮使用适当的控件来托管。在您的xaml中,您正在使用Grid内的项目控件。但Items控件没有行索引和列索引。这就是为什么你无法获得索引。 使用UniformGrid或一些相关控件。

您可以参考这篇文章

https://stackoverflow.com/a/13588066/5273015

在其他任务中也会帮助你很多