Jump to content
  • Advertisement
Sign in to follow this  
CDProp

[.net] WPF Data Binding: INotifyPropertyChanged not needed in constructor...

This topic is 3038 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I apologize for the terrible topic title. XD

So, this isn't really a problem per se. Just something I noticed, that has caused me some confusion. I'm currently doing some data binding stuff for the first time. Really basic. I just have a canvas whose width, height, and background are tied to a a custom Texture object.

And I'm specifying the binding Path in the XAML, but as far as the actual object that it's bound to -- I just set the DataContext of my Window in the Window's constructor.

And one thing that I've noticed is that I can freely change the properties of my Texture object within the Window's constructor, and when the window comes up, the canvas will reflect all of my changes -- even though my Texture class doesn't implement INotifyPropertyChanged.

But then of course, if I tried to change things after the window is constructed, such as through a MouseButtonDown event, then the UI fails to recognize the changes (which is more along the lines of what I'd expect, and fixing this was a simple matter of implementing INotifyPropertyChanged).

I'm guessing that this happens because the binding isn't set up until after the Window is constructed. Am I in the right ball park?

Share this post


Link to post
Share on other sites
Advertisement
As you stated you are setting the DataContext in the constructor.
It is only after the constructor that the object can be added to the visual tree and be displayed.

So when the constructor has finished the object will be added to the visual tree and then the object will be asked for its content/visual representation and thus will evaluate the bindings.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!