Issues with wpf binding
Hey guys, i'm having some issues with bindining a number
Score
to a Label
in wpf, here's my code:
At the beggining the Label
shows 10
, but when i increment Score
it doesn't update20 Replies
When you increment it you have to well WPF it has been incremented. This is done through the INPC pattern
Your view model has to implement this: https://learn.microsoft.com/en-us/dotnet/api/system.componentmodel.inotifypropertychanged?view=net-9.0&redirectedfrom=MSDN
Even better is to download
CommunityToolkit.MVVM
. This is a very popular library which code generates all the logic for you. Otherwise you have to do this for everything
Oh, you're not even using MVVM, why?
Why put it all in the code behind? You should be using a view model
I don't see the code that increments the score in hereidk what that is
how do i use it?
MVVM is something you generally use in WPF and other UI frameworks because it splits the logic tied to the view from the logic tied to the data. Bindings and everything by default depend on the datacontext (the view model) that you have added to it
do you have any tutorial to mvvm?
If you were to use CommunityToolkit for your view model, it could look something like this:
It would then code generate a Score property that implements INotifyPropertyChanged
You have to tie this to the main window, and use Score from there. That's the correct approach
But yes, you should follow a tutorial. There are plenty
I personally like these: https://www.youtube.com/watch?v=Vjldip84CXQ&list=PLrW43fNmjaQVYF4zgsD0oL9Iv6u23PI6M
AngelSix
YouTube
C# WPF UI Tutorials: 01 - The Basics
Support me at www.patreon.com/angelsix
Amazon Store https://www.amazon.co.uk/shop/angelsix
The first in a series of tutorials on creating WPF applications in C#
Starts with creating and using some basic controls like text boxes, labels, buttons, checkboxes and drop downs
Source code here: https://github.com/angelsix/youtube
Episode 3 is the first one with MVVM
;-; i feel overwhelmed
Don't be, just follow the tutorial
is this a good resource?
https://learn.microsoft.com/en-us/dotnet/maui/xaml/fundamentals/mvvm?view=net-maui-9.0
Data binding and MVVM - .NET MAUI
The Model-View-ViewModel (MVVM) pattern enforces a separation between three software layers — the XAML user interface, called the view, the underlying data, called the model, and an intermediary between the view and the model, called the viewmodel.
I should add that SaberSix uses Foddy here instead of CommunityToolkit. You should use CommunityToolkit instead (the difference is very small)
Yes, I believe so
The video I send are very good, that I do know
You see the usage of
INotifyPropertyChanged
in both of these
However, lots of duplicated code since it doesn't use CommunityToolkit
You see in my example it's a matter of adding a field and an attribute, and you don't have to worry about it
But I get the idea you're a beginner. I would really recommend you follow the videos
By the way @Mąż Zuzanny Harmider Szczęście, please note that despite all this, code behind (which is what you have) still works.
So if you just want this fixed, then please share the full code including how you increment the score
Back in Winforms, before WPF, we didn't even have MVVM and everything was like this 😛this is all my code
btw i tried the INotifyPropertyChanged stuff before but for some reason it just didn't work, but now it does
I know why it didnt work back then :picard_facepalm: i did
{Binding Score}
instead of {Binding Path=Score}
now just one issue, do you have an idea how i could make it so i can give score inside Tile.cs?
i made Score static for that reason, but now when there's INotifyPropertyChnaged it can no longer be static (at least i think so)
@FusedQyouNot sure where to look here
Can you use a paste site for the code?
sure
Such as https://scriptbin.xyz/
Scriptbin - Share Your Code Easily
Use Scriptbin to share your code with others quickly and easily.
Pastebin
//MainWindow.xaml
Pastebin.com is the number one paste tool since 2002. Pastebin is a website where you can store text online for a set period of time.
Do have to go now, but will look when I have time
I do suggest you check the videos ont he MVVM pattern
Then you also don't need to use Dispatcher which is super annoying
u mean for the async stuff?
That also