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
MVVM TwoWay bindable SelectedItem/ActiveNode extremely slow with even medium data-sets
posted

Hello!
I used the CustomXamDataTree obtained from
http://ko.infragistics.com/community/forums/p/58081/296039.aspx#296039

(BindingSelectedNode.zip)

This allows me to programmatically select the to-be-selected (active) node in my view-model, and, through data-binding, get the appropriate node in the XamDataTree activated. That works.

However, with data-sets of even a few thousand lines, scrolling becomes ridiculously slow.
It seems to be spending much of its time in the FindCorrespondingNode() method, which matches the value object, really my view-model object,
with the appropriate node in the XamDataTree, so that it can be selected.
This is a recursive method, which check the current node for a match and if one is not found, its children are checked.

Something is going on here in this method!  It recurses based on your data-structure "XamDataTreeNodesCollection".
Why is it so very slow? What is the data-structure used there? A Map? A List? Just the tree structure itself?

Many thanks,
Mark

P.S. The call in "OnPropertyChangedCallback()" to "FindCorrespondingNode()" in my opinion only needs to be called when the user DID NOT click on a graphical tree-node item, but rather only when the View-Model SelectedItem (bound property) is programatically selected.  Can you please confirm?

Here is a copy of the code posted in the above-mentioned link:

namespace GraphicalTree
{
    public class CustomXamDataTree : XamDataTree
    {
        private static XamDataTreeNodesCollection nodes;
        private static bool mMouseLeftButtonDownOrKeyDownCame;

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

        public CustomXamDataTree()
        {
            base.ActiveNodeChanged += new EventHandler<ActiveNodeChangedEventArgs>(CustXamDataTree_ActiveNodeChanged);
            nodes = Nodes;
        }

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

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

        public object SelectedItem
        {
            get { return (object)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);
                }

            }
        }
    }
}