Hoi allemaal,
Ik ben bezig met een usercontrol te maken welke een dependency property bevat genaamd Contacts.
Deze DP is gebind aan een ObservableCollection<Contact>.
So far so good.
Nu komt het echter, als ik de eerste keer een contact toevoeg aan mijn collection dan zie ik dat mijn DP in mijn usercontrol dit netjes oppakt en een propertychanged callback functie aanroept en zijn ding gaat doen.
Maar als ik echter de tweede keer wat toevoeg aan mijn collection dan kom ik nooi meer in mijn callback functie.
Ik hoop dat iemand mij kan vertellen hoe dit komt of hoe ik dit kan oplossen.
Mijn DP in de usercontrol
Mijn AddButtons:
Mijn XAML, aanroep van de UserControl (Contacts is ook een ObservableCollection):
Ik ben bezig met een usercontrol te maken welke een dependency property bevat genaamd Contacts.
Deze DP is gebind aan een ObservableCollection<Contact>.
So far so good.
Nu komt het echter, als ik de eerste keer een contact toevoeg aan mijn collection dan zie ik dat mijn DP in mijn usercontrol dit netjes oppakt en een propertychanged callback functie aanroept en zijn ding gaat doen.
Maar als ik echter de tweede keer wat toevoeg aan mijn collection dan kom ik nooi meer in mijn callback functie.
Ik hoop dat iemand mij kan vertellen hoe dit komt of hoe ik dit kan oplossen.
Mijn DP in de usercontrol
C#:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
| public ObservableCollection<Contact> Contacts { get { return (ObservableCollection<Contact>)GetValue(ContactsProperty); } set { SetValue(ContactsProperty, value); _contacts = value; //Add the buttons to the stackPanel AddButtons(); } } // Using a DependencyProperty as the backing store for MyProperty. This enables animation, styling, binding, etc... public static readonly DependencyProperty ContactsProperty = DependencyProperty.Register("Contacts", typeof(ObservableCollection<Contact>), typeof(AddContactButtonsControl), new FrameworkPropertyMetadata(null, NotifyContactsChanged){BindsTwoWayByDefault = true, DefaultUpdateSourceTrigger = UpdateSourceTrigger.PropertyChanged}); //DependencyProperty.Register("Contacts", typeof(ObservableCollection<Contact>), typeof(AddContactButtonsControl), new UIPropertyMetadata(null, NotifyContactsChanged)); private static void NotifyContactsChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e) { var addContactButtonsControl = sender as AddContactButtonsControl; if (addContactButtonsControl != null) { addContactButtonsControl.Contacts = e.NewValue as ObservableCollection<Contact>; } } |
Mijn AddButtons:
C#:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
| private void AddButtons() { var contactReadeService = new ContactReaderService(); if (_contacts.Count > 0) { spContactButtons.Children.Clear(); foreach (var contact in _contacts) { var contactType = contactReadeService.GetContactType(contact.ContactType); var contactButton = new Button { Content = contactType.Name, Width = 72, Command = LoadContact, CommandParameter = contact }; spContactButtons.Children.Add(contactButton); } } } |
Mijn XAML, aanroep van de UserControl (Contacts is ook een ObservableCollection):
code:
1
| <custom:AddContactButtonsControl Contacts="{Binding Path=Contacts}" /> |