Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Assuming you're designing a tic-tac-toe style game, and the grid in question is the standard WPF Grid, I'd imagine you'd be best putting a control in each separate cell. That way you can hook into mouse events for each, like so:</p> <pre><code>&lt;Window x:Class="WpfApplication1.Window1" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Title="Window1" Height="300" Width="300"&gt; &lt;Grid ShowGridLines="True"&gt; &lt;Grid.ColumnDefinitions&gt; &lt;ColumnDefinition /&gt; &lt;ColumnDefinition /&gt; &lt;ColumnDefinition /&gt; &lt;/Grid.ColumnDefinitions&gt; &lt;Grid.RowDefinitions&gt; &lt;RowDefinition /&gt; &lt;RowDefinition /&gt; &lt;RowDefinition /&gt; &lt;/Grid.RowDefinitions&gt; &lt;Button Grid.Column="0" Grid.Row="0" Click="ButtonClick" /&gt; &lt;Button Grid.Column="1" Grid.Row="0" Click="ButtonClick" /&gt; &lt;Button Grid.Column="2" Grid.Row="0" Click="ButtonClick" /&gt; &lt;Button Grid.Column="0" Grid.Row="1" Click="ButtonClick" /&gt; &lt;Button Grid.Column="1" Grid.Row="1" Click="ButtonClick" /&gt; &lt;Button Grid.Column="2" Grid.Row="1" Click="ButtonClick" /&gt; &lt;Button Grid.Column="0" Grid.Row="2" Click="ButtonClick" /&gt; &lt;Button Grid.Column="1" Grid.Row="2" Click="ButtonClick" /&gt; &lt;Button Grid.Column="2" Grid.Row="2" Click="ButtonClick" /&gt; &lt;/Grid&gt; &lt;/Window&gt; </code></pre> <p>And in your code-behind:</p> <pre><code>private void ButtonClick(object sender, RoutedEventArgs e) { Button _btn = sender as Button; int _row = (int)_btn.GetValue(Grid.RowProperty); int _column = (int)_btn.GetValue(Grid.ColumnProperty); MessageBox.Show(string.Format("Button clicked at column {0}, row {1}", _column, _row)); } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload