更改用户控制内容 [英] Changing User Control Content

查看:75
本文介绍了更改用户控制内容的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

case "Maintenance":
                   switch (DrillDown.Header.ToString())
                   {
                       case "Security":
                           frmSecurity userControl = new frmSecurity();
                           currentContent.Content = userControl;
                           currentContent.Visibility = Visibility.Visible;
                           break;
                       case "Imports/Exports":
                           frmImports userControl2 = new frmImports();
                           currentContent.Content = userControl2;
                           currentContent.Visibility = Visibility.Visible;
                           break;
                       case "Order":
                           frmOrder userControl3 = new frmOrder();
                           currentContent.Content = userControl3;
                           currentContent.Visibility = Visibility.Visible;
                           break;
                   }
                   break;





以上是我项目中的一些代码,维护是菜单标题,下面的选项是用户可以使用的选择以打开将出现在我的主窗体上的不同用户控件。用户选择一个选项并调用usercontrol作为我的调用窗口的嵌入式用户控件的新内容。



问题是,我的Imports / Exports UserControl,实际上需要从其中调用另一个userControl。如何从UserControl2的C#代码中引用currentContent(我用来托管主窗体上的所有内容的userControl),以便我可以更改内容。我知道这是一个范围问题,但似乎无法提出解决方案。如果我不清楚我的问题的描述,请提出问题。在此先感谢!



Above is some code from my project, Maintenance is the menu header and the options below it are ones the user can select to open up different user controls that will appear on my main form. The user selects an option and it calls a usercontrol as the new content of my embedded user control of the calling window.

The problem is, my Imports / Exports UserControl, actually needs to call another userControl from within it. How do I reference currentContent (the userControl that I am using to host everything on my main form) from UserControl2''s C# code, so that I can change the content. I know this is a scope issue, but can''t seem to come up with a solution. If I am unclear with the description of my problem, please ask questions. Thanks in advance!

推荐答案

您使用的是MVVM吗?



您可以做几件事是否使用MVVM。



1)使用事件聚合器(Prism),Mediator(Cinch)或messenger(我认为是mvvm light)。或使用单例来提供两个或多个用户控件之间的通信方式。基本上这些都与发布和订阅模式有关。这实际上是控件之间相互通信的最佳方式。



2)如果失败,请在App.xaml中创建方法,事件或函数。因为这是一个全局类,大多数应用程序都可以访问它,所以你可以/可以使用它。我个人不推荐这个,但它是短期内解决问题的方法。



一些链接:



MVVM中介模式 [ ^ ]

http://joshsmithonwpf.wordpress.com/2009/04/06/a-mediator-prototype-for-wpf-apps/ [ ^ ]

Prism EventAggregator Sample [ ^ ]
Are you using MVVM?

There are a couple of things you could do using MVVM or not.

1) Use an event aggregator (Prism), Mediator (Cinch) or messenger (mvvm light I think). or use a singleton to provide a means of communication between two or more user controls. Basically these all relate to a publish and subscribe pattern. This is really the best way to go about controls communicating with each other.

2) Failing that, create a method, event or function in App.xaml. because this is a global class and accessible to most of your application, you can / could use that. Personally I would not recommend this but it is a solution to a problem in the short term.

some links:

MVVM Mediator Pattern[^]
http://joshsmithonwpf.wordpress.com/2009/04/06/a-mediator-prototype-for-wpf-apps/[^]
Prism EventAggregator Sample[^]


这篇关于更改用户控制内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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