在MaterialDesign的DialogHost中引用MainWindow的内容 [英] Referencing MainWindow's content in MaterialDesign's DialogHost

查看:373
本文介绍了在MaterialDesign的DialogHost中引用MainWindow的内容的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用XAML库中的Material Design开发WPF应用程序.我想使用一个对话框来显示错误消息.在文档中,我读到了为了缩小和禁用对话框后面的内容,我必须将其放在DialogHost标记中,紧跟在DialogHost.DialogContent

I'm developing a WPF application using Material Design in XAML library. I'd like to use a dialog box to display error messages. In the documentation I've read that in order to dimm and disable content behind the dialog box I have to put it in the DialogHost tag, right after DialogHost.DialogContent

这就是我现在拥有的:

<Window>
    <md:DialogHost>
        <md:DialogHost.DialogContent>
            Content of my dialog box
        </md:DialogHost.DialogContent>

    My window's content wrapped in grid.

    </md:DialogHost>
</Window>

问题是:我打算为不同目的添加更多对话框,但我真的不知道该怎么做,因为我必须将其余代码放入DialogHost标记中,该标记在我的意见会有点混乱.

The problem is: I'm planning to add few more dialog boxes for different purposes and I don't really know how to do that, since I have to put the rest of the code inside the DialogHost tag, which in my opinion would be a bit messy.

相反,我想实现以下目标:

Instead I would like to achieve something like this:

<Window>
    <Grid>
        <md:DialogHost>
            <md:DialogHost.DialogContent>
                Content of my dialog box
            </md:DialogHost.DialogContent>

        Reference somehow the rest of the window's content

        </md:DialogHost>

        Window's content

    </Grid>
</Window>

我尝试使用ContentPresenter,但由于无法将内容Content绑定到视觉元素而收到错误消息.

I tried using ContentPresenter but I'm getting error saying that the property Content cannot be bound to visual element.

如果上述想法无法实现,如何使用1个以上的对话框?因为彼此嵌套会导致代码混乱.

If the idea described above is impossible to do, how can I use more than 1 dialog boxes? Because nesting one in another would result in a big messy code.

推荐答案

您应该首先从主窗口中删除< md:DialogHost.DialogContent> 并创建一个< UserControl>; 针对您需要的每个对话框.

You should first remove the <md:DialogHost.DialogContent>from your main window and create an <UserControl>for each dialog box you need.

在使用此类对话框的ViewModel类中,您必须实例化此< UserControl> 并将此实例作为DialogHost.Show方法的参数提供.

In the ViewModel class using such a dialog you must instantiate this <UserControl> and provide this instance as parameter for the DialogHost.Show method.

Dim view As New MyDialog1() With {.DataContext = Me}
Dim obj as Object = Await MaterialDesignThemes.Wpf.DialogHost.Show(view)
if obj IsNot Nothing Then
 'do something
EndIf

在这个(VB)示例中,使用VieModel类的DataContext实例化了MyDialog1 View类,该类允许View类访问ViewModel类的属性.然后调用DialogHost.Show方法.View类可以提供用户响应,该响应在View类的closig之后进行评估.

I this (VB) example an MyDialog1 View class is instantiated using the DataContext of the VieModel class allowing the View class to access ViewModel class properties. Then the DialogHost.Show method is invoked. The View class can provide user response which is evaluated after closig of the View class.

这篇关于在MaterialDesign的DialogHost中引用MainWindow的内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆