[英]How can I pass data from usercontrol to another usercontrol using mvvm
For example I have a mainwindow that has a frame on it which binds to a model of all user controls that I have. 例如,我有一个主窗口,上面有一个框架,该框架绑定到我拥有的所有用户控件的模型。
Example: (Basic Personal Information) 示例:(基本个人信息)
I have a usercontrol or (view-1) that display single person from my personalInformation database. 我有一个usercontrol或(view-1),它们显示我的personalInformation数据库中的一个人。
and I also have a usercontrol or (view-2) that display all the list of person in my database. 而且我还有一个usercontrol或(view-2),它们显示数据库中所有人员列表。
the question is how can I get the ID of single person from (view-2) to (view-1) as they are different usercontrol and different viewmodel as well. 问题是如何从(view-2)到(view-1)获取单身人士的ID,因为他们的用户控件和视图模型也不同。
what is the best approach to this kind of scenario? 这种情况的最佳方法是什么? TIA. TIA。
The Usercontrols
are some elements to be part of your Window
, then the Window
can be the suitable connector between your UserControls
. Usercontrols
是要包含在Window
中的某些元素,然后Window
可以成为UserControls
之间的合适连接器。 you can approach this scenario like this: 您可以这样处理:
WindowA
UserControlPersonList
SelectedPersion
Dependency Property . 包括SelectedPersion
依赖项属性 。 SelectedPerson
type is a model class
or viewmodel class
SelectedPerson
类型是model class
还是viewmodel class
model class
viewmodel class
UserControlPerson
Now, inside WindowA.xaml: 现在,在WindowA.xaml中:
<StackPanel>
<userControls:UserControlPersonList x:Name="PersonListControl"/>
<userControls:UserControlPerson DataContext="{Binding ElementName=PersonListControl, Path=SelectedPerson}"/>
</StackPanel>
The result can be something like this (a master-detail view): 结果可能是这样的(主从视图):
You need fill SelectedPerson
dependency property of UserControlPersonList
when you select a person. 选择一个人时,需要填充UserControlPersonList
SelectedPerson
依赖项属性。 To perform this you can use Command
and change SelectedPerson
property in PersonListViewModel
and bind SelectedPerson
dependency property in UserControlPersonList
to it OR do this in your UserControlPersonList
level like this answer . 要执行此,您可以使用Command
和更改SelectedPerson
财产PersonListViewModel
并结合SelectedPerson
依赖属性在UserControlPersonList
它或在您做这个UserControlPersonList
水平喜欢这个答案 。
But if you want some global changed in your UserControls
in different Windows
you can hold the PersonListViewModel
in a static property that is accessible in all of your Windows
and Usercontrols
of your program then create an event
in it named SelectedPersionChanged
. 但是,如果您希望在其他Windows
的UserControls
中进行一些全局更改,则可以将PersonListViewModel
保留在所有Windows
和程序的Usercontrols
中都可以访问的静态属性中,然后在其中创建一个名为SelectedPersionChanged
的event
。 Now in your UserControls
you can subscribe an EventHandler
to SelectedPersionChanged
and change your DataContext
. 现在,您可以在UserControls
中将EventHandler
预订到SelectedPersionChanged
并更改您的DataContext
。 But you MUST unsubscribe your EventHandler
from SelectedPersionChanged
when you did not need to that UserControl
anymore to prevent memory leaks. 但是,当您不再需要该UserControl
来防止内存泄漏时, 必须从SelectedPersionChanged
取消订阅EventHandler
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.