2010-11-26 61 views
0

WPF/Silverlight - 绑定跨多个XAML

好家伙,

考虑棱镜(WPF/Silverlight的项目)中,我们已经多个区域的通常情况下,在各进一个视图(XAML),这样的情况可能会出现,当我们有一个互动视图(XAML),无论是使用鼠标还是键盘,我们都可能需要相应地更改或更新其他视图(正好是不同的 XAML)。例如,从视图中选择一个项目,比如ItemPanelView,我们可能希望在其他视图中显示所选项目的详细信息,如ItemDetailsView

所以我的问题是,

难道是绑定从一个视图(XAML)元素是个好主意,在其他视图(不同 XAMLs)的元素,来实现这样的功能?如果我没有错,使用这种方法,我们不需要从一个演讲者到另一个演讲者(使用TwoWay绑定等),以便更新其他区域中的视图。

或者,有没有优雅而简单的方法来做到这一点?

+0

视图是否共享视图模型? – 2010-11-26 16:43:46

回答

0

我建议你看看棱镜的EventAggregator(http://blogs.msdn.com/b/francischeung/archive/2008/06/02/decoupled-communication-with-prism-event-aggregation.aspx)。您的每个视图(或最好查看视图可以使用触发器响应/更新的模型)可以订阅/发布共享事件。但是,我建议不要仅仅响应鼠标点击或键盘事件来引发这个共享事件,而是让共享事件有意义(即MyItemSelected或MyItemHidden)。如果您需要更多帮助或澄清,请告诉我。

0

有几种方法可以做到这一点,而不会破坏PRiSM的典型概念。 为[MSDN文档] [1](第9章:沟通松散耦合的组件之间)告诉我们:

当模块之间的通信,重要的是你知道的方法之间的差异,这样就可以最佳地确定哪种方法在您的特定场景中使用。棱镜库提供以下沟通方法:

Commanding. Use when there is an expectation of immediate action from the user interaction. 
Event aggregation. For communication across view models, presenters, or controllers when there is not a direct action-reaction expectation. 
Region context. Use this to provide contextual information between the host and views in the host's region. This approach is somewhat similar to the DataContext, but it does not rely on it. 
Shared services. Callers can call a method on the service which raises an event to the receiver of the message. Use this if none of the preceding is applicable. 

在你的情况下,你应该使用EventAggregator或RegionContext。共享ViewModel是可能的,但这是最后的手段。