xBimTeam / XbimWindowsUI

The home of XbimXplorer and WPF components for your desktop BIM applications.
Other
245 stars 149 forks source link

Selection dependency property in DrawingControl 3D collectionChanged #194

Open Pedrodeo opened 1 year ago

Pedrodeo commented 1 year ago

Hello, In the DrawingControl3D, there is a DependencyProperty Selection which is an EntitySelection, as I understand, a collection of Entities :

public EntitySelection Selection
        {
            get { return (EntitySelection)GetValue(SelectionProperty); }
            set { SetValue(SelectionProperty, value); }
        }

        public static readonly DependencyProperty SelectionProperty = DependencyProperty.Register("Selection",
            typeof(EntitySelection), typeof(DrawingControl3D), new PropertyMetadata(OnSelectionChanged));

        private static void OnSelectionChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            var d3D = d as DrawingControl3D;
            if (d3D == null)
                return;

            var newVal = e.NewValue as EntitySelection;

            d3D.ReplaceSelection(newVal);
        }

This Selection is usefull in multiselection cases, to bind the collection of selected entities between the treeview part of the explorer, and the DrawingControl3D for example.

When the Selection is set, the event OnSelectionChanged is raised, but we never subscribe to the OnCollectionChanged event of the Selection, which means that if an Entity is added or removed to the Selection, no event will be raised and the binding fails. Same in the OnSelectedEntityChanged event, the Selection is Cleared but it doesn't raised any event and so the selected Entities stay selected in the DrawingControl3D.

I would do something like this :

            var newVal = e.NewValue as EntitySelection;
            var oldVal = e.OldValue as EntitySelection;

            if (newVal != null)
            {
                newVal.CollectionChanged += d3D.OnSelectionCollectionChanged;
            }

            if (oldVal != null)
            {
                oldVal.CollectionChanged -= d3D.OnSelectionCollectionChanged;
            }

and update the Highlight in the OnSelectionCollectionChanged.

What do you think ?