从 ContextMenu 中获取 Grid* 的行号
Get Row# of Grid* From ContextMenu
我在网格的每个 RowDefinition 中得到了一个带有 System.Windows.Controls.Image 等控件和标签的网格。问题是当我右键单击上下文菜单时它可以工作并且我可以取回网格但我无法获取发生点击的行。
- 我不知道正在单击什么 UIElement,因为我希望用户能够单击行边界内的任何元素。
顺便说一下,我使用的是 Grid 而不是 DataGrid!
这是我已有的,
<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);
}
}
}
}
您可以在此 post 中找到解决方案。下面我根据您的问题调整该解决方案:
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++;
}
}
}
}
}
我在网格的每个 RowDefinition 中得到了一个带有 System.Windows.Controls.Image 等控件和标签的网格。问题是当我右键单击上下文菜单时它可以工作并且我可以取回网格但我无法获取发生点击的行。
- 我不知道正在单击什么 UIElement,因为我希望用户能够单击行边界内的任何元素。
顺便说一下,我使用的是 Grid 而不是 DataGrid!
这是我已有的,
<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);
}
}
}
}
您可以在此 post 中找到解决方案。下面我根据您的问题调整该解决方案:
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++;
}
}
}
}
}