C
C#10mo ago
Kingdawanage

Elements of my child view do not show on my main view

Basically, I have child view that is nested in my main view with text boxes and images that are controlled by a button on my main view. The controlls are properbound and the data context has been set as well. I need help figuring this out
58 Replies
Keswiik
Keswiik10mo ago
Show code, can't really help if you give us nothing to work with.
Kingdawanage
KingdawanageOP10mo ago
Okay, was waiting for a response
Kingdawanage
KingdawanageOP10mo ago
BlazeBin - lwsyjbhighyy
A tool for sharing your source code with the world!
Kingdawanage
KingdawanageOP10mo ago
basically, the data that is shown on the homeview is generated from an event that is triggered by the mainview
Keswiik
Keswiik10mo ago
Is HomeView what you're trying to nest within your main view?
Kingdawanage
KingdawanageOP10mo ago
yes I have it embeded already. I'll show you
Kingdawanage
KingdawanageOP10mo ago
homeview
No description
Kingdawanage
KingdawanageOP10mo ago
mainview
No description
Kingdawanage
KingdawanageOP10mo ago
running
No description
Kingdawanage
KingdawanageOP10mo ago
so there is supposed tobe a bunch of data in the homeview side which doesnt show I have made sure to check whether the bindings are null, which is false
CoRoys
CoRoys10mo ago
show us the binding in your XAML code front
Kingdawanage
KingdawanageOP10mo ago
the xaml for the main view and the home view is here https://paste.mod.gg/lwsyjbhighyy/0
BlazeBin - lwsyjbhighyy
A tool for sharing your source code with the world!
Insire
Insire10mo ago
what do you think happens here? @Yordle_Breeder
No description
Insire
Insire10mo ago
vs here
No description
Kingdawanage
KingdawanageOP10mo ago
assigning the datacontext to both views notifies that view where the bindings come from?
Insire
Insire10mo ago
yes, but thats not the only thing happening. where is the instance for the datacontext coming from?
Kingdawanage
KingdawanageOP10mo ago
instance for the datacontext? you mean...?
Insire
Insire10mo ago
xaml is not some kind of magic, its all c#/.NET under the hood so, when you add a new element in xaml, thats basically calling a ctor you do that once in each view if you expect them to share a viewmodel instance, then thats not happening here because each creates their own instance
Kingdawanage
KingdawanageOP10mo ago
so i should remove the one from the child view?
Insire
Insire10mo ago
i leave that up to you to decide. just removing the viewmoel in the child view wont magically fix anything, that will probably just break your bindings the question you need to answer is, how do you get the correct ViewModel into your child view
Kingdawanage
KingdawanageOP10mo ago
I assume it wouldnt change anything, but what did you mean about creating their own instance
Insire
Insire10mo ago
im not sure how thats unclear. var x = new SomeClass(); x is your instance which is basically the same as what i showed in the screenshots above
Kingdawanage
KingdawanageOP10mo ago
Oh okay, I'm a beginner at this :sadcat:
Insire
Insire10mo ago
¯\_(ツ)_/¯ idk what you know or dont know i always assume basic c# understanding and knowledge of terminology
Kingdawanage
KingdawanageOP10mo ago
I have basic C# knowledge but didnt know of the instance creation here being the same
No description
Insire
Insire10mo ago
which is why i told you about it i also recommend stating what xaml framework you are using, maybe i missed that, but i didnt see it
Kingdawanage
KingdawanageOP10mo ago
xaml framework? I'm on .NET v8 if that's what you're asking about
Insire
Insire10mo ago
thats your .NET version
Kingdawanage
KingdawanageOP10mo ago
Yah. I'm not aware of xaml having a framework version
Insire
Insire10mo ago
no, there are multiple gui frameworks using xaml and each have different use cases and quirks e.g. WPF, UWP, WinUI, Xamarin, Xamarin.Forms, Maui, Avalonia, Uno to name a few most of them work with .NET 8
Kingdawanage
KingdawanageOP10mo ago
Ohhh
Insire
Insire10mo ago
some are x-plat
Kingdawanage
KingdawanageOP10mo ago
Using wpf
Insire
Insire10mo ago
some are stable and depending on which you use, recommendations may differ
Kingdawanage
KingdawanageOP10mo ago
Now i see what you mean, I know maui xaml is different from wpf.
Insire
Insire10mo ago
anyway, the concept of reference types still applies to xaml, no matter the gui framework you use because xaml is just markup for c# types so your <window> has a corresponding c# class
Kingdawanage
KingdawanageOP10mo ago
and that applies to every control? as well ?
Insire
Insire10mo ago
you can assume so. there are value types in there aswell, so those are obviously different you can google for the difference between reference and value types, if you didnt come across that explanation yet its a c# thing and important to know
Kingdawanage
KingdawanageOP10mo ago
Oh i know what that means. I cam from C++
Insire
Insire10mo ago
ok, cool
Kingdawanage
KingdawanageOP10mo ago
From what you've said, it seems i need to make an instance of my homeview model in my mainview model and make the variable assignments there?
Insire
Insire10mo ago
maybe? im not really sure what the goal is you already have a HomeViewModel on your MainViewModel so without going into the details of your code, it could be enough to just make sure, that there is a single MainViewModel and then either using that everywhere or only forwarding the "Child" ViewModel i havent read the whole conversation in here, i just looked at your code and that stood out as a common (wpf) beginner mistake
Kingdawanage
KingdawanageOP10mo ago
In what way do you mean by forwarding the "Child" viewmodel I'm confused by what you mean by using the instance of MainVIewModel everywhere. I had to set the data context of the MainVIewModel in the homeview model so that it would know where the data bindings came from
Insire
Insire10mo ago
no you dont have to set anything in there. the designer or intellisense dont matter. what matters, is what you do during runtime
Insire
Insire10mo ago
take this app for example. nobody specifies a DataContext property anywhere https://github.com/Insire/Dawn/blob/master/src/Dawn/Dawn.Wpf/Features/About/AboutWindow.xaml and it still works
GitHub
Dawn/src/Dawn/Dawn.Wpf/Features/About/AboutWindow.xaml at master · ...
A utility to quickly update a directories contents while automatically backing up all the new files and making them available as past updates/backups - Insire/Dawn
Insire
Insire10mo ago
because i set the datacontext there in the code behind file (here https://github.com/Insire/Dawn/blob/master/src/Dawn/Dawn.Wpf/Features/About/AboutWindow.xaml.cs#L7 ) (that doesnt mean you should do the same, this is just an example)
Kingdawanage
KingdawanageOP10mo ago
so I should remove the datacontext i set in my mainview and make and instance of the mainview model in the behind-the-code of that and pass that same instance to the behind-the-code of my homeview? At least that's wwhat i got
CoRoys
CoRoys10mo ago
Is your data shared between states?
Insire
Insire10mo ago
no
CoRoys
CoRoys10mo ago
Instead of locking the data context behind a single view, you can use DI to inject it as a singleton to any view at any time
Insire
Insire10mo ago
i get that you want guidance on what you should be doing, but there are so many ways to fix your issue, that i dont want to recommend any of them as "THIS IS THE WAY, DO THIS AND NOTHING ELSE"
SuperBrain
SuperBrain10mo ago
@Yordle_Breeder do you know what an instance is? 👀
Insire
Insire10mo ago
because that ends awfully in 99% of cases
Kingdawanage
KingdawanageOP10mo ago
yes. Correct me if I'm wrong but it's creating a new object of something righ t?
SuperBrain
SuperBrain10mo ago
Something like that. Basically, what you need to understand is, in XAML (WPF), there's 2 standard ways of assigning a DataContext - in XAML or in code. You should use only one and never both. In your case, you have a parent and child control.
CoRoys
CoRoys10mo ago
If XAML confuses you just set it in the code behind no questions asked
SuperBrain
SuperBrain10mo ago
Your child wants to use a ViewModel which is instantiated in parent. You need to properly assign (as in "pass" a reference) child control's DataContext to one that is already instantiated in parent. Does that make more sense?
Kingdawanage
KingdawanageOP10mo ago
yah, that explains it ty So i tried to make this easy on myself and generated the data bound to the elements of the homeview in the homeview model. That way, the homeview only needs the button click event from the mainview model to generate the data Not sure if this is relevant, but the homeview is presented ina contentcontrol class And i bound the content to the homeview @SuperBrain Also when I remove the declaration of the mainviewmodel from the xaml and put it in the code behind my home view bound to my content control becomes invisible
Want results from more Discord servers?
Add your server