Problem with binding
Hey guys, im having issues with binding in this code:
https://paste.mod.gg/bxttdnuopcri/4
i started doing my game from the start to make it easier to code later
BlazeBin - bxttdnuopcri
A tool for sharing your source code with the world!
55 Replies
i ment from scratch
What's the issue?
Side note, maybe look at the CommunityToolkit. It will make the whole process of implementing INotifyPropertyChanged incredibly easy as it will generate everything for you.
I tried but the issue is:
basically:
@FusedQyou ?
This seems more like an issue with the xaml rather than the code behind of it
Can you share that?
it is shared inside the link i sent u
like it works for the first tile added and then stops @Foosed
The
ViewModel
class should have Left
and Top
in a Tiles
collection. It goes wrong here because one, or more, of these things don't exist
Idk what ViewModel
is though
Also, this code is very confusing. Why did you implement INotifyPropertyChanged on the Window? The whole pattern should exist inside the view mode. The code behind should be practically empty but it almost seems like you have it all in there instead of the ViewModel
By default, Bindings
assume it has to fetch and act on the view model. That's the purpose of MVVM. You can modify this behaviour to point to a difference source, as it's incredibly configurable. However, you should move this logic to the view model since that's the best practice.
inotifychanged on mainwindow is for the score, ure right tho, i can move it to viewmodel
Maybe do that first and worry about the error later
What is
Image
here btw? Why not put it on the Image level then?
It is common that you make a class specific with INPC if you use them in a view. For example, depsite a Vector3 being a struct, people often make a class version with INCP and just convert them back and forth
Sounds dumb until you realize the whole system has a lot of extra bells attachedits for it to display image
new code
it does,
Tile
has property Left
and Top
dont understand thisIf it's not yours forget it
Alternatively you can wrap the image in INPC using a wrapper that just mimics it but it also depends on if it helps anything
I think having it on the Tile level is fine enough, assuming you implemented it there
Does the new code have the same issue?
yeah
when i launch the app it looks like this

the first one is placed and then it is having issues
idk why

I'm not sure. Has the xaml changed at all?
nope
i tried to add tile before it, but it did nothing
Can you share the Tile class?
@Foosed
Okay. No clue. What happends if you remove the
ItemsControl.ItemTemplate
part?
See if removing parts of the code fixes it. I assume that would be the one that "fixes" itsame
this seems off

idk what that says
missing datacontext?
Didnt find DataContext for element "Tiles"
yeah
but how
should it be
DataContext=ViewModel.Tiles
?like wtf do u mean Left doesnt exist

like it is there...
what is this?
so your bindable properties are in your code behind
and you have a ViewModel declared?
yeah
this is not the MVVM pattern
ik
idk how to do it
GitHub
wpfui/samples/Wpf.Ui.Demo.Mvvm at main · lepoco/wpfui
WPF UI provides the Fluent experience in your known and loved WPF framework. Intuitive design, themes, navigation and new immersive controls. All natively and effortlessly. - lepoco/wpfui
Here's a sample of how to set up MVVM properly
And use the MVVM toolkit or ReactiveUI
mvvm is a option tho
The former is preferred if you don't need advanced functionality
im so lost...
Put a pause on your current work
Download that sample
Launch it, play around with it, and see how it works
how to download it?
for this particular sample it is available as a template
which can be obtained from a VS extension
WPF UI - Visual Studio Marketplace
Extension for Visual Studio - WPF UI provides the Fluent experience in your known and loved WPF framework. Intuitive design, themes, navigation and new immersive controls. All natively and effortlessly.
wdym as a template
Download the extension and restart VS
Then create a new project and select this template

done
what vs version do u have?

VS 22 but it shouldn't matter
Did you not wait for the extension to be installed?
Load up a project and go check if it was installed
i have that extension installed alreadt
Double check
installing it again ig
Also you need to search for it in the search bar
ok, worked after reinstall
I dont understand 80% of the stuff in the example
that is the underlaying issue
you need to learn how to apply the pattern properly before you can begin to use it
play around with this template
see what connects what, then apply what you've learnt to your original project
Befoe you do that however, give this a read
https://learn.microsoft.com/en-us/dotnet/architecture/maui/mvvm
Model-View-ViewModel - .NET
Overview of the Model-View-ViewModel pattern used by .NET MAUI
I can't find an article specific for WPF but this one should explain the concept as well
ok, learned to add pages
i give up...
You're conflating your data and your UI layers. This is a good talk that talks about how to separate those by construction so you're not blending your UI and data layers: https://www.youtube.com/watch?v=83UVWrfYreU
dotnet
YouTube
MVVM Building Blocks for WinUI and WPF Development
Embark with us on a hands-on journey to acquire the foundational elements of modern Windows app development. We'll show you how XAML, Data Binding, and MVVM come together to empower your development process, increase agility, and simplify your codebase!
You'll end up with the essential skills to start crafting applications with WinUI or WPF and...
Controls in your UI layer should use DependencyProperties instead to accept bindings in XAML to your underlying VMs/models which are the things which implement INotifyPropertyChanged. There's a new DependencyProperty generator which can help in Windows Community Toolkit Labs, as it is a lot of boilerplate.