Ant*_*yrd 2 c# wpf grid .net-4.5 visual-studio-2012
我在我的wpf应用程序中的网格中创建了文本块.我知道如何创建click事件.但我不知道如何从那个细胞获得属性.我想要Grid.Row和Grid.Column的属性.我怎样才能做到这一点?
<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)
{
}
Run Code Online (Sandbox Code Playgroud)
由于Grid.Row和Grid.Column是Grid类的附加属性,您可以使用以下语法获取它们:
int row = Grid.GetRow(myTextBox);
int column = Grid.GetColumn(myTextBox);
Run Code Online (Sandbox Code Playgroud)
在您的情况下,您可以在Click处理程序中转换sender参数,因此它看起来像这样:
var myTextBox = sender as TextBox;
if(myTextBox != null) {
int row = Grid.GetRow(myTextBox);
int column = Grid.GetColumn(myTextBox);
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
7479 次 |
| 最近记录: |