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:
public partial class MainWindow : Window
{
public static int Score { get; set; } = 10;
public static int Seed { get; set; }
double WorldHeight { get; set; }
double WorldWidth { get; set; }

public MainWindow()
{
InitializeComponent();
Seed = new Random().Next(1,1000);
Loaded+=(s,e)=>GenerateWorld();
DataContext = this;
}
//....
}
public partial class MainWindow : Window
{
public static int Score { get; set; } = 10;
public static int Seed { get; set; }
double WorldHeight { get; set; }
double WorldWidth { get; set; }

public MainWindow()
{
InitializeComponent();
Seed = new Random().Next(1,1000);
Loaded+=(s,e)=>GenerateWorld();
DataContext = this;
}
//....
}
<Canvas x:Name="Display">
<Label x:Name="ScoreDisplay" Content="{Binding Path=Score}" Panel.ZIndex="10" Canvas.Top="0" Canvas.Left="960" FontSize="24" FontWeight="Bold"/>
</Canvas>
<Canvas x:Name="Display">
<Label x:Name="ScoreDisplay" Content="{Binding Path=Score}" Panel.ZIndex="10" Canvas.Top="0" Canvas.Left="960" FontSize="24" FontWeight="Bold"/>
</Canvas>
At the beggining the Label shows 10, but when i increment Score it doesn't update
20 Replies
FusedQyou
FusedQyou2d ago
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 here
Mąż Zuzanny Harmider Szczęście
idk what that is how do i use it?
FusedQyou
FusedQyou2d ago
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
Mąż Zuzanny Harmider Szczęście
do you have any tutorial to mvvm?
FusedQyou
FusedQyou2d ago
If you were to use CommunityToolkit for your view model, it could look something like this:
public partial class MainWindowViewModel : ObservableObject
{
[ObservableProperty]
private string _score = null!;
}
public partial class MainWindowViewModel : ObservableObject
{
[ObservableProperty]
private string _score = null!;
}
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
FusedQyou
FusedQyou2d ago
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
FusedQyou
FusedQyou2d ago
Episode 3 is the first one with MVVM
Mąż Zuzanny Harmider Szczęście
;-; i feel overwhelmed
FusedQyou
FusedQyou2d ago
Don't be, just follow the tutorial
Mąż Zuzanny Harmider Szczęście
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.
FusedQyou
FusedQyou2d ago
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 😛
Mąż Zuzanny Harmider Szczęście
//MainWindow.xaml
<Window x:Class="TileGame.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:TileGame"
mc:Ignorable="d"
ResizeMode="NoResize"
WindowState="Maximized"
Title="MainWindow" Height="450" Width="800">
<Canvas x:Name="Display">
<Label x:Name="ScoreDisplay" Content="{Binding Path=Score}" Panel.ZIndex="10" Canvas.Top="0" Canvas.Left="960" FontSize="24" FontWeight="Bold"/>
</Canvas>
</Window>
//MainWindow.xaml
<Window x:Class="TileGame.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:TileGame"
mc:Ignorable="d"
ResizeMode="NoResize"
WindowState="Maximized"
Title="MainWindow" Height="450" Width="800">
<Canvas x:Name="Display">
<Label x:Name="ScoreDisplay" Content="{Binding Path=Score}" Panel.ZIndex="10" Canvas.Top="0" Canvas.Left="960" FontSize="24" FontWeight="Bold"/>
</Canvas>
</Window>
Mąż Zuzanny Harmider Szczęście
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) @FusedQyou
FusedQyou
FusedQyou2d ago
Not sure where to look here Can you use a paste site for the code?
FusedQyou
FusedQyou2d ago
Scriptbin - Share Your Code Easily
Use Scriptbin to share your code with others quickly and easily.
Mąż Zuzanny Harmider Szczęście
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.
FusedQyou
FusedQyou2d ago
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
Mąż Zuzanny Harmider Szczęście
u mean for the async stuff?
FusedQyou
FusedQyou2d ago
That also

Did you find this page helpful?