2010-11-26 60 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是可能的,但這是最後的手段。