0

How do i center my application on the screen after i've changed the height and width of the application?

My ShellViewModel handles the DashboardRequestEvent like this:

public void Handle(DashboardRequestEvent message)
        {
            ActivateItem(IoC.Get<DashboardViewModel>());
            Application.Current.MainWindow.Height = 600;
            Application.Current.MainWindow.Width = 800;
            // How to center the application here?
        }

I've tried the answers from this link: How do you center your main window in WPF?, but none of them centers my application.

Martin
  • 15
  • 5
  • Maybe you should use the activated / loaded event in the window and change the position there, because the window might not be ready to handle the .top / .left updates at the time you are trying to do it in this code. I've used the code in the link and it works, so I am guessing that you are trying to do an update before the window can handle it. – Kevin Cook Nov 15 '19 at 13:20
  • @KevinCook I tried what you suggested, but it still doesn't work for me. It manages to change width and height, but it never re-centers the application. – Martin Nov 18 '19 at 07:20
  • Put a debugger and see what System.Windows.SystemParameters.PrimaryScreenWidth is set to, and when your .left is set. I can't reproduce your problem, because I cannot determine in what context your "handle" function is being called. – Kevin Cook Nov 18 '19 at 16:25
  • @KevinCook `System.Windows.SystemParameters.PrimaryScreenWidth = 1920`. `Left = 560`. ShellViewModel is listening to an event that is being published when a user clicks a button. The idea is, it will change view and resize it. The handle takes care of this. – Martin Nov 19 '19 at 07:06
  • So the window is already open, or is the window hidden/uninitialized when this call happens? – Kevin Cook Nov 19 '19 at 13:16
  • Yes, the window is open and showing. – Martin Nov 20 '19 at 13:31
  • Is this event running on the UI thread? Maybe you need to dispatch to it? – Kevin Cook Nov 20 '19 at 16:46
  • The event is published on the UIThread like this `_events.PublishOnUIThread(new DashboardRequestEvent());` – Martin Nov 21 '19 at 08:02
  • I've never used Caliburn micro, but if it has problems doing simple stuff like this, I'm glad I've never had to use it, seems like junk... – Kevin Cook Nov 21 '19 at 12:18
  • Nah, i must be doing something wrong. – Martin Nov 21 '19 at 13:24

1 Answers1

0

I made a quick viewmodel with a RelayCommand (I use mvvmlight) tied to a button click that expands / contracts a window and centers it.

public class MoreJunk : ViewModelBase
{
    private bool HeightSet = false;
    private double oldHeight;
    private double oldWidth;

    public RelayCommand ResizeAndCenterWindowCommand { get; private set; }
    private void ResizeAndCenterWindow()
    {
        if (!HeightSet)
        {
            Application.Current.MainWindow.Height = 300;
            Application.Current.MainWindow.Width = 500;
            Application.Current.MainWindow.Top = ((SystemParameters.PrimaryScreenHeight - 300) / 2);
            Application.Current.MainWindow.Left = ((SystemParameters.PrimaryScreenWidth - 500) / 2);
            HeightSet = true;
        }
        else
        {
            Application.Current.MainWindow.Height = oldHeight;
            Application.Current.MainWindow.Width = oldWidth;
            Application.Current.MainWindow.Top = ((SystemParameters.PrimaryScreenHeight - oldHeight) / 2);
            Application.Current.MainWindow.Left = ((SystemParameters.PrimaryScreenWidth - oldWidth) / 2);
            HeightSet = false;
        }
    }

    #region " Constructor "
    /// <summary>
    /// MoreJunk
    /// </summary>
    public MoreJunk()
    {
        this.ResizeAndCenterWindowCommand = new RelayCommand(ResizeAndCenterWindow);
        this.oldHeight = Application.Current.MainWindow.Height;
        this.oldWidth = Application.Current.MainWindow.Width;
    }
    #endregion
}

I can drag and move the window, click the button, and the window centers and resizes itself without a problem.

My simple grid on the page:

<Grid>
    <Grid.RowDefinitions>
        <RowDefinition></RowDefinition>
        <RowDefinition Height="10"></RowDefinition>
        <RowDefinition></RowDefinition>
    </Grid.RowDefinitions>
    <Button Grid.Row="2" Content="SizeMe" Command="{Binding ResizeAndCenterWindowCommand}"></Button>
</Grid>
Kevin Cook
  • 1,922
  • 1
  • 15
  • 16