2011-06-15 91 views
8

我有一個WPF網格,它分爲3行和3列, 我無法找到一種方式獲取網上的鼠標點擊的行和列號,哦和如果可能的話這將是我的計劃,這部分將在代碼,而不是XAML, 更好,這是我簡單的網格:獲取網格單元格通過鼠標點擊

<Grid Name="GridCtrl" ShowGridLines="True"> 
    <Grid.RowDefinitions> 
     <RowDefinition Height="3*" /> 
     <RowDefinition Height="3*" /> 
     <RowDefinition Height="3*" /> 
    </Grid.RowDefinitions> 
    <Grid.ColumnDefinitions> 
     <ColumnDefinition Width="3*" /> 
     <ColumnDefinition Width="3*" /> 
     <ColumnDefinition Width="3*" /> 
    </Grid.ColumnDefinitions> 
    </Grid> 
+0

請詳細說明您的問題多一點... – Syeda 2011-06-16 06:24:01

回答

1

它在這裏回答:Ways to identify which cell was clicked on WPF Grid?

我從來沒有使用WPF Grid之前,雖然使用上面的鏈接作爲例子,但我認爲像這樣的事情應該這樣做:

添加到您的初始化方法:

this.GridCtrl.MouseDown += new MouseButtonEventHandler(GridCtrl_MouseDown); 

,並添加新的方法來處理事件:

private void GridCtrl_MouseDown(object sender, MouseButtonEventArgs e) 
{ 
    if (sender != null) 
    { 
     Grid _grid = sender as Grid; 
     int _row = (int)_grid.GetValue(Grid.RowProperty); 
     int _column = (int)_grid.GetValue(Grid.ColumnProperty); 
     MessageBox.Show(string.Format("Grid clicked at column {0}, row {1}", _column, _row)); 
    } 
} 
+0

_grid.GetValue (Grid.RowProperty);將獲得行_grid是在一個父網格,這是不是任何 – markmnl 2013-12-11 04:29:07

1

我用的是這樣的:

private void DataGrid1_MouseDoubleClick(object sender, MouseButtonEventArgs e) 
    { 
     // Check if the user double-clicked a grid row and not something else 
     if (e.OriginalSource == null) return; 
     var row = ItemsControl.ContainerFromElement((DataGrid)sender, e.OriginalSource as DependencyObject) as DataGridRow; 

     // If so, go ahead and do my thing 
     if (row != null) 
     { 
      var Item = (CLASS_YOU_USE_TO_BIND)DataGrid1.Items[row.GetIndex()]; 
//Here you can work with Item, it is now the object of class you used in 
//DataGrid.DataSource 
     } 
} 
+1

問題是關於一個「網格」而不是「DataGrid」 – markmnl 2013-12-11 04:29:36

7

面臨着同樣的問題我想出了這個解決方案:

XAML:

<Grid Name="myGrid" Background="Transparent" PreviewMouseLeftButtonDown="OnPreviewMouseLeftButtonDown"> 

注:Grid必須給予一個背景,以提高鼠標按下事件,請參閱:How to get a Grid to raise MouseDown events when no UIElemets in cells clicked?

代碼隱藏:

private void OnPreviewMouseLeftButtonDown(object sender, System.Windows.Input.MouseButtonEventArgs e) 
{ 
    if(e.ClickCount == 2) // for double-click, remove this condition if only want single click 
    { 
     var point = Mouse.GetPosition(myGrid); 

     int row = 0; 
     int col = 0; 
     double accumulatedHeight = 0.0; 
     double accumulatedWidth = 0.0; 

     // calc row mouse was over 
     foreach (var rowDefinition in myGrid.RowDefinitions) 
     { 
      accumulatedHeight += rowDefinition.ActualHeight; 
      if (accumulatedHeight >= point.Y) 
       break; 
      row++; 
     } 

     // calc col mouse was over 
     foreach (var columnDefinition in myGrid.ColumnDefinitions) 
     { 
      accumulatedWidth += columnDefinition.ActualWidth; 
      if (accumulatedWidth >= point.X) 
       break; 
      col++; 
     } 

     // row and col now correspond Grid's RowDefinition and ColumnDefinition mouse was 
     // over when double clicked! 
    } 
} 
-1

只是試試這個

Grid.GetRow(NAME OF GRID) 
Grid.GetColumn(NAME OF GRID)