locked
DependencyProperty change notification not working when containing instance is static? RRS feed

  • Question

  • I have a dependency property:

    public class Data : DependencyObject
    {
      public static DependencyProperty ValueProperty = DependencyProperty.Register
        (
             "Value",
             typeof(int),
             typeof(Data),
             new PropertyMetadata(1)
         );
     
         public int Value
         {
             get { return (int)GetValue(ValueProperty); }
             set { SetValue(ValueProperty, value); }
         }
     }

    I have some UI bound to it:

    <TextBlock Text="{Binding Value}" />
    

    And a button changes the value of the property:

    <Button Content="Increment" Click="Increment"  />
    void Increment(object sender, RoutedEventArgs e) { Data.Value++;  }
    

    The binding does not update when the object in DataContext is static:

    public static Data Data { get; set; } // works without 'static', does not with 'static'
     
    public MainPage()
    {
        this.InitializeComponent();
     
        Data = new Data();
     
        DataContext = Data;
    }

    The behavior is the same in 8.0 release and the 8.1 preview. For comparison, the binding will update with and without static in WPF so this looks like either a bug or a 'by design' change in WinRT.

    Any thoughts?

    (As a workaround, INotifyPropertyChanged works with and without static, it's just the built-in DependencyProperty change notification that's not working here.)

    Thursday, August 15, 2013 3:47 PM

Answers

  • Thanks for the report. I'm not sure if this is deliberate or not, but I'll file it for investigation. This is different behavior from Silverlight as well, but there are some intentional differences in this general area.

    In the future it would be easier if you could either provide a repro project or organize your code snippets so they could be cut & pasted in one go rather than having to pick pieces apart from two interspersed files.

    --Rob


    Friday, August 16, 2013 5:04 AM
    Moderator

All replies

  • Thanks for the report. I'm not sure if this is deliberate or not, but I'll file it for investigation. This is different behavior from Silverlight as well, but there are some intentional differences in this general area.

    In the future it would be easier if you could either provide a repro project or organize your code snippets so they could be cut & pasted in one go rather than having to pick pieces apart from two interspersed files.

    --Rob


    Friday, August 16, 2013 5:04 AM
    Moderator
  • Thanks for investigating, Rob!

    Sorry about the multiple code segments - I wrote it like I was explaining it to someone - one concept at a time -  it was a pain to do it that way so I'm happy to stop ;-)

    Friday, August 16, 2013 12:52 PM
  • This might a shot in the dark since I don't know your full system, but is there a reason you use DependencyProperty here and not INotifyPropertyChanged for property change notification? There's a HUGE overhead with using these, classes inheriting from DependencyObject is UI bound as well as DependencyProperties and they not very portable. They are mostly meant for just Controls and not model objects.

    /Morten
    twitter: http://www.twitter.com/dotMorten
    blog: http://www.sharpgis.net

    Sunday, August 18, 2013 5:09 AM
  • I agree it's a bit of an academic discussion. My main reason for posting is that this is different behavior than many of us are used to from Silverlight or WPF, so there's some chance this is a bug.

    If it is by-design behavior, then it might be nice to have this post here are documentation - the WinStore documentation is very sparse, so I think of these forums as helping fill in the gaps in the official docs.

    Sunday, August 18, 2013 2:01 PM