从上下文菜单中获取网格* 的行#
本文关键字:的行 网格 获取 上下文 菜单 | 更新日期: 2023-09-27 18:36:00
我得到了一个网格,在我的网格的每个行定义中都有控制系统,如System.Windows.Controls.Image和标签。问题是当我执行右键单击上下文菜单时,它可以工作,我可以恢复网格,但我无法获取单击发生的行。
- 我不知道正在单击什么UIElement,因为我希望用户能够单击行边界内的任何元素。
顺便说一下,我使用的是网格而不是数据网格!
这是我已经拥有的,
<Grid.ContextMenu>
<ContextMenu>
<MenuItem Header="Open Client CP" Background="#FF1C1C1C"/>
<MenuItem Header="Auto Mine" Background="#FF1C1C1C"/>
<MenuItem Header="Disconnect" Background="#FF1C1C1C"/>
<MenuItem Header="Uninstall" Background="#FF1C1C1C"/>
<MenuItem Header="Refresh" Background="#FF1C1C1C" Click="onRefreshMenuClick" CommandParameter="{Binding RelativeSource={RelativeSource Self}, Path=Parent}"/>
</ContextMenu>
</Grid.ContextMenu>
private void onRefreshMenuClick(object sender, RoutedEventArgs e)
{
MenuItem mi = sender as MenuItem;
if (mi != null)
{
ContextMenu cm = mi.CommandParameter as ContextMenu;
if (cm != null)
{
Grid g = cm.PlacementTarget as Grid;
if (g != null)
{
// need something here like g.getrowof(cm.placementtarget)
if (debugWindow != null)
debugWindow.LogTextBox.AppendText("Requested refresh from "+ row);
}
}
}
}
您可以在这篇文章中找到解决方案。 下面我根据您的问题调整该解决方案:
private void onRefreshMenuClick(object sender, RoutedEventArgs e)
{
MenuItem mi = sender as MenuItem;
if (mi != null)
{
ContextMenu cm = mi.CommandParameter as ContextMenu;
if (cm != null)
{
Grid g = cm.PlacementTarget as Grid;
if (g != null)
{
// need something here like g.getrowof(cm.placementtarget)
var point = Mouse.GetPosition(g);
int row = 0;
int col = 0;
double accumulatedHeight = 0.0;
double accumulatedWidth = 0.0;
// calc row mouse was over
foreach (var rowDefinition in g.RowDefinitions)
{
accumulatedHeight += rowDefinition.ActualHeight;
if (accumulatedHeight >= point.Y)
break;
row++;
}
// calc col mouse was over
foreach (var columnDefinition in g.ColumnDefinitions)
{
accumulatedWidth += columnDefinition.ActualWidth;
if (accumulatedWidth >= point.X)
break;
col++;
}
}
}
}
}