WPF:处理模式对话框

问题描述:

我发现这个网站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。所以实际上它显示了整个事情,那里没有空间。我不知道为什么鼠标没有在那个时候工作,但现在它完美地工作。

回答“更好的方法来处理WPF中的模态对话框?”在WPF Extended Toolkit中有一个new control called Child Window,它解决了您的模态对话框的难题。

enter image description here

+0

我实际上认为我只是在做一些简单的事情是错的。但这是一个很好的信息。 – 2011-03-14 15:32:13