Search code examples
c#wpfattached-propertiesattachedbehaviors

AttachedProperty vs behavior to get SelectedItems in the ViewModel


I have to options to get the SelectedItems in the ViewModel.

A attached property like this:

public class ListBoxSelectedItemsAttachedProperty
    {
        #region SelectedItems
        ///
        /// SelectedItems Attached Dependency Property
        ///
        public static readonly DependencyProperty SelectedItemsProperty =
        DependencyProperty.RegisterAttached("SelectedItems", typeof(IList),
        typeof(ListBoxSelectedItemsAttachedProperty),
        new FrameworkPropertyMetadata(null, FrameworkPropertyMetadataOptions.BindsTwoWayByDefault,
        new PropertyChangedCallback(OnSelectedItemsChanged)));

        public static IList GetSelectedItems(DependencyObject d)
        {
            return (IList)d.GetValue(SelectedItemsProperty);
        }

        public static void SetSelectedItems(DependencyObject d, IList value)
        {
            d.SetValue(SelectedItemsProperty, value);
        }

        private static void OnSelectedItemsChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            ListBox miLb = (ListBox)d;
            miLb.SelectionChanged += listBox_SelectionChanged;
        }

        private static void listBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            ListBox miLg = (ListBox)sender;
            //Get list box's selected items.
            IEnumerable miDgSelectedItems = miLg.SelectedItems;
            //Get list from model
            IList ModelSelectedItems = GetSelectedItems(miLg);

            //Update the model
            ModelSelectedItems.Clear();

            if (miLg.SelectedItems != null)
            {
                foreach (var item in miLg.SelectedItems)
                    ModelSelectedItems.Add(item);
            }
            SetSelectedItems(miLg, ModelSelectedItems);
        }
        #endregion
    }

And in the axml it is used in this way, for example in a Listbox:

Behaviors:ListBoxSelectedItemsAttachedProperty.SelectedItems="{Binding MyPropertyInViewModel}"

Attached Behavoir:

public class SelectedItemsBehavior : Behavior<MultiSelector>
    {
        protected override void OnAttached()
        {
            AssociatedObject.SelectionChanged += AssociatedObjectSelectionChanged;
        }
        protected override void OnDetaching()
        {
            AssociatedObject.SelectionChanged -= AssociatedObjectSelectionChanged;
        }

        void AssociatedObjectSelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            List<object> selectedItemList = AssociatedObject.SelectedItems.Cast<object>().ToList();
            ObservableCollection<object> selectedItems = new ObservableCollection<object>(selectedItemList);
            SelectedItems = selectedItems;
        }
        public ObservableCollection<object> SelectedItems
        {
            get { return (ObservableCollection<object>)GetValue(SelectedItemsProperty); }
            set { SetValue(SelectedItemsProperty, value); }
        }
        public static readonly DependencyProperty SelectedItemsProperty =
            DependencyProperty.Register("SelectedItems"
                , typeof(ObservableCollection<object>)
                , typeof(SelectedItemsBehavior)
                ,
            new FrameworkPropertyMetadata(null, FrameworkPropertyMetadataOptions.BindsTwoWayByDefault));
    }

And in the axml is used in this way, for example in a DataGrid:

<i:Interaction.Behaviors>
    <Behaviors:SelectedItemsBehavior SelectedItems="{Binding MyPropertyInViewModel}" />
</i:Interaction.Behaviors>

But Really I don't know the differences between an attached property and an attached behavior, and what is the best option to get the SelectedItems in the ViewModel.

Thanks.


Solution

  • There are basically two different ways of implementing attached behaviours in WPF. You could either create an attached property and apply a PropertyChangedCallback to it that performs some action on or extends the DependencyObject to which it is attached when the value of the dependency property changes.

    The other way is to create a class that dervies from System.Windows.Interactivity.Behavior<T>. This is commonly referred to as a "Blend" behaviour and it provides a better way of encapsulating the functionality of a behaviour compared to creating an attached property with a callback. Blend behaviours are also more design friendly easily as they can be easily attached to visual elements in the UI via drag-drop functionality in Blend and they also offer an easy and clean way to attach to and release event handlers using the OnAttached and OnDetaching methods. The main drawback is that you cannot apply these kind of behaviours in style setters.

    So if you need to be able to attach your behaviour in a Style, use an attached property. Otherwise I would prefer to use a Blend behaviour. There is an example of how to bind to read-only properties available on this blog if you are interested.