C
C#11mo ago
Mayka

WPF Data Binding Shows Blank

I would like to have an AppState class within my WPF apps that can hold common properties I may want to access throughout the application, such as the assembly’s version number. When I tried to create an instance of this class within my Main Window and then access one of the instance’s properties via a data binding, it’s just showing blank. I know the property itself is set since I can set the window title via code behind to this property, so I’m a bit stumped why nothing populates when I try to access the instance via a binding. Can anyone shed some light on what I may be missing? See attached for a skeleton example.
7 Replies
Mayor McCheese
Mayor McCheese11mo ago
Have you considered using di for wpf?
Honza K.
Honza K.11mo ago
All properties that are used for binding must be public
Mayka
MaykaOP11mo ago
Is there a good example of that somewhere online? Still new to dependency injection, so not 100% sure what it would look like for WPF. That fixed it, thank you!!! I knew it was something simple, just wasn’t sure what I was missing.
Honza K.
Honza K.11mo ago
Also you might want to consider creating a viewmodel instead of setting datacontext to the view itself
Mayka
MaykaOP11mo ago
I’m dealing with an existing application which I didn’t write that already heavily uses code-behind, so I’m just leaving things as-is for now, since it’s working fine currently. On the first application I create myself from scratch I’ll definitely look into using MVVM, though.
Mayor McCheese
Mayor McCheese11mo ago
https://github.com/devdevdeveau/wpf ignore the asp.net stuff, it was a sample for something else
Sir Rufo
Sir Rufo11mo ago
GitHub
GitHub - SirRufo/so-77657479-PageNavigation: Example for stackoverflow
Example for stackoverflow. Contribute to SirRufo/so-77657479-PageNavigation development by creating an account on GitHub.
Wpf.Extensions.Hosting 1.1.2
Package Description
Want results from more Discord servers?
Add your server