Referencing MainWindow's content in MaterialDesign's DialogHost

荒凉一梦 提交于 2021-01-29 11:17:02

问题


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

This is what I have right now:

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

    My window's content wrapped in grid.

    </md:DialogHost>
</Window>

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>

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

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.


回答1:


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

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

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.



来源:https://stackoverflow.com/questions/57479758/referencing-mainwindows-content-in-materialdesigns-dialoghost

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!