none
public event PropertyChangedEventHandler PropertyChanged always null???

    السؤال

  • Hi,

    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


    15/محرم/1429 12:14 م

الإجابات

  • You should specify the binding in the following method:

    Code Snippet
    Binding 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

    19/محرم/1429 04:28 ص

جميع الردود

  • First thing I noticed: you are registering the dependency prop as an attached prop, try:

    Code Snippet

    public static readonly DependencyProperty DisplayNameProperty =
              DependencyProperty.Register("DisplayName", typeof(string), typeof(IdentityCard), new FrameworkPropertyMetadata(null));


    I think this will work better.

    15/محرم/1429 12:50 م
  • Hi,

    didnt work. the behaviour is the same.

    Any other suggestions?

    Thx,

    Nuno
    15/محرم/1429 01:14 م
  • You should specify the binding in the following method:

    Code Snippet
    Binding 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

    19/محرم/1429 04:28 ص