title: WPF C# Get array position of grid button from Click Event
body: <p>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.</p>
  
  <p>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.</p>
  
  <p>Does anyone know how I can get the array position (preferably row and column) from the setup that I have?</p>
  
  <p>XAML Below, the C# click events follow it.</p>
  
  <pre><code>&lt;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"&gt;
      &lt;Window.Resources&gt;
          &lt;DataTemplate x:Key="Buttons_Template"&gt;
              &lt;Button Content="" 
                      Height="20" 
                      Width="20" 
                      Margin="0,0,0,0" 
                      Visibility="Visible" 
                      Click="ButtonClick" 
                      MouseRightButtonUp="RightClick"/&gt;
          &lt;/DataTemplate&gt;
          &lt;DataTemplate x:Key="DataTemplate_2"&gt;
              &lt;TextBlock Text="{Binding}" 
                             Height="20" 
                             Width="20" 
                             Margin="0,0,0,0"
                             FontFamily="Rockwell"
                             HorizontalAlignment="Center"
                             VerticalAlignment="Center"
                             Padding="5"/&gt;
          &lt;/DataTemplate&gt;
          &lt;DataTemplate x:Key="DataTemplate_1"&gt;
              &lt;ItemsControl ItemsSource="{Binding}" ItemTemplate="{DynamicResource DataTemplate_2}"&gt;
                  &lt;ItemsControl.ItemsPanel&gt;
                      &lt;ItemsPanelTemplate&gt;
                          &lt;StackPanel Orientation="Horizontal"/&gt;
                      &lt;/ItemsPanelTemplate&gt;
                  &lt;/ItemsControl.ItemsPanel&gt;
              &lt;/ItemsControl&gt;
          &lt;/DataTemplate&gt;
          &lt;DataTemplate x:Key="Buttons_Template2"&gt;
              &lt;ItemsControl ItemsSource="{Binding}" ItemTemplate="{DynamicResource Buttons_Template}"&gt;
                  &lt;ItemsControl.ItemsPanel&gt;
                      &lt;ItemsPanelTemplate&gt;
                          &lt;StackPanel Orientation="Horizontal"/&gt;
                      &lt;/ItemsPanelTemplate&gt;
                  &lt;/ItemsControl.ItemsPanel&gt;
              &lt;/ItemsControl&gt;
          &lt;/DataTemplate&gt;
      &lt;/Window.Resources&gt;
      &lt;Grid&gt;
          &lt;Grid.RowDefinitions&gt;
              &lt;RowDefinition Height="Auto"/&gt;
              &lt;RowDefinition Height="Auto"/&gt;
              &lt;RowDefinition Height="*"/&gt;
          &lt;/Grid.RowDefinitions&gt;
          &lt;Grid&gt;
              &lt;TextBlock x:Name="RemainingMines" HorizontalAlignment="Left" /&gt;
              &lt;TextBlock x:Name="Difficulty" HorizontalAlignment="Center" /&gt;
              &lt;TextBlock x:Name="Timer" HorizontalAlignment="Right" /&gt;
          &lt;/Grid&gt;
          &lt;Grid Name="ResetButton" Grid.Row="1"&gt;
              &lt;Button Name="Reset" Content="Reset"/&gt;
          &lt;/Grid&gt;
          &lt;Grid Name="GridBoard" ShowGridLines="True" Grid.Row="2"&gt;
              &lt;ItemsControl x:Name="GridItems" ItemTemplate="{DynamicResource DataTemplate_1}"/&gt;
          &lt;/Grid&gt;
          &lt;Grid Name="ButtonsBoard" ShowGridLines="True" Grid.Row="2"&gt;
              &lt;ItemsControl x:Name="ButtonItems" ItemTemplate="{DynamicResource Buttons_Template2}"/&gt;
          &lt;/Grid&gt;
      &lt;/Grid&gt;
  &lt;/Window&gt;
  </code></pre>
  
  <p>Click Methods in C#</p>
  
  <pre><code>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
  }
  </code></pre>
  
  <p>Any help would be appreciated on this.</p>