❔ 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
HimmDawg
HimmDawg2y ago
It's a little hard to imagine this with the description only. Can you add some of your code and screenshots? 🙂
Byakko.1325
Byakko.1325OP2y ago
Sure but is it fine to send everything? Like frontcode and backcode for each window?
HimmDawg
HimmDawg2y ago
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
Byakko.1325
Byakko.1325OP2y ago
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
HimmDawg
HimmDawg2y ago
Ah, so these are shown in sequence?
Byakko.1325
Byakko.1325OP2y ago
Yes
HimmDawg
HimmDawg2y ago
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 owo
MODiX
MODiX2y ago
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/
Byakko.1325
Byakko.1325OP2y ago
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!
HimmDawg
HimmDawg2y ago
I suppose you used Bindings to display those items?
Byakko.1325
Byakko.1325OP2y ago
If its for the lists then yes I have bindings in my displayform
HimmDawg
HimmDawg2y ago
So first, in your Lists class, I'd change each List to ObservableCollection. This way, the ui is notified when items are added or removed
Byakko.1325
Byakko.1325OP2y ago
Ok I made that change to my list class
HimmDawg
HimmDawg2y ago
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 collection
Florian Voß
Florian Voß2y ago
now 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 again
Byakko.1325
Byakko.1325OP2y ago
ItemsSource="{Binding recipes}" Thats all I did for my frontcode in the listview
HimmDawg
HimmDawg2y ago
Binding for fields doesn't work You have private List<Lists> recipes;, however, you'd need a property to bind to. Something like
private ObservableCollection<Lists> recipes;

public ObservableCollection<Lists> Recipes
{
get => recipes;
set
{
recipes = value;
PropertyChanged?.Invoke(...);
}
}
private ObservableCollection<Lists> recipes;

public ObservableCollection<Lists> Recipes
{
get => recipes;
set
{
recipes = value;
PropertyChanged?.Invoke(...);
}
}
Byakko.1325
Byakko.1325OP2y ago
Would I have to clear it manually or would it just clear when the application closes? Sorry this kinda confused me
Florian Voß
Florian Voß2y ago
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 component
Byakko.1325
Byakko.1325OP2y ago
Ohh okay I'll try that
HimmDawg
HimmDawg2y ago
Don't forget to update the binding to the correct name 🙂
Byakko.1325
Byakko.1325OP2y ago
Btw just another question. For this to work do I need to bind every textbox/combobox where input is taken?
HimmDawg
HimmDawg2y ago
If you need those values in your ViewModel, then yes
Byakko.1325
Byakko.1325OP2y ago
Ok so then that might be the reason why nothing is being displayed because I never binded any of the input boxes
Accord
Accord2y ago
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.
Want results from more Discord servers?
Add your server