Click here to Skip to main content
15,867,686 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hi,

I have a UserControl, which is having a button. On click of this button, a popup (another usercontrol) is being opened. This popup has a Ok button. On click of this ok button popup is being closed and at that popup close event I want to get the viewModel object of this popup.
The sender object of this close popup event is ChildWindow.

Here is the popup close event:

C#
void ClosePopup(object sender, EventArgs e)
        {
            PopupVM vm = (sender as ChildWindow).Resources["ViewmodelKey"] as PopupVM;

            if (vm != null)
            {
                //Do something
            }
        }


But here vm is coming as Null. Eventhough the sender is ChildWindow and also key
ViewmodelKey
is correct.

Where I'm going wrong, please suggest.

Thanks...
Posted
Updated 19-Jul-13 0:30am
v3
Comments
Naz_Firdouse 19-Jul-13 5:41am    
why you want this convertion???
If you need a usercontrol, create one usercontrol itself
please eloborate your requirement
Mohammed Hameed 19-Jul-13 5:54am    
Ok, just ignore about childwindow & usercontrol.
Actually I have a popup in silverlight, on some event (like ok button click) inside popup I would like to catch my viewModel object, which I'm unable to. Any hint to solve this.
Mohammed Hameed 19-Jul-13 5:57am    
Getting viewModel object as null. Here is some code:
MyViewModel vm = (sender as ChildWindow).Resources["vmKey"] as MyViewModel;

Here vm is coming as null.
Naz_Firdouse 19-Jul-13 6:09am    
please imporve your question and post some code there
Mohammed Hameed 19-Jul-13 6:11am    
Yes, I think I must update it. Thanks.

if you had set the datacontext for the child window, use like this
C#
PopupVM vm = (sender as ChildWindow).DataContext as PopupVM;
 
Share this answer
 
Comments
Mohammed Hameed 19-Jul-13 8:10am    
No datacontext.
UserControl was a content inside the contents of ChildWindow.
Hence able to find it by something like this.

C#
PopupVM vm = ((((sender as ChildWindow).Content as StackPanel).Children[0] as ScrollViewer).Content as UserControl).Resources["ViewmodelKey"] as PopupVM;
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900