简体   繁体   English

我如何使用mvvm将数据从用户控件传递到另一个用户控件

[英]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。

A 一种

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: 您可以这样处理:

  1. WindowA
  2. UserControlPersonList
    • Include SelectedPersion Dependency Property . 包括SelectedPersion 依赖项属性 SelectedPerson type is a model class or viewmodel class SelectedPerson类型是model class还是viewmodel class model class viewmodel class
  3. 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水平喜欢这个答案


B

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 . 但是,如果您希望在其他WindowsUserControls中进行一些全局更改,则可以将PersonListViewModel保留在所有Windows和程序的Usercontrols中都可以访问的静态属性中,然后在其中创建一个名为SelectedPersionChangedevent 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.

相关问题 如何从 UserControl1 WPF MVVM 中的按钮将数据从一个 UserControl1 传递到 UserControl2 - How to Pass data from one UserControl1 to UserControl2 from a button in UserControl1 WPF MVVM 如何使用 MVVM 将 UserControl 从插件传递到我的主窗口? - How can I pass a UserControl from a Plugin to my main window using MVVM? 如何使用事件从UserControl调用方法到另一个UserControl或另一个aspx.Page? - How can i call method from UserControl to another UserControl or another aspx.Page using Events? 如何使用 MVVM 将 UserControl 参数传递给 ViewModel - How to pass a UserControl parameter to the ViewModel using MVVM 我如何使用C#从另一个用户控件的用户控件调用方法? - How can i call method from usercontrol from another usercontrol using c#? 如何使用asp.net从另一个usercontrol中调用usercontrol的方法? - How can i call method from usercontrol from another usercontrol using asp.net? 如何使用Server.Transfer()将文本框值从usercontrol(ascx)传递到另一个页面(aspx) - How can I pass textbox value from usercontrol(ascx) to another page (aspx) using Server.Transfer() 如何使用 C# 在 Windows Forms 中隐藏另一个用户控件的用户控件 - How can I hide a usercontrol from another usercontrol in Windows Forms using C# 如何将信息从一个WPF UserControl传递到另一个WPF UserControl? - How to pass information from one WPF UserControl to another WPF UserControl? 使用另一个用户控件中的MVVM启用/禁用按钮 - Enable / Disable the Button using MVVM from another UserControl
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM