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
245
XamDataTree takes the keyboard focus when programatically setting a XamDataTreeNode to selected
posted

We have two views (graphical windows, i.e. plugins) that are synchronized. They both show the same data in a different manner.
One view displays the data in grid form, the ohter in tree form, which I just switched over from Microsoft WPF to IG XamDataTree.

When I click on a row in the grid-view, it sends a C# event to the tree-view to select the corresponding item.
The tree-view receives the event, finds the corresponding view-model item, and sets a bound property equal to this found item, effectively selecting the correct XamDataTreeNode.

However, the keyboard focus is not set to the tree-view although I was scrrolling in the grid-view!!

Our XamDataTree is a custom one, that I located in an IG forum. Here it is (with very slight mods):

//--------------------------------------------------------------------
public class RsCustomIgTreeView : XamDataTree
{
    private static XamDataTreeNodesCollection msNodes;

    public static readonly DependencyProperty SelectedItemsProperty = DependencyProperty.Register(
        "SelectedItem",
        typeof(object),
        typeof(RsCustomIgTreeView),
        new FrameworkPropertyMetadata(null, FrameworkPropertyMetadataOptions.AffectsRender, OnPropertyChangedCallback));


    public static void OnPropertyChangedCallback(DependencyObject d, DependencyPropertyChangedEventArgs e)
    {
        FindCorrespondingNode(msNodes, e.NewValue);
    }

    public RsCustomIgTreeView()
    {
        ActiveNodeChanged += OnActiveNodeChanged;
        msNodes = Nodes;


        AddHandler(PreviewGotKeyboardFocusEvent, (RoutedEventHandler)OnPreviewGotKeyboardFocusEvent, true);
    }

    private static void OnPreviewGotKeyboardFocusEvent(object sender, RoutedEventArgs e)
    {
        var treeView = sender as RsCustomIgTreeView;
        if (treeView == null) return;
        var viewModelBase = treeView.DataContext as ViewModelBase;
        if (viewModelBase == null) return;

        if (!viewModelBase.IsActive)
            e.Handled = true;
    }

    void OnActiveNodeChanged(object sender, ActiveNodeChangedEventArgs e)
    {
        if (e.NewActiveTreeNode != null)
        {
            SelectedItem = e.NewActiveTreeNode.Data;
        }
    }

    public object SelectedItem
    {
        get { return GetValue(SelectedItemsProperty); }
        set
        {
            SetValue(SelectedItemsProperty, value);
        }
    }

    private static void FindCorrespondingNode(XamDataTreeNodesCollection nodes, object value)
    {
        foreach (XamDataTreeNode node in nodes)
        {
            if (node.Data.Equals(value))
            {
                node.IsActive = true;
                node.IsSelected = true;
                return;
            }
            if (node.HasChildren)
            {
                FindCorrespondingNode(node.Nodes, value);
            }
        }
    }
}
//--------------------------------------------------------------------

--> The fix was the event-handler "OnPreviewGotKeyboardFocusEvent", see above. Why is this necessary? Do all IG components have this behavior?

Thanks,
Mark

Parents Reply Children
No Data