0

I got a Grid with controls such System.Windows.Controls.Image and Labels in each RowDefinition of my Grid. The problem is when I do the right click contextmenu it works and I can get the grid back but I cannot get the Row which the click occurred.

  • I do not know what UIElement is being clicked on as I want the user to be able to click on any element within the row boundaries.

By the way, I am using a Grid NOT a DataGrid!

Here is what I have already,

<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);
                }
            }
        }
    }
Ya Wang
  • 1,758
  • 1
  • 19
  • 41

1 Answers1

0

You can find a solution in this post. Below I adapt that solution to your problem:

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++;
                    }
                } 
            }
        }   
 }
Community
  • 1
  • 1
ocuenca
  • 38,548
  • 11
  • 89
  • 102