public event PropertyChangedEventHandler PropertyChanged always null???
-
Thursday, January 24, 2008 12:14 PMHi,
I'm having problems implementing my own dependency properties as they dont reallt update on source update.
I have a user control (IdentityCard) with some dependency properties implemented. For example:
public static readonly DependencyProperty DisplayNameProperty =
DependencyProperty.RegisterAttached("DisplayName",
typeof(string),
typeof(IdentityCard));
public string DisplayName
{
get { return (string)GetValue(DisplayNameProperty); }
set { SetValue(DisplayNameProperty, value); }
}
Then i have a ObservableCollection of things of the type Presence which implement the INotifyPropertyChanged:
public class Presence : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
#region properties
private string displayName = "";
public string DisplayName
{
set {
displayName = value;
OnPropertyChanged("DisplayName");
}
get { return displayName; }
}
// Create the OnPropertyChanged method to raise the event
protected void OnPropertyChanged(string name)
{
PropertyChangedEventHandler handler = PropertyChanged;
if (handler != null)
{
handler(this, new PropertyChangedEventArgs(name));
}
}
Then i bind the two this way:
Binding b = new Binding();
b.Source = pre.DisplayName;
b.UpdateSourceTrigger = UpdateSourceTrigger.PropertyChanged;
id.SetBinding(IdentityCard.DisplayNameProperty, b);
The result is that nothing is updated when i go to the instance of Presence and change a property. I have also found that the event variable is alway null.
I have never noted this but looking at an example which uses data context to expose the data to the binding target, this event variable is diferent from null and the update works. What am i doing wrong?
Thx,
Best regards,
Nuno
All Replies
-
Thursday, January 24, 2008 12:50 PMFirst thing I noticed: you are registering the dependency prop as an attached prop, try:
Code Snippetpublic static readonly DependencyProperty DisplayNameProperty =
DependencyProperty.Register("DisplayName", typeof(string), typeof(IdentityCard), new FrameworkPropertyMetadata(null));
I think this will work better. -
Thursday, January 24, 2008 1:14 PMHi,
didnt work. the behaviour is the same.
Any other suggestions?
Thx,
Nuno -
Monday, January 28, 2008 4:28 AM
You should specify the binding in the following method:
Code SnippetBinding b = new Binding();
b.Source = pre;
b.Path = new PropertyPath("DisplayName");
b.UpdateSourceTrigger = UpdateSourceTrigger.PropertyChanged;
b.Mode = BindingMode.TwoWay;
this.SetBinding(IdentityCard.DisplayNameProperty, b);
You should specify the PropertyPath, so that the Two way data binding can kick in.
Hope this helps

