wpf无模式对话框和窗口,如何通信 [英] wpf modeless dialog and windows, how to communicate

查看:125
本文介绍了wpf无模式对话框和窗口,如何通信的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想问一个问题,也许是愚蠢的,但是我想最好的解决方案,所以这里是:

我在wpf中有一个无模式对话,它的所有者是一个窗口.当我单击无模型对话框上的按钮时,我想在一部分Windows上做某事.

我尝试过这样的解决方案:
定义该所有者窗口的静态方法,并在无模型对话框的每次单击按钮时调用该静态方法,但是在这种静态方法中,我无法访问datagrid. (每次单击时,我想在窗口上的数据网格中选择不同的行),我想在单击对话框时定义该窗口的对象不是一个好主意.


我该如何回应点击窗口?如何在它们之间进行通信,我应该可以访问窗口中的控件?

I want to ask a question , maybe it is stupid , but I want to best solution , so here it is:

I have a mode-less dialogue in wpf, its owner is a window. when I click a button on the model-less dialogue I want to do something on a part of windows.

I tried a solution like that:
define a static methods of that owner window, and call that static method on every click of button on modelless dialog, but in this static methods I have no access to datagrid. (on every click I want to select different row in a datagrid which is on window) and I guess define a object of that window in click of dialog is not good idea.


how can I response the click in window ? how can I communicate between them and I should have access to controls in window?

could anyone tell me or direct me to solution?

推荐答案

在WPF中,您应该将数据网格(包括其选定的index属性)绑定到视图模型.给第二个窗体一个对该视图模型实例的引用,然后它可以简单地设置属性(这将导致第一个窗体上的UI控件以及正在监视它的所有其他控件更新).

对于某些类型的更改,您可能需要修改基础模型.但是选择另一行对我来说似乎是一种视图模型.通常,在WPF中,您应该遍历视图模型,因为所有更新和绑定机制都附加在该模型上,而不是通常附加到该模型上(尽管如果一个模型是由多个VM附加的,则在其上通知机制是有意义的)
In WPF you should have the data grid (including its selected index property) bound to a view model. Give the second form a reference to that view model instance and then it can simply set the property (which will cause the UI controls on the first form, and anything else that is watching it, to update).

For some types of change you might want to modify the underlying model, instead. But selecting a different row seems like a view model thing to me. And generally in WPF you should go through the view model, as all the update and binding mechanics are attached there and not normally to the model (though if one model is attached to by several VMs, it makes sense to have notify mechanics on it as well).


据我所知,WPF中没有无模式对话框的概念.它必须是主窗口拥有的常规窗口.

您的方法不是最好的.最可靠的方法是在Windows类中实现适当的接口.这样,另一个窗口应该只获取代表另一个窗口的接口引用,从而改善了窗口的隔离性-仅传递了协作所需的接口.

此外,还有一种非常方便的方法可以在window类中实现接口.注意,窗口类被声明为局部的.您可以再添加一个表示相同类的文件,并添加一个单独的部分声明.诀窍是:继承列表应在所有部分重复.您可以在一个部分中指定一个基类(它已经在自动生成的代码中完成了),而在其他部分的继承列表中指定了一个或多个接口.

这样,您可以使用部分类声明创建一个单独的文件,该类声明仅包含要在继承列表及其实现中实现的接口.

—SA
There is no a concept of modeless dialog in WPF, to the best of my knowledge. It must be the regular window, owned by a main window.

Your approach is not the best. The most robust approach is implementing appropriate interface in a windows class. In this way, another windows should get only the interface reference representing the other window, which improves the isolation of windows — only the interface required for collaboration is passed.

Also, there is a very convenient way to implement interface in the window class. Pay attention, a window class is declared as partial. You can add one more file representing the same class and add a separate partial declaration of it. The trick is: the inheritance list should be be repeated in all parts. You specify a base class in one part (it it already done in the auto-generated code), and one or more interfaces in the inheritance list in other parts.

In this way, you can create a separate file with partial class declaration containing only the interface to be implemented in the inheritance list and its implementation.

—SA


这篇关于wpf无模式对话框和窗口,如何通信的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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