WPF how to add data from 2 list entries to a different window that uses a CustomContol
First time using C# and WPF. Currently my application has 2 windows - the main window and a secondary window to add data to the main window.
- In the secondary window you add data via 2 textboxes, once you fill both textboxes and click a button the input gets stored in 2 different list entries and can then further add more entries or delete specific entires or all entries using an ObservableCollection.
- the place I want all of the entries to be stored once the user is done adding entries is in the main window which uses a customcontrol that also has 2 different list entries.
Sorry if this is too complicated or if the explanation doesn't make much sense, maybe I just have the wrong approach, but either way how do you add all of the elements from a list entry to a list entry that is on a different window?
57 Replies
Code behind from the second window:
"btnConfirm_Click" is the button that is trying to transfer the data from the list entries to the mainwindow and into the CustomControl list entries whilist also closing the second window and clearing the lists (in the second window)
so MeasurementEntries and UnitEntries are supposed to be shared between the 2 windows?
One thing I am thinking about is if a seperate file is made that stores all of that information and just have the Confirm Click button send all of that info to the file and have the mainwindow somehow read that file and get the information that way.
yes
side note, u dont need to do all thise for observablecollection:
Just
well then what u would do is simple, you would use dependency injection to pass a reference of your windowA to windowB
i.e.:
no you have both MeasurementEntries and UnitEntries accessible within the 2nd window
does that make sense to u?
kind of - trying to make it work and understand
will ask for help if needed
ok, just in case u access those properties via _parent, i.e.:
okay managed to finally make it work with some more help.
thanks a lot!
and how u ended up doing it? care to share some code so I can see what u did and possible advise further
changes in the secondwindow:
+
relevant code in the main window:
For some reason I couldn't add items in the CustomControl from the second window even when directly setting a specific value from the code itself, but instead pushing the observable collections into the mainwindow worked perfectly.
definetly not the best or cleanest execution out there, but am glad it works
Would u be able to show me what the 2 windows look like?
and their xaml
sure I'll post how they look right now (not pretty).
its ok, I just want to have an idea so I can try to reproduce the issue it should work without having to do what u did
main window:
the part that uses the custom control is the factory measurements and existing measurements (not implemented yet)
under new measurements there are 2 seperate list views
ah so its not multiple window
ye
its window and usercontrol?
the project has 2 windows and 2 user controls right now
and a class file for input validation
ok but what u posted all happens in the above window + usercontrol right?
no
this is the second window
again uses 2 different list view rather than the control
mmm ok
should start making sense now
yeah I suppose, u add multiple combination on the last window confirm and it should move to previous window
yes!
how are u binding the list you're using ItemsSource?
if u could add directly to the list on the previous window would that be better?
actually I dont even think u need that second window
I can make it all in the mainwindow but UI would get too cluttered?
can u show me hte first window populate with some info?
I will show u an idea
note that the existing will expand up once it has info as well
definetly don't mind redesigning the application.
also note that "New Measurements" and the listviews underneath it are completely seperate from the measurements that are "saved" to the left
does anything else goes under the right side
on the white space
on the right (bottom right-ish) I havent added the button that basically does all of the math and spits you out the result the user is looking for based on all of the lists on a different 3rd window.
that would currently be
so by adding a datatemplate
u could do something like this
and then u could remove this part
and have the form the there
with add and clear all
honestly I am trying to understand why do u need to collect unit/measurements there and then u have it again on the right
can't it be a unified process?
my idea is that everything on the left will eventually have it's save file or something along those lines so that when you open the program you have everything that is left stored there.
The add factory measurements can only add measurements or do nothing once that window is closed.
I do plan on adding delete buttons to the usercontrol like you showed.
so you're saying the damage and disc are side settings u dont want to save
these are settings u want to use the units/measurements with when u fill then
The whole idea of the application is let's you have 2 planks that are different width that would be your "Factory Measurements" and you want to cut the planks into smaller pieces that would be your "New Measurements". When you hit the "optimize" button that doesn't exist currently. The program's purpose is to show/tell the user from which plank to cut what smaller pieces so that they waste less of the planks.
the side damage and disc settings are only there to setup and get an accurate result.
what I want to be saved in the future is let's say you have like 1meter left from one of the planks in the above example that 1 meter leftover would move into the "Existing" usercontrol and if you had way more planks to begin with and some of them weren't used at all in the previously they would stay under "Factory"
I see
well in this case I would just make that left side a navigation menu
and have them both be pages
that is a good idea
this way u have more space to work with
less bloat
or even a tabcontrol
so back to the problem
are u using ItemsSource in your Measurements Factory list?
also I just realize ur using strings oh no
:Smadge:
mainwindow.
using itemsSource binding on the second window
Then u can just have:
then u would just have say
what is the benefit of all this?
- Observable collection takes care of updating the items in the listbox u dont have to manually add items to the list
- You can easily customize what the rows look like or what data it presents
You can further set a SelectedItem binding it u need to.
in the above all u would do to add or remove an item would be
For example:
doesn't need to be a listbox can be done with a datagrid or listview as well
then _parent.Entries should work as intended from the 2nd window
and would remove/add items on the list behind it
u could even bind it to both lists and it would synchronize both
althou I find it unecessary
and that is why I was trying to understand your layout because I feel u would get away without needing that 2nd window
u have so much space in the left side
Combining all of this with a navigation bar will definitely be better...
not quite familiar with Listbox or datagrids yet
it not much different from your listview in terms of being able to set the datatemplate etc
currently can't decide on if I should do the redesign first and then try to make the program work or if I should just try to get the program to work and then redeisgn
redesign should be better?
since if I also show the results also in the mainwindow I wouldn't have to worry about accessing elements and such.
well u definitively should redesign to use bindings and command where needed
and avoid code behind
i.e.: x:Name etc
this will help u also learn towards entering MVVM pattern which is very helpful and organizational for UI Frameworks
what u can do in xaml do in xaml etc
i see
If you want I can post here the redesign here once it's done
will take some time tho
sure if u have questions feel free to ask
okay, thank you so much!
essentially what I explained above with the observable collection, + the class and itemssource
how did you manage to make the delete button to delete the specific Entry?
I made the listbox inside a usercontrol that will be used in other usercontrols that will be displayed on the main window when you click a button on the navbar
also I get "Object reference not set to an instance of an object" error when trying to use the usercontrol
I use Command and CommandParameter