0

I'm going to create a UserControl for a specific purpose. I need to bind a IEnumerable object to it, so I need to Implement ItemsSource property.

I found following answer for WPF: Custom ItemsSource property for a UserControl

As noticed on a comment newValueINotifyCollectionChanged is always null. What should I do to make this working?

Community
  • 1
  • 1
RaminMT
  • 174
  • 1
  • 14
  • Have you tried it? – Stefan Nov 22 '16 at 13:11
  • @Stefan Yes. I Tried, It's always null – RaminMT Nov 22 '16 at 13:12
  • Is `oldValueINotifyCollectionChanged` always null as well? And how are you setting the `ItemSource` property? You should set it with a collection which implements `INotifyCollectionChanged` – Stefan Nov 22 '16 at 13:15
  • Yes, actually the element doesn't have value! I have a type named `category` & `List` is what I set on ItemsSource – RaminMT Nov 22 '16 at 13:19
  • 1
    Try to make the list a `ObservableCollection`, see: https://msdn.microsoft.com/en-us/library/ms668604(v=vs.110).aspx – Stefan Nov 22 '16 at 13:20
  • Hey tnx, It worked successfully! but what's the difference really? I always use `List<>` to bind my arrays to a listview or such elements – RaminMT Nov 22 '16 at 13:33
  • 1
    Well, the main difference is that it implements the `INotifyCollectionChanged` interface, which according to the spec's `Notifies listeners of dynamic changes, such as when an item is added and removed or the whole list is cleared.` (https://msdn.microsoft.com/en-us/library/system.collections.specialized.inotifycollectionchanged(v=vs.110).aspx). If you are using lists, binding a new list would work, but in case you add a single element after the bind, the ui won't be updated. – Stefan Nov 22 '16 at 13:38
  • Yes, there are some options to do that, but it's mostly a matter of choice. Perhaps you want two combo boxes, or nested items. In the latter you'll need find a way to visualize it. Maybe a group by, some indent `--` 's, or maybe there are some components at github that will do that trick. – Stefan Nov 22 '16 at 14:19
  • ok, really thanks man – RaminMT Nov 22 '16 at 14:31

0 Answers0