wpf - 在WPF网格中,如何在鼠标位置找到“行和列”?

标签 wpf grid mouseevent

我有一些行和列的WPF Grid,例如

<Grid Name="myGrid" MouseMove="OnMouseMove">
    <Grid.RowDefinitions>
        <RowDefinition/>
        <RowDefinition/>
        <RowDefinition/>
    </Grid.RowDefinitions>

    <Grid.ColumnDefinitions>
        <ColumnDefinition/>
        <ColumnDefinition/>
        <ColumnDefinition/>
    </Grid.ColumnDefinitions>
</Grid>

在.cs文件中使用MouseMove的处理程序,例如
private void OnMouseMove(object sender, MouseEventArgs e)
{
    var position = e.GetPosition(myGrid);

    // What row & col is the mouse over?
}

我希望能够找到鼠标悬停在网格中的行和列,这可能吗?

[注意:这是问题的简化版本,因此以这种方式呈现它看起来有些奇怪-它是网格功能之间某些拖放的一部分]

最佳答案

希望您已经找到了解决方案。我遇到了同样的问题,却发现了这个未答复的帖子。因此,我认为下一个将很乐意找到此解决方案:

private void OnMouseMove(object sender, MouseEventArgs e)
{
    var element = (UIElement)e.Source;

    int c = Grid.GetColumn(element);
    int r = Grid.GetRow(element);
}

关于wpf - 在WPF网格中,如何在鼠标位置找到“行和列”?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1259606/

相关文章:

java - 使用向上/向下键导航后,jTable 的行号错误

c++ - MouseCallback 函数 OpenCV

wpf - Visual Studio WPF。将文件夹添加到输出目录中

Extjs 组合框自动完成功能不起作用

WPF:为什么 DataContextChanged 没有在逻辑 child 上提出?

c# - WPF - 以一种方式从滚动条效果中排除元素

mysql - 定制的客户网格导致 MySQL 表扫描和文件排序也就是性能下降

java - 识别 "click-point"和 JfreeChart 图中单击的子图时出现问题

wpf - ListView 不显示 ListView 项目

c# - 我实际上如何从方法外部获得异步 Web 请求的响应?