0

I am currently binding an observable list to the selected items in my ListView by using an attached property as follows:

public class DisplaySelectedItems
{
    public static void SetSelectedItems(DependencyObject element, object value)
    {
        if (element is SfDataGrid || element is ListView)
            element.SetValue(SelectedItemsProperty, value);
        else
            throw new NotSupportedException("This property can be used only with SfDataGrid or ListView");
    }
    public static object GetSelectedItems(DependencyObject element)
    {
        return (object)element.GetValue(SelectedItemsProperty);
    }

    public static readonly DependencyProperty SelectedItemsProperty = 
        DependencyProperty.RegisterAttached("SelectedItems", typeof(ObservableCollection<Object>), typeof(DisplaySelectedItems), new PropertyMetadata(new ObservableCollection<Object>(), OnSelectedItemsChanged));
    private static void OnSelectedItemsChanged(DependencyObject d, DependencyPropertyChangedEventArgs args)
    {
        if (d is SfDataGrid)
        {
            var sfDataGrid = d as SfDataGrid;
            if (sfDataGrid == null)
                return;
            //SfDataGridHelper.SelectedItems property updated based on SfDataGrid.SelectedItems Collectionchanged event.
            sfDataGrid.SelectedItems.CollectionChanged += (sender, e) =>
            {
             DisplaySelectedItems.SetSelectedItems(sfDataGrid, sfDataGrid.SelectedItems);
            };
        }

        if (d is ListView)
        {
            var listView = d as ListView;
            if (listView == null)
                return;
            //ListView Selectionchanged event
            listView.SelectionChanged += (sender, e) =>
            {
                DisplaySelectedItems.SetSelectedItems(listView, listView.SelectedItems);
            };
        }
    }
}

The issue with this method is that it will not work unless the observable collection I have binded to the selectedItems property consists of object types (ObservableCollection). How do I modify this code to allow it to work for any type of object type in my ListView?

MElSawy
  • 17
  • 5
  • Use `IList` or `IEnumerable` as property type and check if the actual type implements `INotifyCollectionChanged`. – Clemens Jun 03 '22 at 05:40
  • You may also replace `if (d is SfDataGrid) { var sfDataGrid = d as SfDataGrid; ... }` by `if (d is SfDataGrid sfDataGrid) { ...}`. – Clemens Jun 03 '22 at 05:43
  • I dont understand the IList or IEnumerable part. Dont I still need to specify an object type when defining the IList? my attached property code above currently uses ObservableCollection. I want to make it IList or ObservableCollection or do I need to hard code the object type in my listview? Edit: just saw the similar post. Will check it out. – MElSawy Jun 03 '22 at 13:32
  • Did you take a look at the answers to the duplicate questions? If you use IList as type of your attached property, you can assign any collection that implements that interface, not only `ObservableCollection`. – Clemens Jun 03 '22 at 14:26
  • This one may help: https://stackoverflow.com/a/16953833/1136211 – Clemens Jun 03 '22 at 14:32
  • Thank you @Clemens I will dig into them after work. Appreciate it. – MElSawy Jun 03 '22 at 15:18
  • I took a look and I am not able to make it work. I changed my extended property code above to use IList instead of ObservableCollection and now I am binding it to an ObservableCollection in my VM. That observablecollection is always null now. – MElSawy Jun 07 '22 at 20:26

0 Answers0