25

Does anyone know how to make a custom ItemsSource?

What I want to do is to make an itemsSource to my own UserControl so that it could be bound by ObservableCollection<>.

Also, I could know Whenever the number of items in the itemsSource updated, so as to do further procedures.

Thank you so much.

Sam
  • 86,580
  • 20
  • 181
  • 179
user1184598
  • 449
  • 1
  • 5
  • 11

3 Answers3

42

You may need to do something like this in your control

public IEnumerable ItemsSource
{
    get { return (IEnumerable)GetValue(ItemsSourceProperty); }
    set { SetValue(ItemsSourceProperty, value); }
}

public static readonly DependencyProperty ItemsSourceProperty =
    DependencyProperty.Register("ItemsSource", typeof(IEnumerable), typeof(UserControl1), new PropertyMetadata(new PropertyChangedCallback(OnItemsSourcePropertyChanged)));

private static void OnItemsSourcePropertyChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
{
    var control = sender as UserControl1;
    if (control != null)
        control.OnItemsSourceChanged((IEnumerable)e.OldValue, (IEnumerable)e.NewValue);
}



private void OnItemsSourceChanged(IEnumerable oldValue, IEnumerable newValue)
{
    // Remove handler for oldValue.CollectionChanged
    var oldValueINotifyCollectionChanged = oldValue as INotifyCollectionChanged;

    if (null != oldValueINotifyCollectionChanged)
    {
        oldValueINotifyCollectionChanged.CollectionChanged -= new NotifyCollectionChangedEventHandler(newValueINotifyCollectionChanged_CollectionChanged);
    }
    // Add handler for newValue.CollectionChanged (if possible)
    var newValueINotifyCollectionChanged = newValue as INotifyCollectionChanged;
    if (null != newValueINotifyCollectionChanged)
    {
        newValueINotifyCollectionChanged.CollectionChanged += new NotifyCollectionChangedEventHandler(newValueINotifyCollectionChanged_CollectionChanged);
    }

}

void newValueINotifyCollectionChanged_CollectionChanged(object sender, NotifyCollectionChangedEventArgs e)
{
    //Do your stuff here.
}
gaurawerma
  • 1,826
  • 13
  • 16
  • 1
    `newValueINotifyCollectionChanged` is always `null`. –  Jul 23 '12 at 08:47
  • When I clear the bounded list, OnItemsSourcePropertyChanged is not triggered... Should it be? – user3260977 Jan 17 '17 at 22:30
  • @user3260977: No. OnItemsSourcePropertyChanged will get triggered only if you change the whole ItemsSource. But newValueINOtifyCollectionChanged should be triggered, if you use a lis that implelements INotifyCollectionChanged. – SomeCoder May 12 '22 at 06:46
  • @user615057: newValueINotifyCollectionChanged and OldValueINotifyCollectionChanged will only be not null if the IEnumerable you set as ItemsSource is implementing INotifyCollectionChanged. – SomeCoder May 12 '22 at 06:48
12

Use a DependencyProperty ItemsSource in your CustomControl and then bind to this DependencyProperty

This is the XAML-Code (Recognize the DataContext of the ListBox):

<UserControl
    x:Name="MyControl">
    <ListBox
        DataContext="{Binding ElementName=MyControl}"
        ItemsSource="{Binding ItemsSource}">
    </ListBox>
</UserControl>

This is the CodeBehind:

public partial class MyCustomControl
{
    public IEnumerable ItemsSource
    {
        get { return (IEnumerable)GetValue(ItemsSourceProperty); }
        set { SetValue(ItemsSourceProperty, value); }
    }

    public static readonly DependencyProperty ItemsSourceProperty =
        DependencyProperty.Register("ItemsSource", typeof(IEnumerable),
            typeof(ToolboxElementView), new PropertyMetadata(null));
}

This is the Code, where you use your "MyCustomControl":

<Window>
    <local:MyCustomControl
        ItemsSource="{Binding MyItemsIWantToBind}">
    </local:MyCustomControl>
</Window>
Alex
  • 369
  • 4
  • 16
  • This is the solution i was looking for. I didn't really think to use "IEnumerable" as type for the property, i kept using string. – Matteo Jun 20 '23 at 12:41
1

Simplified answer.

    public IEnumerable ItemsSource
    {
        get => (IEnumerable)GetValue(ItemsSourceProperty);
        set => SetValue(ItemsSourceProperty, value);
    }

    public static readonly DependencyProperty ItemsSourceProperty =
        DependencyProperty.Register("ItemsSource", typeof(IEnumerable), typeof(UserControl1), new PropertyMetadata(null, (s, e) =>
        {
            if (s is UserControl1 uc)
            {
                if (e.OldValue is INotifyCollectionChanged oldValueINotifyCollectionChanged)
                {
                    oldValueINotifyCollectionChanged.CollectionChanged -= uc.ItemsSource_CollectionChanged;
                }

                if (e.NewValue is INotifyCollectionChanged newValueINotifyCollectionChanged)
                {
                    newValueINotifyCollectionChanged.CollectionChanged += uc.ItemsSource_CollectionChanged;
                }
            }
        }));

    private void ItemsSource_CollectionChanged(object sender, NotifyCollectionChangedEventArgs e)
    {
        // Logic Here
    }

    // Do Not Forget To Remove Event On UserControl Unloaded
    private void UserControl1_Unloaded(object sender, RoutedEventArgs e)
    {
        if (ItemsSource is INotifyCollectionChanged incc)
        {
            incc.CollectionChanged -= ItemsSource_CollectionChanged;
        }
    }
Egemen Çiftci
  • 699
  • 5
  • 13