Emergency Routing AvalonDock + UserControl + DataGrid + ContextMenu

I get weird behavior with team spreading from MenuItemsfrom ContextMenu.

I have the following type of layout: ContextMenuinstalled for each DataGridRow DataGridinside UserControl, which, in turn, is inside DockableContentAvalonDock. If I get rid of the docking or UserControlaround my net, there is no problem. ListBoxinstead DataGridalso does not have this problem.

<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">
    <ad:DockingManager>
        <ad:DocumentPane>
            <ad:DockableContent Title="Doh!">
                <UserControl>
                    <UserControl.CommandBindings>
                        <CommandBinding Command="Zoom" 
                                        Executed="ExecuteZoom" 
                                        CanExecute="CanZoom"/>
                    </UserControl.CommandBindings>
                    <DataGrid Name="_evilGrid">
                        <DataGrid.Resources>
                            <Style TargetType="DataGridRow">
                                <Setter Property="ContextMenu">
                                    <Setter.Value>
                                        <ContextMenu>
                                            <MenuItem Command="Zoom"/>
                                        </ContextMenu>
                                    </Setter.Value>
                                </Setter>
                            </Style>
                        </DataGrid.Resources>
                    </DataGrid>
                </UserControl>
            </ad:DockableContent>
        </ad:DocumentPane>
    </ad:DockingManager>
</Window>

Code lag is also trivial:

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;
    }
}

So, here is the problem: right-clicking on the selected line (if it was selected before the right click), my command is disabled. In this case, the “Scale” command, but it can be any other, including the user one.

, . SNOOP , , , UserControl, CanExecute "PART_ShowContextMenuButton" (), .

UI UserControls, AvalonDock, .

+3
1

ContextMenu - , , FocusScope, true:

MSDN

- FocusManager..::. FocusedElement . , , ContextMenu ToolBar . , scope IsFocusScope false.

, .

,

  • FocusManager.IsFocusScope="True"

  • , ContextMenu UserControl :

:

<ContextMenu FocusManager.IsFocusScope="False">
    <ContextMenu.CommandBindings>
        <CommandBinding Command="Zoom" 
                Executed="ExecuteZoom" 
                CanExecute="CanZoom"/>
    </ContextMenu.CommandBindings>
         <MenuItem Command="Zoom"/>
</ContextMenu>

, !:)

FocusScope :

+2

Source: https://habr.com/ru/post/1745474/


All Articles