Your Privacy Matters: We use our own and third-party cookies to improve your experience on our website. By continuing to use the website we understand that you accept their use. Cookie Policy
955
Why CommandTarget is not set correctly?
posted

Hello,

I have below code with OnDataRecordPresenterContextMenuOpening that attach contextMenu to the grid and  ExportExecuted is the command handler.

When ExportExecuted is invoked, I was expecting the sender of it to be XamDataGrid instance (as I do exportMenuItem.CommandTarget = grid), instead I am getting the sender as my window instance,

Any idea what I am doing wrong here? Thanks in advance.

Muthu

 --------

            EventManager.RegisterClassHandler(typeof(DataRecordPresenter),
                DataRecordPresenter.ContextMenuOpeningEvent, new ContextMenuEventHandler(OnDataRecordPresenterContextMenuOpening));

            this.CommandBindings.Add(new CommandBinding(_exportCommand, ExportExecuted, ExportCanExecute));

        public void OnDataRecordPresenterContextMenuOpening(object sender, ContextMenuEventArgs e)
        {
            var grid = (sender as DataRecordPresenter).DataPresenter as XamDataGrid;

            if (grid.ContextMenu == null)
            {
                MenuItem exportMenuItem = new MenuItem { Header = "_Export", Name = "mnuExport", Command = _exportCommand };
                exportMenuItem.CommandParameter = grid;
                exportMenuItem.CommandTarget = grid;
                ContextMenu gridContextMenu = new ContextMenu();
                gridContextMenu.Items.Add(exportMenuItem);
                grid.ContextMenu = gridContextMenu;
            }
        }

        void ExportExecuted(object sender, ExecutedRoutedEventArgs e)
        {

            var grid = sender as XamDataGrid;. // Why this is NULL?

        }
--------

 

Parents
  • 69686
    Verified Answer
    posted

    Hello Muthu,

    This is probably because you are adding this command binding in the Window's CommandBindings collection. Have you tried adding it to the XamDataGrid's CommandBindings collection?

Reply Children