2013-07-22 96 views
0

我有一個很多單元格的網格,其中一些是空的。我想確定MouseDown事件發生時哪個單元格是鼠標。那怎麼可能?通過座標點擊網格中的哪個單元格?

+1

您可以使用'Mouse.DirectlyOver作爲FrameworkElement',但我敢肯定,您正在使用錯誤的方法開始。發佈相關代碼和XAML。 –

回答

1

要記住的是,有一個透明背景的控制不會產生它的透明區域活動的第一件事。爲所需網格設置顏色,或將其綁定到網格所在窗口的背景顏色,否則事件不會觸發。

此代碼示例演示了一種計算方法,用於在給定MouseMove事件的情況下確定網格元素位置。 ButtonClick事件參數非常相似。該樣本的相關方法是ColumnComputationRowComputation,它們在控制和列或行定義上進行線性分析。該示例在UI元素上運行。

Form類:

public partial class MainWindow : Window 
{ 
    public MainWindow() 
    { 
     InitializeComponent(); 
    } 



    private void InnerGrid_PreviewMouseMove(object sender, MouseEventArgs e) 
    { 
     this.XCoordinate.Text = e.GetPosition(InnerGrid).X.ToString(); 
     this.YCoordinate.Text = e.GetPosition(InnerGrid).Y.ToString(); 
     this.ColumnPosition.Text = ColumnComputation(InnerGrid.ColumnDefinitions, e.GetPosition(InnerGrid).X).ToString(); 
     this.RowPosition.Text = RowComputation(InnerGrid.RowDefinitions, e.GetPosition(InnerGrid).Y).ToString(); 

    } 

    private double ColumnComputation(ColumnDefinitionCollection c, double YPosition) 
    { 
     var columnLeft = 0.0; var columnCount = 0; 
     foreach (ColumnDefinition cd in c) 
     { 
      double actWidth = cd.ActualWidth; 
      if (YPosition >= columnLeft && YPosition < (actWidth + columnLeft)) return columnCount; 
      columnCount++; 
      columnLeft += cd.ActualWidth; 
     } 
     return (c.Count + 1); 
    } 
    private double RowComputation(RowDefinitionCollection r, double XPosition) 
    { 
     var rowTop = 0.0; var rowCount = 0; 
     foreach (RowDefinition rd in r) 
     { 
      double actHeight = rd.ActualHeight; 
      if (XPosition >= rowTop && XPosition < (actHeight + rowTop)) return rowCount; 
      rowCount++; 
      rowTop += rd.ActualHeight; 
     } 
     return (r.Count + 1); 
    } 
} 

XAML形式:

<Window x:Name="window" x:Class="GridHitTest.MainWindow" 
     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
     Title="MainWindow" Height="350" Width="525"> 
    <Grid Name="OuterBorder" > 
     <Grid.ColumnDefinitions> 
      <ColumnDefinition/> 
      <ColumnDefinition/> 
      <ColumnDefinition/> 
      <ColumnDefinition/> 
     </Grid.ColumnDefinitions> 
     <Grid.RowDefinitions> 
      <RowDefinition Height="20"/> 
      <RowDefinition Height="20"/> 
      <RowDefinition Height="*"/> 
     </Grid.RowDefinitions> 
     <TextBlock Grid.Column="0" HorizontalAlignment="Center" >X Coordinate</TextBlock> 
     <TextBlock Grid.Column="1" HorizontalAlignment="Center" >Y Coordinate</TextBlock> 
     <TextBlock Grid.Column="2" HorizontalAlignment="Center" >Column</TextBlock> 
     <TextBlock Grid.Column="3" HorizontalAlignment="Center" >Row</TextBlock> 
     <TextBlock Grid.Row="1" Grid.Column="0" HorizontalAlignment="Center" Name="XCoordinate">kjahsd</TextBlock> 
     <TextBlock Grid.Row="1" Grid.Column="1" HorizontalAlignment="Center" Name="YCoordinate">___ahsdjf</TextBlock> 
     <TextBlock Grid.Row="1" Grid.Column="2" HorizontalAlignment="Center" Name="ColumnPosition">___ahsdjf</TextBlock> 
     <TextBlock Grid.Row="1" Grid.Column="3" HorizontalAlignment="Center" Name="RowPosition">___ahsdjf</TextBlock> 
     <Grid Name="InnerGrid" Margin="20,45,20,10" Grid.ColumnSpan="4" Grid.RowSpan="3" Background="{Binding Background, ElementName=window}" PreviewMouseMove="InnerGrid_PreviewMouseMove" > 
      <Grid.RowDefinitions> 
       <RowDefinition/> 
       <RowDefinition/> 
       <RowDefinition/> 
      </Grid.RowDefinitions> 
      <Grid.ColumnDefinitions> 
       <ColumnDefinition/> 
       <ColumnDefinition/> 
       <ColumnDefinition/> 
      </Grid.ColumnDefinitions> 
     </Grid> 
    </Grid> 
</Window> 
1

將一個按鈕放入空單元格(或所有單元格中的按鈕,但其他項目存在時隱藏的樣式並使該單元格不爲空)。然後,當用戶點擊該單元格,報告小區如

private void OnButtonClick(object sender, RoutedEventArgs e) 
{ 
    var buttonClicked = sender as Button; 

    var gridRow = (int)buttonClicked.GetValue(MyGrid.RowProperty); 
    var gridColumn = (int)buttonClicked.GetValue(MyGrid.ColumnProperty); 

} 
0

這裏是一個似乎比建議的正確答案更簡單的另一種選擇。

private void Grid_MouseDown(object sender, MouseButtonEventArgs e) 
{ 
    var element = (UIElement)e.Source; 
    int row = Grid.GetRow(element); 
    int column = Grid.GetColumn(element); 
} 
相關問題