简体   繁体   English

如何通过ComboBox将值从子窗口传递到父窗口(WPF和MVVM)

[英]How can I pass value from child window to parent window by ComboBox (WPF and MVVM)

I have a child window and there is a ComboBox. 我有一个子窗口,还有一个ComboBox。 I want to send value of child window to parent window using WPF and MVVM. 我想使用WPF和MVVM将子窗口的值发送到父窗口。 Can anyone help me how to do it ?? 谁能帮我怎么做??

Don't focus on MVVM for this scenario, MVVM is not designed for passing values from child windows back to parent windows. 在这种情况下,不要专注于MVVM,MVVM并非旨在将值从子窗口传递回父窗口。

Instead, if you have some code in a viewmodel or the code behind of a view that spawns or opens the child window, then that code should be responsible for retrieving the value determined by the child window and propagating it back to the appropriate property (at which point any binding will reflect the value back in the UI - this is where MVVM should be used). 相反,如果您在视图模型中有某些代码,或者在生成或打开子窗口的视图中有代码,则该代码应负责检索由子窗口确定的值并将其传播回适当的属性(在任何绑定都将在界面上反映该值-这是应该使用MVVM的地方)。

The best thing you can do is make sure the code that opens the child window doesn't go directly accessing a ComboBox on the child window, instead the child window should bind it to a property which is then accessed by the parent's code (preferably via an interface). 最好的办法是确保打开子窗口的代码不会直接访问子窗口上的ComboBox,而是子窗口应将其绑定到一个属性,然后该属性可以由父代码访问(最好通过接口)。

For further reading check out Creating an MVVM friendly dialog strategy . 要进一步阅读,请查看创建MVVM友好对话策略 This should be your preferred solution, then the parent code simply uses the Dialog Service to show the child window, and the Dialog Service is responsible for aggregating the child window result and making it available back to the calling code in the parent window. 这应该是您的首选解决方案,然后父代码仅使用Dialog Service来显示子窗口,而Dialog Service则负责汇总子窗口结果并将其返回给父窗口中的调用代码。

Since you havent given adequate info so, lets assume the child window is a dialog. 由于您没有提供足够的信息,因此,假设子窗口是一个对话框。 Now, lets assume the child window is a class Child() with its ViewModel having the object in Child class, say 现在,假设子窗口是Child()类,其ViewModel在Child类中具有对象,例如

public ChildViewModel chVM { get; set; } 

and this viewmodel having the property: 并且此viewmodel具有以下属性:

public string ComboBoxSelectedValue { get; set; }

Lets have the xaml of the dialog having the combobox as this :- 让对话框的xaml具有如下组合框:-

<ComboBox Name="cbTest" SelectedItem="{Binding ComboBoxSelectedValue}">
<ComboBoxItem>A</ComboBoxItem>
<ComboBoxItem>B</ComboBoxItem>
<ComboBoxItem>C</ComboBoxItem>
</ComboBox>

Now, everytime a value is selected in the combobnox, the property in its viewmodel that is, ComboBoxSelectedValue will be filled with the selected value. 现在,每次在combobnox中选择一个值时,其视图模型中的属性ComboBoxSelectedValue都将填充所选值。

You have to handle the Close event of the dialog on your parent page. 您必须处理父页面上对话框的Close事件。 Lets move on to the class Parent() that is the parent page: 让我们继续进入作为父页面的类Parent():

public partial class Parent : Page
{
    private Child ch;

    public Parent()
    {
        InitializeComponent();

        ch= new Child();
        ch.Closed += ChildClosed;
    }

    public void ChildClosed(object sender, System.EventArgs e)
    {
        //even after closing of child window
        var selectedValue = ch.chVM.ComboBoxSelectedValue;
    }

    public void OpenChild(object sender, System.EventArgs e)
    {
        //Button event to open the child window
        ch.Show();
    }
}

Please respond if this is the one you needed. 如果这是您需要的,请回答。 Or else please feel free to ask for another solution. 否则,请随时寻求其他解决方案。 This can be done without mvvm also, but since you asked for MVVM, so this is the solution. 也可以在没有mvvm的情况下完成此操作,但是由于您要求使用MVVM,因此这是解决方案。

The best solution I have found for allowing view models to communicate with each other is via a messaging framework, my preference is MVVM light available on nuget. 我发现允许视图模型相互通信的最佳解决方案是通过消息传递框架,我更喜欢nuget上的MVVM light。

Your child view model sends a message via the framework, which the parent subscribes to. 您的子视图模型通过框架(父级订阅)发送一条消息。 Not dissimilar to event handlers. 与事件处理程序没什么不同。

Child... 儿童...

Messenger.Default.Send<MyMessageClass>(message);

That can go in a combo box selected item binding setter, or part of a command action method. 可以在组合框中选择项目绑定设置器,也可以将其放入命令操作方法的一部分。

Parent... 父母...

Messenger.Default.Register<MyMessageClass>(this, OnMessage);

The MyMessageClass must extend BaseMessage and should include properties for the data you want to share. MyMessageClass必须扩展BaseMessage,并应包含要共享的数据的属性。 The OnMessage method in the parent should accept this class as a parameter and do whatever you need it to in the parent with those values. 父级中的OnMessage方法应接受此类作为参数,并使用这些值在父级中执行您需要执行的任何操作。

Is is better MVVM since it keeps the logic out of code behind or view, but also doesn't create strong couplings. MVVM更好,因为它可以使逻辑脱离代码或视图,但也不会产生强耦合。 If a view model sends a message that no other view model receives, nothing happens - you also use message objects rather than inspection of view models to share data. 如果视图模型发送的消息没有其他视图模型收到,则什么也不会发生-您还使用消息对象而不是检查视图模型来共享数据。

Tutorial on msdn here . msdn的教程在这里

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM