Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>note the CommandParameter </p> <pre><code>&lt;Grid Background="Red" Height="100" Width="100"&gt; &lt;Grid.ContextMenu&gt; &lt;ContextMenu&gt; &lt;MenuItem Header="Change status" Click="EditStatusCm_Click" CommandParameter="{Binding RelativeSource={RelativeSource Self}, Path=Parent}" /&gt; &lt;/ContextMenu&gt; &lt;/Grid.ContextMenu&gt; &lt;/Grid&gt; </code></pre> <p>and use it in the handler to figure out which Grid it is</p> <pre><code> private void EditStatusCm_Click(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) { Console.WriteLine(g.Background); // Will print red } } } } </code></pre> <p><b>Update:</b><br> If you want the menuitem handler to get to the Grid's children instead of the Grid itself, use this approach </p> <pre><code>&lt;Grid Background="Red" Height="100" Width="100"&gt; &lt;Grid.Resources&gt; &lt;ContextMenu x:Key="TextBlockContextMenu"&gt; &lt;MenuItem Header="Change status" Click="EditStatusCm_Click" CommandParameter="{Binding RelativeSource={RelativeSource Self}, Path=Parent}" /&gt; &lt;/ContextMenu&gt; &lt;Style TargetType="{x:Type TextBlock}"&gt; &lt;Setter Property="ContextMenu" Value="{StaticResource TextBlockContextMenu}" /&gt; &lt;/Style&gt; &lt;/Grid.Resources&gt; &lt;Grid.RowDefinitions&gt; &lt;RowDefinition /&gt; &lt;RowDefinition /&gt; &lt;/Grid.RowDefinitions&gt; &lt;TextBlock Text="Row0" Grid.Row="0" /&gt; &lt;TextBlock Text="Row1" Grid.Row="1" /&gt; &lt;/Grid&gt; </code></pre> <p>Just replace the TextBlocks with whatever your custom object type is. Then in the event handler, replace <code>Grid g = cm.PlacementTarget as Grid</code> with <code>TextBlock t = cm.PlacementTarget as TextBlock</code> (or whatever your custom object type is).</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload