将视图加载到 ContentControl 中并通过单击按钮更改其属性
我有一个 mvvm(模型视图视图模型)silverlight 应用程序,它有几个需要加载到 ContentControls 中的视图(我将其全部放在表达式混合中)。例如,我不知道该怎么做,通过单击另一个内容控件中的另一个视图中的按钮来加载一个内容控件中的一个视图(用户控件)。为了更容易理解问题,我需要做类似的事情:
http:// /www.codeproject.com/KB/silverlight/BlendableVMCom.aspx
不同之处在于,child1 和 child2 应该通过单击“调用 child1”或“调用 child2”按钮来加载到它们自己的内容控件中。
并举例说明,我们将不胜感激。提前致谢!
I have a mvvm(model view viewmodel) silverlight application that has several views that need to be loaded into ContentControls (i made it all in expression blend). What i dont know how to do is, for example, to load one view (user control) in one content control by clicking a button from another view that is in another content control. To make it easier to understand the problem, i need to do something similar to this:
http://www.codeproject.com/KB/silverlight/BlendableVMCom.aspx
with that difference that child1 and child2 are supposed to be loaded into theirown content controls by clicking Call child1 or call child2 buttons.
and example would be appreciated. Thanks in advance!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这个示例非常简单,但我想您现在如何将其调整到您的应用程序。
主视图:
我没有创建单独的视图作为用户控件,这里只是边框,可以用真实的视图替换。
代码隐藏中不同视图的不同视图模型:
第一个视图模型包含将消息发送到另一个视图模型的命令:
要使此示例正常工作,请下载 MVVM Light 库。
第二个视图模型接收消息并为其属性创建视图:
同样,可以使用 clr 对象而不是控件并在 xaml 中应用数据模板,但没有足够的空间来提供所有结果代码。
这就是全部,主要思想是某种事件聚合器,在本例中是 Messenger 类。
如果没有 MVVM Light,则需要更多代码:
在此示例中,您必须更改
DelegateCommand
类以获得不同的内容。其他代码适用于每个人。This example is very simplified, but I think you now how to adjust it to your application.
The main view:
I haven't created separated views as user controls, here are just borders, which can be replaced by real views.
Different view models for different views in code behind:
First view model conains the command which sends the message to another view model:
To make this example work, download the MVVM Light library.
The second view model receive the message and creates a view for its property:
Again, it is possible to use clr object instead of controls and apply data templates in xaml, but there will not be enough space to provide all the resulting code.
So that is all, the main idea is a some kind of event aggregator, which is the
Messenger
class in this particular case.Without the MVVM Light it will require more code:
In this example you must change the
DelegateCommand
class for something different. Other code will work for everyone.听起来您可能正在尝试进行某种导航。如果确实如此,请查看 Silverlight导航框架。
It sounds like you might be trying to do some sort of navigation. If that's true, check out the Silverlight navigation framework.