❔ Saving input correctly to a list in WPF
Hi I am a beginner in C# and I am making a WPF that takes user input and saves it into lists. I'm converting my console app into a wpf so what I did was split the input part into 4 windows and tried to save them to a seperate class which contains the lists but im running into a problem where the input isn't being saved properly and was wondering how I could layout each window so that the inputs can be saved properly.
25 Replies
It's a little hard to imagine this with the description only. Can you add some of your code and screenshots? 🙂
Sure but is it fine to send everything?
Like frontcode and backcode for each window?
Nahhhh, not everything. Just as much so that we can get an idea where the problem comes from
In this case, maybe the "split the input into 4 parts" could be clarified
Ohh okay I'll send screenshots of the way the form looks to show an idea of what I mean.
So first picture is the main menu and then once the 1st button is clicked the 4 other screenshots are the input that I split into 4 windows
Ah, so these are shown in sequence?
Yes
Ok, so the class that contains the list. You say it's not saved properly. How do you tell. And the code would be nice
To post C# code type the following:
```cs
// code here
```
Get an example by typing
$codegif
in chat
If your code is too long, post it to: https://paste.mod.gg/I tried to display the saved input from the lists in a listview but nothing would pop up when I open the listview form but when I manually inputted data in the listview it worked so I thought that there was nothing wrong with the display. Just that my inputs aren't being saved properly. Heres the link to the back code I'm using(https://paste.mod.gg/vidkitfchzcc/0)
BlazeBin - vidkitfchzcc
A tool for sharing your source code with the world!
I suppose you used Bindings to display those items?
If its for the lists then yes I have bindings in my displayform
So first, in your
Lists
class, I'd change each List
to ObservableCollection. This way, the ui is notified when items are added or removedOk I made that change to my list class
Second, in
Addrecipeform.cs
did you perhaps bind recipes
to the ListView? It looks like it doesn't have any way to notify the UI, so that should also be an observable collectionnow that you've changed to
ObservableCollection
, you can no longer update the collection by assigning it to a new collection cuz then it loses it's binding with the UI component. Gotta clear it and add the items againItemsSource="{Binding recipes}"
Thats all I did for my frontcode in the listview
Binding for fields doesn't work
You have
private List<Lists> recipes;
, however, you'd need a property to bind to. Something like
Would I have to clear it manually or would it just clear when the application closes? Sorry this kinda confused me
What I meant is that usually when you wana change the content of the list you could just do
recipes = new ObservableCollection<Lists>()
but that won't work here because that would make our collection lose it's binding. That's why you gotta .Clear()
it and .Add()
each item to it to update the content of the collection while maintining it's binding with the UI componentOhh okay I'll try that
Don't forget to update the binding to the correct name 🙂
Btw just another question. For this to work do I need to bind every textbox/combobox where input is taken?
If you need those values in your ViewModel, then yes
Ok so then that might be the reason why nothing is being displayed because I never binded any of the input boxes
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.