我發現這個網站http://www.thesilvermethod.com/Default.aspx?Id=ModalDialogManagerAsimpleapproachtodealingwithmodaldialogsinMVVMWPF:處理模式對話框
一個很好的解決辦法,而不得不做一些改變,把它融入我的代碼。一路上,我遇到一些小問題,主要是因爲我沒有完全得到代碼的某些部分。
我如何做到將ModalDialogManager綁定到Type IDialogViewModel的MainWindow屬性。然後我有一個WindowsManager類來處理這個屬性中的正確實例。其中一個是EditDialogViewModel,它將EditableViewModel公開給此DialogManager。我將EditDialog視圖設置爲此EditDialogViewModel的DataTemplate,但是當我顯示它時,新窗口僅顯示它的一部分。
這裏查看:
<UserControl.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="EditDataTemplates.xaml" />
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</UserControl.Resources>
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="7*" />
<RowDefinition Height="2*" />
<RowDefinition Height="1*" />
</Grid.RowDefinitions>
<ContentControl Content="{Binding Path=ViewModel}" />
<TextBlock Text="{Binding Path=ViewModel.Error}" />
<UniformGrid Grid.Row="2" Columns="2">
<Button Command="{Binding SaveCommand}" />
<Button Command="{Binding CancelCommand}" />
</UniformGrid>
</Grid>
</UserControl>
但新的對話窗口僅顯示綁定到EditDialogViewModel的視圖模型屬性ContentControl中(其持有的視圖模型被編輯)。
我的猜測是它有事情做與此代碼在ModelDialogManager:
void Show()
{
if (_window != null) Close();
Window w = new Window();
_window = w;
w.Closing += w_Closing;
w.Owner = GetParentWindow(this);
w.DataContext = this.DataContext;
w.SetBinding(Window.ContentProperty, ""); //This code here does something I don't fully understand
w.Title = Title;
w.Icon = Icon;
w.Height = DialogHeight;
w.Width = DialogWidth;
w.ResizeMode = DialogResizeMode;
w.ShowDialog();
}
他正在申請的結合有,但我想這是隻有獲取綁定什麼的第一ContentControl中。這非常棘手。
另一個問題是鼠標在模態對話框內不起作用。我可以標籤到文本框中,但不能點擊它們。
有沒有辦法解決這個或更好的方法來處理WPF中的模式對話框?
編輯
好了我要承認這一點。我是一個巨大的白癡。這很簡單,我只是看不到它。我將UserControl的Height和Width設置爲一個固定值,而我仍然在討論它是一個Window。所以實際上它顯示了整個事情,那裏沒有空間。我不知道爲什麼鼠標沒有在那個時候工作,但現在它完美地工作。
我實際上認爲我只是在做一些簡單的事情是錯的。但這是一個很好的信息。 – 2011-03-14 15:32:13