Ben Rubin
Ben Rubin

Reputation: 7341

Can I dynamically size dialogs that are opened with IDialogService in Prism?

Is there any way to dynamically size a dialog in code in Prism using IDialogService? I would like to adjust the size of my dialog based on the user's screen resolution.

Here's how I'm opening my dialog:

public class MainViewModel
{
   // Gets injected in the constructor
   private IDialogService dialogService;

   private void OpenDialog()
   {
      this.dialogService.ShowDialog(
         nameof(MyDialog), 
         new DialogParameters(),
         result => { }); 
   }
}

Here's what my dialog looks like in XAML

<UserControl 
   x:Class="MyApplication.MyDialog"
   xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
   xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
   xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
   xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
   xmlns:prism="http://prismlibrary.com/"
   prism:ViewModelLocator.AutoWireViewModel="True"
   mc:Ignorable="d" 
   d:DesignHeight="450" d:DesignWidth="800">

  <!-- various controls -->

</UserControl>

Upvotes: 0

Views: 499

Answers (1)

thatguy
thatguy

Reputation: 22089

The easiest way is to expose Width and Height properties in your view model and bind to them. The drawback is that width and height are purely view related and should not not be accessible in a view model in pure MVVM.

I would like to adjust the size of my dialog based on the user's screen resolution.

If the size adjustment is related to the user's screen resolution, you should consider creating an attached behavior for either your custom dialog window or the dialog user control. This way you can encapsulate the logic for screen resolution adaption in a reusable component that resides in XAML and maintains separation of view and view model concerns. Furthermore, you will have access to the associated window or user control in the behavior, which makes it easier to handle even more compley scenarios without violating MVVM principles.

Upvotes: 1

Related Questions