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
20
XamDataTree ActiveNodeChanging, cancelling leaves newActiveNode highlighted
posted

In WPF/17.2, I'm using XamDataTree to select records to edit. When a record is edited, an isDirty flag is set. If user clicks another node in the XamDataTree, I use the ActiveNodeChanging event to cancel the change if the isDirty flag = true. However, upon e.Cancel = true, the e.NewActiveTreeNode remains highlighted, and the OriginalActiveTreeNode is outlined. I would expect e.Cancel would handle this better, but since it does not, I added code to clear the selected items collections, and set NewActiveTreeNode IsSelected = false, and OriginalActiveTreeNode IsSelected = true, and multiple variations on logic, yet none work.

Please describe how to reset the treeView as expected.

  • 34510
    Verified Answer
    Offline posted

    Hi Michael,

    The reason setting NewActiveTreeNode.IsSelected doesn't work is because the selection is overwritten later on after the ActiveNodeChanging event has fired.  You need to change the IsSelected state after the XamDataTree has run it's selection logic.  There are two ways you can do this that I know of.

    1.) Use a Dispatcher.BeginInvoke call to run code that reverts the selection after selection has changed.

    private void dataTree_ActiveNodeChanging(object sender, Infragistics.Controls.Menus.ActiveNodeChangingEventArgs e)
    {
        if (isdirty)
        {
            e.Cancel = true;
    
            Dispatcher.BeginInvoke(new Action(() =>
            {
                e.NewActiveTreeNode.IsSelected = false;
                e.OriginalActiveTreeNode.IsSelected = true;
            }));
        }
    }

    2.) Handle the SelectedNodesCollectionChanged event and revert the selection here

    private void dataTree_SelectedNodesCollectionChanged(object sender, Infragistics.Controls.Menus.NodeSelectionEventArgs e)
    {
        if (isdirty)
        {
            dataTree.SelectedNodesCollectionChanged -= dataTree_SelectedNodesCollectionChanged;
    
            dataTree.SelectionSettings.SelectedNodes.Clear();
            foreach (var node in e.OriginalSelectedNodes)
            {
                dataTree.SelectionSettings.SelectedNodes.Add(node);
            }
    
            dataTree.SelectedNodesCollectionChanged += dataTree_SelectedNodesCollectionChanged;
        }
    }