MaterialDesignInXAML / MaterialDesignInXamlToolkit

Google's Material Design in XAML & WPF, for C# & VB.Net.
http://materialdesigninxaml.net
MIT License
15.1k stars 3.42k forks source link

How to make dialoghost in a user control display in the whole window? #2404

Closed april-lzy closed 3 years ago

april-lzy commented 3 years ago

All demos I found are to apply dialoghost in MainWindow.xaml.

I want to separate usercontrols in different projects. Now I need to open a dialog in one user control but it will only be displayed in the region of that user control. The design is that the dialog need to be displayed in the whole window. Can anyone tell me how to implement it?

chrome_ehGw15JL2V

Keboo commented 3 years ago

Hi @MoeAge The DialogHost control should be placed such that it encompasses all of the control that you want the Dialog to be displayed on top of. Based on your description your DialogHost should likely be placed as close to the root of your Window.

I have written a blog post on it and have several example projects that leverage it.

april-lzy commented 3 years ago

@Keboo Thank you very much for clarification. I am using Prism Library framework for WPF, so I separate different regions into different .Net projects. The region in the picture above is in the different project from MainWindow project.

For example, in the MainWindow, I only have three user controls:

<Grid>
    <ContentControl Grid.Row="0" Grid.Column="0" prism:RegionManager.RegionName="{x:Static core:RegionNames.ProcessRegion}" />
    <ContentControl Grid.Row="0" Grid.Column="1" prism:RegionManager.RegionName="{x:Static core:RegionNames.ConfigurationRegion}" />
    <ContentControl Grid.Row="1" Grid.Column="0" Grid.ColumnSpan="2" prism:RegionManager.RegionName="{x:Static core:RegionNames.MonitoringRegion}" />
</Grid>

In the first user control (ProcessRegion), I have a button to trigger a dialog to display in the whole window. If I have to put all dialogHost content in the MainWindow project, all dialog related view models will be put in MainWindow project. And I also have other buttons in other regions to trigger different dialogs. If I put all of them in MainWindow, it seems cannot satisfy the loosely coupled concept of Prism Library.

Is there any other controls can satisfy this requirement? If no, I think I have to use it in MainWindow even if the logic looks not very good.

Keboo commented 3 years ago

So it depends on how your button is displaying the dialog. I would still put the DialogHost in the MainWindow, since its primary purpose is to declare the content that it should overlap. As for displaying the dialog there are a few options.

  1. If you use the static DialogHost.Show method you can simply pass a UserControl (or other view object) as the content for the dialog. This assumes that you are calling the method from View code and not a ViewModel.
  2. If you are calling the DialogHost.Show method from a view model and passing a view model as the dialog content. You can still keep the relevant DataTemplates in a resource dictionary in the separate project, and simply reference that resource dictionary as a merged dictionary from the MainWindow.
  3. If you are using the routed commands to show the dialog, you can specify the dialog's view content as the command parameter.
  4. Alternatively, the pattern I typically go with is having my code from the sub regions in post a message. In Prism this pattern is covered with EventAggregator. This doesn't quite solve the problems you are describing but it does help break the tight coupling to the DialogHost.Show calls.
april-lzy commented 3 years ago

@Keboo Very thanks. I think I got your ideas. I can put view models in the user control projects and pass it as a paramter to call DialogHost.Show, then it can be passed. And I also can extract content of DialogHost in an another user control. It seems a good way to resolve the coupling issue.

The fourth one is also a good way, but it is a little complicated, I think the first and second options can be very helpful for me.

Thank you very much!