0

I have a window that I'm basically building ghetto minesweeper in. I have a grid that I feed a jagged array into, set up so that it will adapt to any change in the size of the array (no hard set values or rows/columns). Over top of that, I have a grid of blank buttons that simply adapts in size to the grid below.

When you click a button, it hides revealing the value under it, and I need some way to return the position of the button clicked, so I can compare against the original jagged array to find out whether not the item was a bomb. (this would also help me for doing a fill action on empty tiles). But given how I have it set up, the Grid.GetRow or Grid.GetColumn just return 0's.

Does anyone know how I can get the array position (preferably row and column) from the setup that I have?

XAML Below, the C# click events follow it.

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

Click Methods in C#

private void ButtonClick(object sender, RoutedEventArgs e)
{
    int col = Grid.GetColumn((Button)sender); //this just returns 0
    int row = Grid.GetRow((Button)sender);  //this just returns 0
    Button source = e.Source as Button;
    source.Visibility = Visibility.Hidden;        
    Console.WriteLine("L: {0} x {1}", col, row);  //prints L: 0 x 0
}
private void RightClick(object sender, RoutedEventArgs e)
{
    int col = Grid.GetColumn((Button)sender);  //this just returns 0
    int row = Grid.GetRow((Button)sender);  //this just returns 0
    Button source = e.Source as Button;
    if(source.Content.Equals(""))
    {
        source.Content = "\u2691";
    }
    else
    {
        source.Content = "";
    }
    Console.WriteLine("R: {0} x {1}", col, row);  //prints R: 0 x 0
}

Any help would be appreciated on this.

user3066571
  • 1,381
  • 4
  • 14
  • 37

1 Answers1

0

You need to use appropriate control for buttons to host. In your xaml you are using items control inside Grid. But Items control do not have row and column index. Thats why you are not able to get the index. Use UniformGrid or some relevant control.

You can refer this article

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

Will help you a lot in other assignments as well

Community
  • 1
  • 1
Ramankingdom
  • 1,478
  • 2
  • 12
  • 17