单击鼠标获取网格单元格

时间:2011-06-15 19:41:27

标签: c# wpf

我有一个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>

4 个答案:

答案 0 :(得分:8)

面对同样的问题,我提出了这个解决方案:

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 :(得分:1)

这里回答:Ways to identify which cell was clicked on WPF Grid?

我之前从未使用过WPF Grid,但是以上面的链接为例我认为这样的事情应该这样做:

将此添加到Initialize方法:

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));
    }
}

答案 2 :(得分:0)

我使用这样的东西:

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
        }
}

答案 3 :(得分:-2)

试试这个

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