Adjustiing properties for popups e.g. MinWidth, MinHeight

Topics: Prism v4 - WPF 4
Jul 21, 2011 at 5:57 PM

Hi guys. Pardon this silly question; I'm a newbie in prism. I'm wondering, what's the easiest way to set the Windows propery thru the Behaviors?

Developer
Jul 22, 2011 at 2:15 PM

Hi,

If you're referring to the properties of the Shell (which is the main window in your application), you could have a ShellViewModel, which would be set as the DataContext of your Shell view. Then, you could bind properties in your view (such as MinWidth, MinHeight, etc) to properties in your view model, and modify them from the view model.

For example, you could have a service that determines such properties based on some custom logic, and make your view model consume that service and update its properties based on the interaction with it.

You can read more about the MVVM pattern in this chapter from the Prism MSDN documentation:

Chapter 5: Implementing the MVVM Pattern

I hope you find this helpful.

Guido Leandro Maliandi
http://blogs.southworks.net/gmaliandi

Jul 25, 2011 at 10:55 PM

Guido, I'm referring to dialogs. I notice that Prism is implementing dialogs/pop-ups/messageboxes thru the Behavior construct. I'm hoping I could intercept this host Window and set the properties accodingly. Thanks for the resource and for the reply.

Developer
Jul 26, 2011 at 1:56 PM

Hi,

In that case, you can modify the Widht and Height properties of the Window that is generated inside the WindowWrapper class. For example, in the StockTrader Reference Implementation, the WindowWrapper class is located in the WindowWrapper.Desktop.cs file inside the Behaviors folder of the StockTraderRI.Infrastructure project.

I hope you find this helpful.

Guido Leandro Maliandi
http://blogs.southworks.net/gmaliandi

Jul 26, 2011 at 4:44 PM

Exactly what I did and I'm glad you being the more experience dev confirms it. Thanks so much.

Mar 28, 2013 at 6:35 PM
Ok, but how about different sizes of Windows?
How to set different sizes?
Developer
Apr 3, 2013 at 6:53 PM
Hi,

The RegionPopupBehaviors defines a ContainerWindowStyle attached property, which you can use to set a Style to be used by the popup Window control. Therefore, you should be able to set the Width / Height properties of the Window using a Style.

An example of this can be seen in the Stock Trader RI (desktop version) in the Shell window, where a popup region is defined:
<Window x:Class="StockTraderRI.Shell"
    ...
    infBehaviors:RegionPopupBehaviors.CreatePopupRegionWithName="{x:Static inf:RegionNames.SecondaryRegion}"
    infBehaviors:RegionPopupBehaviors.ContainerWindowStyle="{StaticResource WindowRegionStyle}"
    ... >
I hope this helps,

Damian Cherubini
http://blogs.southworks.net/dcherubini
Apr 3, 2013 at 7:47 PM
I still don't understand how to set different windows sizes for differents ViewModels.
I saw the example and it simple defines a static value for Width and Height. There's no dynamic size.

Please, ellaborate more about it.
Apr 5, 2013 at 7:33 PM
Hi,

Based on my understanding, a good way to set dinamyc sizes for different ViewModels could be to create a Style with Width and Height properties binded to the properties defined inside the ViewModels. Therefore, properties that are being binded from the Style should share the same name across all your ViewModels in order for the binds to work properly.
In this case, you can apply a Style to the window through the RegionPopupBehaviors.ContainerWindowStyle property which could have a binding to properties in the ViewModels.

Regards,

Federico Martinez
http://blogs.southworks.net/fmartinez
Apr 9, 2013 at 1:37 AM
Thanks for your help. However, I still couldn't make it work:
<Window x:Class="WMB.Full.Shell"
        xmlns:Names="clr-namespace:WMB.Infrastructure.Names;assembly=WMB.Infrastructure"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:prism="http://www.codeplex.com/prism"
        xmlns:vm="clr-namespace:WMB.Full"
        xmlns:popup="clr-namespace:WMB.Infrastructure.Popup;assembly=WMB.Infrastructure"
        xmlns:inf="clr-namespace:WMB.Infrastructure.Names;assembly=WMB.Infrastructure"
        Title="WMB" WindowStartupLocation="CenterScreen"
        WindowStyle="None" AllowsTransparency="True" Background="Transparent"
        popup:RegionPopupBehaviors.CreatePopupRegionWithName="{x:Static inf:RegionNames.PopupRegion}"
        popup:RegionPopupBehaviors.ContainerWindowStyle="{DynamicResource WindowRegionStyle}"
        Style="{DynamicResource ControlStyle}" BorderThickness="4">
    <Window.Resources>
        <Style x:Key="WindowRegionStyle" TargetType="Window">
            <!--<Setter Property="Background" Value="Transparent" />
            <Setter Property="Margin" Value="0" />
            <Setter Property="Padding" Value="0" />-->
            <Setter Property="Width" Value="{Binding Width}" />
            <Setter Property="Height" Value="{Binding Height}" />
            <!--<Setter Property="Title" Value="{Binding Path=Content.Title, RelativeSource={RelativeSource Self}}" />-->
        </Style>
    </Window.Resources>
I've created a base view model that all my view models inherits from:
public class ViewModelBase : NotificationObject
    {
        private bool isBusy;
        private string busyMessage;
        private int asyncCalls = 0;
        private double height = 768;
        private double width = 1280;

        protected const string DefaultMessage = "Aguarde, carregando...";

        public double Height
        {
            get { return height; }
            set { height = value; RaisePropertyChanged(() => this.Height); }
        }

        public double Width
        {
            get { return width; }
            set { width = value; RaisePropertyChanged(() => this.Width); }
        }
Apr 9, 2013 at 6:10 PM
Hi,

Based on my understanding of your code snippet, it seems that you are trying to set the Style for the views by setting it as a Window Resource. Nevertheless, this won't work because the Width and Height properties are binding to the Window DataContext, and not to the one of the View inside it. Therefore, in order to follow this approach you need to target the binding against the content of the Window using a relative path.

On the other hand, another simpler approach you could try is to set the Window to adapt itself to its content's size and then set the Height and Width of the view only, instead of the Window. To do this, you can:

  • Replace the properties width and height from your Window style for this setter property:
<Setter Property="SizeToContent" Value="WidthAndHeight" />
  • Set the Height and Width of the view
This way, the Window should resize automatically to wrap the view.

Hope this helps,

Federico Martinez
http://blogs.southworks.net/fmartinez