Reputation: 26159
I have create textblocks in my grid in my wpf application. I know how to create the click event. But I'm not sure how to get properties from that cell. The properties I want Grid.Row and Grid.Column. How can I do this?
<Window x:Class="TicTacToe.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Tic-Tac-Toe" Height="356" Width="475">
<Grid VerticalAlignment="Top" ShowGridLines="True" Height="313" Margin="10,10,2,0">
<Grid.ColumnDefinitions>
<ColumnDefinition />
<ColumnDefinition />
<ColumnDefinition />
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition />
<RowDefinition />
<RowDefinition />
</Grid.RowDefinitions>
<TextBlock Grid.Row="0" Grid.Column="0" Text="o" TextAlignment="Center" FontSize="72" FontFamily="Lucida Bright" FontWeight="Bold"></TextBlock>
<TextBlock Grid.Row="0" Grid.Column="1" MouseLeftButtonDown="ChoosePosition" ></TextBlock>
<TextBlock Grid.Row="0" Grid.Column="2" ></TextBlock>
<TextBlock Grid.Row="1" Grid.Column="0" ></TextBlock>
<TextBlock Grid.Row="1" Grid.Column="1" ></TextBlock>
<TextBlock Grid.Row="1" Grid.Column="2" ></TextBlock>
<TextBlock Grid.Row="2" Grid.Column="0" ></TextBlock>
<TextBlock Grid.Row="2" Grid.Column="1" ></TextBlock>
<TextBlock Grid.Row="2" Grid.Column="2" ></TextBlock>
</Grid>
</Window>
private void ChoosePosition(object sender, MouseButtonEventArgs e)
{
}
Upvotes: 2
Views: 8197
Reputation: 1290
As Grid.Row and Grid.Column are attached properties from the Grid class, you can get them using this syntax:
int row = Grid.GetRow(myTextBox);
int column = Grid.GetColumn(myTextBox);
In your case, you can cast the sender argument in the Click handler, so it would look like this:
var myTextBox = sender as TextBox;
if(myTextBox != null) {
int row = Grid.GetRow(myTextBox);
int column = Grid.GetColumn(myTextBox);
}
Upvotes: 5
Reputation: 317
Have you checked the sender
parameter? That will give you a reference to the textbox object which might be all you need depending on what you are trying to do.
Upvotes: 0