12

I need to set the default size of a view when it first opens, but the view must allow for the user to expand it. (For other reasons I can't use the SizeToContent property in my WindowManager.)

This must be a common thing, what is the recommended approach to setting the default window size?

Kelly
  • 6,992
  • 12
  • 59
  • 76
  • The only way I can see this working is if you set a particular size in your view, then release the view by setting the size to auto after the view has loaded ensuring that the window is the correct size for the content. It could be a behaviour you set on your view – Charleh May 06 '13 at 21:04

5 Answers5

13

This is something that has actually bugged me for a while. Once I figured it out, it annoyed me that I didn't figure it out sooner.

When displaying a window in caliburn, you can set attributes about the Window object when calling it.

So, lets say you want to set the height and width on the window to 600 x 300:

First, you would start with something like this:

public class ShellViewModel : PropertyChangedBase, IShell
{
    private readonly IWindowManager windowManager;

    public ShellViewModel()
    {
        this.windowManager = new WindowManager();
        this.windowManager.ShowWindow(new LameViewModel());
    }
}

There are two other fields on the ShowWindow method. The third parameter lets you dynamically set the attributes on the Window object.

public class ShellViewModel : PropertyChangedBase, IShell
{
    private readonly IWindowManager windowManager;

    public ShellViewModel()
    {
        this.windowManager = new WindowManager();

        dynamic settings = new ExpandoObject();
        settings.Height = 600;
        settings.Width = 300;
        settings.SizeToContent = SizeToContent.Manual;

        this.windowManager.ShowWindow(new LameViewModel(), null, settings);
    }
}

I wish there was more information about working with this on the documentation, but there you have it.

GrantByrne
  • 849
  • 10
  • 21
6

Not sure if this applies at the time this post was made, but for anyone following now this is how you can easily set the window size in CaliburnMicro in the app bootstrapper. My code is designed to retain the same window dimensions on startup as at previous closedown. I save the screen height/width as setting properties on closedown and then retrieve them back on startup here (with a check to ensure not greater than current screen size).

AppBootstrapper.cs

     protected override void OnStartup(object sender, System.Windows.StartupEventArgs e) {

        double width = Settings.Default.screen_width;  //Previous window width 
        double height = Settings.Default.screen_height; //Previous window height

        double screen_width = System.Windows.SystemParameters.PrimaryScreenWidth;
        double screen_height = System.Windows.SystemParameters.PrimaryScreenHeight;

        if (width > screen_width) width = (screen_width - 10);
        if (height > screen_height) height = (screen_height-10);

        Dictionary<string, object> window_settings = new Dictionary<string, object>();

        window_settings.Add("Width", width);
        window_settings.Add("Height", height);

        DisplayRootViewFor<IShell>(window_settings);
    }
Hugh
  • 175
  • 2
  • 2
3

Not sure if it's the recommended approach but instead of bootstrapping/showing a UserControl you can bootstrap/show a Window instead and then set Height, Width, etc.

Hack
  • 1,408
  • 10
  • 13
1

Here is the answer based on Hugh's answer:

        Dictionary<string, object> window_settings = new Dictionary<string, object>();
        window_settings.Add("WindowState", System.Windows.WindowState.Maximized);

        DisplayRootViewFor<IShellViewModel>(window_settings);
FindOutIslamNow
  • 1,169
  • 1
  • 14
  • 33
0

You could also get the instance of the IWindowManager in the bootstrapper and set the initial size of the RootView there. This is based on GrantByrne's answer.

public class CustomBootstrapper : BootstrapperBase
{
    public CustomBootstrapper()
    {
        Initialize();
    }

    protected override void OnStartup(object sender, StartupEventArgs e)
    {
        var windowManager = IoC.Get<IWindowManager>();

        MainViewModel mainViewModel = IoC.Get<MainViewModel>();

        dynamic settings = new ExpandoObject();
        settings.Height = 500;
        settings.Width = 800;
        settings.SizeToContent = SizeToContent.Manual;

        windowManager.ShowWindow(mainViewModel, null, settings);
    }
}