There is a UserControl . It has a dependency property ItemsProperty type IList<string> (yes, yes, it is a strictly typed sheet). You can bind to this property from the outside and, moreover, the control itself uses this property for display. But when you change the collection, something like Items.Add("asd") in the display does not change anything. This is logical, but how to tell the mapping that the collection has changed?

Now what I was trying to do about it.

  1. I tried to make an ItemsProperty type ObservableCollection<string> . But when using the control, I cannot pry him a property of type List<string> for obvious reasons.

  2. I tried to call DependencyObject.OnPropertyChanged , but nothing happened.

  3. I tried in xaml.cs control to implement INotifyPropertyChanged and raise the PropertyChanged event for the Items property

So what to do?

  • You want that when you change the collection tied to your ItemsProperty property ItemsProperty element ItemsProperty to this, i.e. displayed that the data has changed? - sp7
  • @ sp7, no. For this, I would simply use the ObservableCollection<string> to use the control. This will be so. The control itself when interacting with the user can change this collection. And I want the control to react to changes within itself. - iRumba

1 answer 1

I do not know how much it will help you, but because in most cases, the ObservableCollection<T> acts as a data source, we can use this, so we do the following:

1) In the user control create DP .

 public static readonly DependencyProperty ItemsSourceProperty = DependencyProperty.Register("ItemsSource", typeof(IEnumerable<object>), typeof(MyUserControl), new PropertyMetadatadefault(IEnumerable<object>))); public IEnumerable<object> ItemsSource { get { return (IEnumerable<object>)GetValue(ItemsSourceProperty); } set { SetValue(ItemsSourceProperty, value); } } 

2) In the controller constructor, we subscribe to change the value of your DP .

 public MyUserControl() { InitializeComponent(); var dp = DependencyPropertyDescriptor.FromProperty(ItemsSourceProperty, typeof(MyUserControl)); dp?.AddValueChanged(this, Handler); } 

3) In the handler for a DP change, we INotifyCollectionChanged your property to INotifyCollectionChanged and subscribe to collection change notifications.

 private void Handler(object sender, EventArgs eventArgs) { var nc = ItemsSource as INotifyCollectionChanged; if (nc != null) nc.CollectionChanged += OnCollectionChanged; } 

4) Next, we implement your logic that reacts to changes in the collection:

 private void OnCollectionChanged(object sender, NotifyCollectionChangedEventArgs e) { // Π’Π°ΡˆΠ° Π»ΠΎΠ³ΠΈΠΊΠ° Π½Π° Ρ€Π΅Π°ΠΊΡ†ΠΈΡŽ ΠΎΠ± ΠΈΠ·ΠΌΠ΅Π½Π΅Π½ΠΈΠΈ свойства } 

All entirely:

 public partial class MyUserControl : UserControl { public MyUserControl() { InitializeComponent(); var dp = DependencyPropertyDescriptor.FromProperty(ItemsSourceProperty, typeof(MyUserControl)); dp?.AddValueChanged(this, Handler); } private void Handler(object sender, EventArgs eventArgs) { var nc = ItemsSource as INotifyCollectionChanged; if (nc != null) nc.CollectionChanged += OnCollectionChanged; } private void OnCollectionChanged(object sender, NotifyCollectionChangedEventArgs e) { // Π’Π°Ρˆ ΠΊΠΎΠ΄ } public static readonly DependencyProperty ItemsSourceProperty = DependencyProperty.Register( "ItemsSource", typeof(IEnumerable<object>), typeof(MyUserControl), new PropertyMetadata(default(IEnumerable<object>))); public IEnumerable<object> ItemsSource { get { return (IEnumerable<object>)GetValue(ItemsSourceProperty); } set { SetValue(ItemsSourceProperty, value); } } } 
  • Did you check it yourself in your work? - iRumba
  • Yes, it works. - sp7
  • Not exactly what you need, but there is something to push off. Plus, but as a response, I will not note yet - iRumba
  • Then describe your problem in more detail. - sp7
  • I described what I need. What details are needed? In your decision, I cannot change the collection inside the control. In this case, I will only hope that the ObservableCollection added as the ItemsSource . But the performance of the control should not depend on it. - iRumba