2012-06-05 54 views
0

如何使用MVVM Light中的消息傳遞來顯示ChildWindow並從中返回一些值? 我需要的是向用戶呈現帶有2個日期選擇器的模式對話框,並且該消息以某種方式返回2個值作爲參數用於其他視圖。 這可能嗎?MVVM燈中的子窗口

回答

0

通常我只是使用Popup這樣的東西。

Popup通常有Visibility必將像在ViewModelIsPopupVisible屬性,彈出DataContext通常也是視圖模型

部分我沒有WPF的默認Popup控制一個巨大的風扇,所以如果您有興趣,我可以自定義Popup控件here

它通常像這樣使用:

<local:PopupPanel Content="{Binding PopupContent}" 
    local:PopupPanel.PopupParent="{Binding ElementName=ParentPanel}" 
    local:PopupPanel.IsPopupVisible="{Binding IsPopupVisible}" /> 

,我展示它從ViewModel這樣的:

PopupContent = new SelectDatesViewModel(); 
IsPopupVisible = true; 

一旦SelectDatesViewModel.SaveCommand被觸發,你可以解僱掉包含所選日期的消息以任何ViewModels感興趣:

Messenger.Default.Send(new DatesChangedMessage(this.Date1, this.Date2)) 

實際彈出窗口內容可以在XAML中定義,也可以用隱式定義DataTemplate

<DataTemplate DataType="{x:Type local:SelectDatesViewModel}"> 
    <local:SelectDatesView /> 
</DataTemplate>