如何从Silverlight中的另一个UserControl从ViewModel.cs文件获取UserControl的ViewModel对象? [英] How to get a ViewModel object of a UserControl from ViewModel.cs file from another UserControl in Silverlight?

查看:117
本文介绍了如何从Silverlight中的另一个UserControl从ViewModel.cs文件获取UserControl的ViewModel对象?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述





我有一个UserControl,它有一个按钮。单击此按钮,将打开一个弹出窗口(另一个用户控件)。这个弹出窗口有一个Ok按钮。点击这个ok按钮弹出窗口正在关闭,在弹出关闭事件中我想获得此弹出窗口的viewModel对象。

这个关闭弹出事件的发送者对象是ChildWindow。



这是弹出关闭事件:



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:

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

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





但是这里的虚拟现实是空的。虽然发件人是ChildWindow并且键



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...

推荐答案

如果您为子窗口设置了datacontext,请使用像这样
if you had set the datacontext for the child window, use like this
PopupVM vm = (sender as ChildWindow).DataContext as PopupVM;


UserControl是ChildWindow内容中的内容。

因此能够通过类似的东西找到它。



UserControl was a content inside the contents of ChildWindow.
Hence able to find it by something like this.

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


这篇关于如何从Silverlight中的另一个UserControl从ViewModel.cs文件获取UserControl的ViewModel对象?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆