✅ How can i save Ui content when switching between views in MVVM wpf
when i switching between views everytime what i my textboxes are cleared only listview items are saved but i have main list in MainWindow what can i do?
48 Replies
show code
but i guess you either overwrite the data when you switch back to the view
or recreate the datacontext
Thats my main view model
Thats my update view command
And thats where i create Application resources
That could be it but i can't see it
what else from code you need?
hmm nothing jumps into my face as wrong here...
in the views do you maybe have some onload/unload event implemented that clears the data?
in the worst case just debug your code
are variables behind the textboxes properties?
i have nothing like that
you have any instruction how to properly do that?
i mean with debugging UI
all my fields and elements in ui are normal i think
@ACiDCA7 btw do i need to have like connected datacontext in every view to its model?
Okay i found that initialize component is not triggering from one of my views
i dont understand it because InitializeComponent is not triggered but rest of things that are next to it are triggered
and my models for every view are empty iam doing everyting in views
sorry i had to go yesterday...
did you solve your problem?
no problem
still not yet
but i think problem could be in my model - view
because iam new with xaml i have no idea how to connect it but with what i read to this time it all reseting when switching view because iam not containing things in model? iam not 100% sure iam still trying and testing things
that could be a cause aswell
you are connecting the value of a textbox with the viiewmodel/model via bindings
so i can make any variable that will store my field text and it needs to be created in viewModel?
and then i need to set dataContext for view to that viewModel or i can do it different?
the dataconext gets autmatically set since you are using datatemplates..
basicly whats its doing is when you want to draw a viewmodel it draws the view instead and sets the datacontext of the view to the viewmodel
lets say you have a viewmodel that includes a string property
so that the content of a textbox gets written into it the TextboxContent has to look something like this
there is no way that i didn't found that useful informations on internet. It makes sense a lot now
you would have found it.. its all over the place..xD
And so if iam changing visibility of my grid for example do i need to make bool variable and store it in viewModel so it will be saved when i will be chaning views?
i was finding similar things but when you explained it i understood it immediately
i dont quite get what you mean
hmm because iam chaning my grid visibility in one place one of my views and when i will bind grid visibility to my variable grid visibility will be stored it this variable ye? Sorry for my stupid english xD
yea i guess...
but since you cant just say true or false to visibility you have to use a converter with the binding, that converts true to visible and false to hidden/collapsed
how to use that converter?
i typed that out of my head so might have to doublecheck...
that what i have typed assumes you have created the converter yourself but iirc wpf has its own might have to google a bit
And i have problem with this because in my xaml
because on my AddName it says that it not found element DataContext for AddName
hmm so i assumes that i need to create that BoolToVisibilityConverter needs to return Visibility.Hidden or false etc etc
thats coming from the designer right?
dont listen on it too much.. its because you have to tell the designer what type the datacontext is
like i said, i think wpf has its own so you can save a couple of lines but to be honest it doesnt hurt to learn how to do it ;)
Okay but i want to say that i need for my settings view set data context from constructor to MainWindow because i need to change my ItemsSource from MainWindow to list view in settings
facts
Oh and i need to say that it still not saves my TextBox text when iam swithing between views :/
so thats why i said with what designer said to it
do you have your project on github or so?
i have to guess what you now actually did and its hard to point out mistakes ;)
i have but its private
but i can add you for a moment
I immediately say that iam trying to make opening program tool for minecraft speedrun community
why not make it public?
i dont judge what you do as long you dont do anything nefarious
i will make it public but when i will make at least first working demo version
but i can make it public for a moment
if you want to help me with it
its my second xaml project so there is so much prototyping and trying different things
fine, you got a link for me?
GitHub
GitHub - FaNim21/MultiOpener
Contribute to FaNim21/MultiOpener development by creating an account on GitHub.
here's link i made it public at the moment
jup give me a moment
ye sure
meeh i see where the problem is...
you are overriding the datacontext in the constructor in the view at least in the settings view
when doing mvvm you usually the codebehind in the view are pretty empty, just covering UI stuff
hmm okay i was thinking later that this can cause problem
yeeee
okay thats it
but now how can i set itemSource for listview from MainWindow list? :/
you create a list and bind to it
in the VM
so you think it will be better to have that list in VM for settings and reference to this list from settingsVM via MainViewModel etc?
yes
assuming i understood you correctly^^
Okay
so i things thats propably all
thank you so much you are insane that you helped with this all ❤️
np
@ACiDCA7 sorry for taking up your time again but in ViewModel side can i make functions for normal button clicks? because it cant work for me like i wrote it in view code side do i really need to make it as commands? or i can keep button functions in view side class and just edit field from view not from ViewModel?
you can and should do it the viewmodel...
as soon as you got the hang of it how it works its easy
but i have debug error when joining settings view
this is the 112 line
so it needs to be as ICommand or not?
Thats my user control button code side
and thats the xaml version of my custom button
i want to use normal click trigger from and with RoutedEventHandler
yes it needs to be something that implement icommand
in xaml you then use Command instead of Click
but what if i need to use RoutedEventHandler like here
so i need to put that back to view cs?
thats now subjective and depends on how you want to do it..
but to me its a plain UI-operation (drag'n'drop) and therefore can be done in the codebehind(in the view)
but when you moved a item from one place to another the removing of first place and adding to secondplace should be done if necessary in the viewmodel
thats just my opinion tho
hmm but what can i done if i use sender and args and list from viewmodel at the same moment
what?
anyway sadly im off now
Okay
then thank you any way
Was this issue resolved? If so, run
/close
- otherwise I will mark this as stale and this post will be archived until there is new activity.