【问题标题】:WPF: Handling Modal DialogsWPF:处理模态对话框
【发布时间】:2011-02-27 20:19:34
【问题描述】:

我在这个网站上找到了一个不错的解决方案 http://www.thesilvermethod.com/Default.aspx?Id=ModalDialogManagerAsimpleapproachtodealingwithmodaldialogsinMVVM

但必须进行一些更改才能将其集成到我的代码中。在此过程中,我遇到了一些小问题,主要是因为我没有完全理解代码的某些部分。

我是如何将 ModalDialogManager 绑定到 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>

但新的 Dialog 窗口仅显示绑定到 EditDialogViewModel 的 ViewModel 属性的 ContentControl(它保存正在编辑的 ViewModel)。

我的猜测是它与 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 设置为固定值,而我仍然在搞乱它是一个窗口。所以实际上它显示了整个事情,只是没有空间。我不知道为什么鼠标当时不能工作,但现在它工作得很好。

【问题讨论】:

    标签: wpf xaml data-binding dialog modal-dialog


    【解决方案1】:

    回答“在 WPF 中处理模态对话框的更好方法?” WPF 扩展工具包中有一个 new control called Child Window 可以解决您的模态对话框问题。

    【讨论】:

    • 我实际上认为我只是做错了一些简单的事情。但这是很好的信息。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-04-15
    • 1970-01-01
    • 1970-01-01
    • 2010-09-21
    • 2020-07-27
    • 1970-01-01
    • 2010-10-21
    相关资源
    最近更新 更多