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
Show code, can't really help if you give us nothing to work with.
Okay, was waiting for a response
BlazeBin - lwsyjbhighyy
A tool for sharing your source code with the world!
basically, the data that is shown on the homeview is generated from an event that is triggered by the mainview
Is
HomeView
what you're trying to nest within your main view?yes
I have it embeded already. I'll show you
homeview
mainview
running
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
show us the binding in your XAML code front
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!
what do you think happens here? @Yordle_Breeder
vs here
assigning the datacontext to both views notifies that view where the bindings come from?
yes, but thats not the only thing happening. where is the instance for the datacontext coming from?
instance for the datacontext? you mean...?
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
so i should remove the one from the child view?
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
I assume it wouldnt change anything, but what did you mean about creating their own instance
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 aboveOh okay, I'm a beginner at this :sadcat:
¯\_(ツ)_/¯
idk what you know or dont know
i always assume basic c# understanding and knowledge of terminology
I have basic C# knowledge but didnt know of the instance creation here being the same
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
xaml framework? I'm on .NET v8 if that's what you're asking about
thats your .NET version
Yah. I'm not aware of xaml having a framework version
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
Ohhh
some are x-plat
Using wpf
some are stable
and depending on which you use, recommendations may differ
Now i see what you mean, I know maui xaml is different from wpf.
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
and that applies to every control?
as well
?
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
Oh i know what that means. I cam from C++
ok, cool
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?
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
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
no you dont have to set anything in there. the designer or intellisense dont matter.
what matters, is what you do during runtime
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 worksGitHub
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
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)
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
Is your data shared between states?
no
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
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"
@Yordle_Breeder do you know what an instance is? 👀
because that ends awfully in 99% of cases
yes. Correct me if I'm wrong but it's creating a new object of something righ t?
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.
If XAML confuses you just set it in the code behind no questions asked
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?
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