1

I want to bind the SelectedItems of the DataGrid to my view model. I am using this attached property and code in my XAML.

Attached property:

public class DataGridSelectedItemsAttachedPropertyV2
    {
        #region SelectedItems
        ///
        /// SelectedItems Attached Dependency Property
        ///
        public static readonly DependencyProperty SelectedItemsProperty =
        DependencyProperty.RegisterAttached("SelectedItems", typeof(IList),
        typeof(DataGridSelectedItemsAttachedPropertyV2),
        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)
        {
            DataGrid miDg = d as DataGrid;

            IList ModelSelectedItems = GetSelectedItems(miDg);

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

        }
        #endregion
    }

XAML:

<DataGrid AutoGenerateColumns="False" HorizontalAlignment="Stretch" Margin="5,5,0,12" Name="dgdGeneros" VerticalAlignment="Stretch" Grid.Row="1"
Behaviors:DataGridSelectedItemsAttachedPropertyV2.SelectedItems="{Binding DgdOrigenesSelectedItems}">

The code get a this point:

public static readonly DependencyProperty SelectedItemsProperty =
        DependencyProperty.RegisterAttached("SelectedItems", typeof(IList),
        typeof(DataGridSelectedItemsAttachedPropertyV2),
        new FrameworkPropertyMetadata(null, FrameworkPropertyMetadataOptions.BindsTwoWayByDefault,
        new PropertyChangedCallback(OnSelectedItemsChanged)));

But it never enters in the OnSelectedItemsChanged() method when I select an item in my DataGrid, but I am not able to see the problem.

Thanks.

OhBeWise
  • 5,350
  • 3
  • 32
  • 60
Álvaro García
  • 18,114
  • 30
  • 102
  • 193
  • You didn't show how DgdOrigenesSelectedItems (to which you bind) is defined and used. – Evk Dec 10 '17 at 10:12
  • As a note, it makes no sense to call `SetSelectedItems(miDg, ModelSelectedItems)` in OnSelectedItemsChanged. You are just setting the value that the property already has. – Clemens Dec 10 '17 at 10:34
  • What you actually should do is to check if the collection implements the INotifyCollectionChanged interface, and register and deregister a CollectionChanged event handler. E.g. as shown in [this answer](https://stackoverflow.com/a/9128855/1136211) or in [this one](https://stackoverflow.com/a/15023687/1136211). However, it is unclear how you expect the selection of an item in your DataGrid to trigger an update of your attached property. You haven't implemented anything like that. Search StackOverflow for "bindable SelectedItems", there are a couple of solutions available. – Clemens Dec 10 '17 at 10:43

0 Answers0