Note that there are some explanatory texts on larger screens.

plurals
  1. POAvalonDock + UserControl + DataGrid + ContextMenu command routing issue
    text
    copied!<p>I'm getting weird behavior with command propagation from <code>MenuItems</code> of <code>ContextMenu</code>.</p> <p>I have the following kind of layout: <code>ContextMenu</code> is set for each <code>DataGridRow</code> of <code>DataGrid</code> inside <code>UserControl</code>, which in its turn is inside <code>DockableContent</code> of AvalonDock. If I get rid of <em>either</em> docking or <code>UserControl</code> around my grid there are no problems. <code>ListBox</code> instead of <code>DataGrid</code> doesn't have this issue either.</p> <pre><code>&lt;Window x:Class="DockAndMenuTest.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:ad="clr-namespace:AvalonDock;assembly=AvalonDock" Title="MainWindow" Height="350" Width="525"&gt; &lt;ad:DockingManager&gt; &lt;ad:DocumentPane&gt; &lt;ad:DockableContent Title="Doh!"&gt; &lt;UserControl&gt; &lt;UserControl.CommandBindings&gt; &lt;CommandBinding Command="Zoom" Executed="ExecuteZoom" CanExecute="CanZoom"/&gt; &lt;/UserControl.CommandBindings&gt; &lt;DataGrid Name="_evilGrid"&gt; &lt;DataGrid.Resources&gt; &lt;Style TargetType="DataGridRow"&gt; &lt;Setter Property="ContextMenu"&gt; &lt;Setter.Value&gt; &lt;ContextMenu&gt; &lt;MenuItem Command="Zoom"/&gt; &lt;/ContextMenu&gt; &lt;/Setter.Value&gt; &lt;/Setter&gt; &lt;/Style&gt; &lt;/DataGrid.Resources&gt; &lt;/DataGrid&gt; &lt;/UserControl&gt; &lt;/ad:DockableContent&gt; &lt;/ad:DocumentPane&gt; &lt;/ad:DockingManager&gt; &lt;/Window&gt; </code></pre> <p>Code-behind is trivial as well:</p> <pre><code>public partial class MainWindow { public MainWindow() { InitializeComponent(); _evilGrid.ItemsSource = new[] { Tuple.Create(1, 2, 3), Tuple.Create(4, 4, 3), Tuple.Create(6, 7, 1), }; } private void ExecuteZoom(object sender, ExecutedRoutedEventArgs e) { MessageBox.Show("zoom !"); } private void CanZoom(object sender, CanExecuteRoutedEventArgs e) { e.CanExecute = true; } } </code></pre> <p>So here's the problem: right-clicking on the selected row (if it it was selected before the right click) my command comes out disabled. The command is "Zoom" in this case, but can be any other, including a custom one.</p> <p>I don't know what's at fault here. SNOOP shows that in cases when this propagation fails, instead of <code>UserControl</code>, <code>CanExecute</code> is handled by "PART_ShowContextMenuButton" (Button), which is part of docking header.</p> <p>I've had other issues with UI command propagation within <code>UserControls</code> hosted inside AvalonDock, but this one is the easiest to reproduce.</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