✅ how to call a function from another window WPF
I tried using this code but it wasnt working
45 Replies
What's not working, are you saying the two snippets above are from different files?
they are from different windows
and basically im first uploading data to sql server than using loadlist to refresh the list
the load list function is from managestock window
Have you got some shared class that deals with retrieving data from your db?
yes
dBAccess is the class
I'd put your logic from the LoadList() method into that class then, make it return a List instead of void, and then call that from the class for the window that contains the lstBox
can you show demo code of the function
like how would it return list?
Or whatever type dbAccess.readDatathroughAdapter returns?
it doesnt return anything
it just fills the data table
yeah sorry, you'd return dt.DefaultView then
but i would like to know how to call a function from another window
Ill need to use it in the future as well
Well ideally you don't really want to have your windows updating your other windows, they should pretty much be self contained
I wrote this code
its not really updating other window
wait lemme send ss
so then you can refresh your list from your window's class with lstBox.ItemsSource = dbAccess.LoadList();
when i click upload It should kind of send msg which updates or refreshes the list in managestock
its not working
again back to same thing how do i call function from another window
Is it an error you're getting?
it is showing build failed
I think you possibly also mean to say different
class
?
What error?wpf has windows
idk if they are different class
partial class
it says
Show the actual error
yea wait
Try the suggestion then
i did
even after that the build fails
Well the error the build is failing with must be different then, what's that?
I had forgot to clean the code
but even after that
it is showing execption
...
also u were telling about putting code of load list in dbaccess
but i realised it wont work
Why?
as for that i would need access to lstbox
in the class
wait let me try and show
That's the point though
DbAccess class reads and writes data to your db
Your classes for your windows should deal with the presentation
DbAccess won't try and set the listbox directly, it will just read the data from the db and pass it back to your other class, which well then deal with how/where it's displayed
Ok so what you've done there is, you load up your ManageStock window, which calls dbAccess.LoadList(), which then creates a new ManageStock window, which calls dbAccess.LoadList(), which then creates a new ManageStock window... You see where I'm going?
oh
yea
Your dbAccess class shouldn't be creating a ManageStock window, or trying to set listbox content itself
but all this would be not needed
if my orignal code would work
my head is aching ill try to solve this after a while
Well you can go back to your original way of doing it, and having your LoadList method in the same class as the window where your listBox is
It's often desirable to have your code separated by concerns, like I mentioned above the class for your window just deals with the layout etc, your dbAccess class just deals with reading/writing data to your db etc
hey I found this code online but i dont really understand how to implement it in my code
Second Window
I wouldn't try to call between windows in any meaningful way tbh; consider more like something like a publish subscribe model
do you have any documents or tutorial link for doing that?
I don't in particular; the simplest mechanism is something like a Channel<T>, which allows you to write data to the channel and have a reader in another location pick up the work
I'll try and write a sample; I just can't right at the moment
Please do
It would help me a lot