2012-12-02 294 views
2

我在我的wpf應用程序的網格中創建了文本塊。我知道如何創建點擊事件。但我不確定如何從該單元獲取屬性。我想要Grid.Row和Grid.Column的屬性。我怎樣才能做到這一點?WPF TextBlock Click Event

<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) 
     { 
     } 
+0

你必須把你列/行內textblox在網格 –

回答

5

由於Grid.Row和Grid.Column從網格類的附加屬性,你可以讓他們使用此語法:

int row = Grid.GetRow(myTextBox); 
int column = Grid.GetColumn(myTextBox); 

在你的情況,你可以投sender參數的Click處理程序,所以它看起來像這樣:

var myTextBox = sender as TextBox; 
if(myTextBox != null) { 
    int row = Grid.GetRow(myTextBox); 
    int column = Grid.GetColumn(myTextBox); 
} 
0

您是否檢查過sender參數?這會給你一個參考文本框對象,這可能是你所需要的全部,這取決於你正在嘗試做什麼。

0

只要改變文本框文本塊