Search code examples
c#wpfxamlprism

WPF Prism cannot maximize application on secondary screen


I'm writing an application with WPF Prism using MVVM. My app utilizes 1280x800px touch screen, that is set as secondary screen and has the taskbar hidden. My main screen is 1920x1080px and is set as primary. I want my app to start on the secondary screen and cover all visible space on it and here I got strange behavior. I can set the screen to display on like that: MainWindow.xaml:

<Window x:Class="MyApp.Views.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:prism="http://prismlibrary.com/"
    prism:ViewModelLocator.AutoWireViewModel="True"
    WindowState="{Binding WinState}"
    WindowStyle="{Binding WinStyle}"
    Left="{Binding PositionX}"
    Top="{Binding PositionY}"
    Height="{Binding Height}"
    Width="{Binding Width}"
    Title="{Binding Title}" >
<Grid>
    <ContentControl prism:RegionManager.RegionName="ContentRegion" />
</Grid>

MainWindowViewModel.cs:

using Prism.Mvvm;
using System.Windows;
using System.Windows.Forms;
using System.Linq;

namespace MyApp.ViewModels
{
    public class MainWindowViewModel : BindableBase
    {
        public string Title
        {
            get { return _title; }
            set { SetProperty(ref _title, value); }
        }

        public WindowState WinState
        {
            get { return windowState; }
            set { SetProperty(ref windowState, value); }
        }

        public WindowStyle WinStyle
        {
            get { return windowStyle; }
            set { SetProperty(ref windowStyle, value); }
        }

        public double PositionX
        {
            get { return positionX; }
            set { SetProperty(ref positionX, value); }
        }

        public double PositionY
        {
            get { return positionY; }
            set { SetProperty(ref positionY, value); }
        }

        public int Height
        {
            get { return height; }
            set { SetProperty(ref height, value); }
        }

        public int Width
        {
            get { return width; }
            set { SetProperty(ref width, value); }
        }

        #region Fields
        private string _title = "";
        private WindowState windowState;
        private WindowStyle windowStyle;
        private double positionX;
        private double positionY;
        private int height;
        private int width;
        #endregion

        public MainWindowViewModel()
        {
            InitScreen();
        }

        private void InitScreen()
        {
            var screens = Screen.AllScreens.ToList();
            var screen = Screen.AllScreens.Where(s => !s.Primary).First();
            WinState = WindowState.Normal;
            WinStyle = WindowStyle.SingleBorderWindow;
            if(screen != null) //Secondary screen detected
            {
                //Set second screen as app display screen
                this.PositionX = screen.WorkingArea.Left;
                this.PositionY = screen.WorkingArea.Top;
                this.Height = screen.WorkingArea.Height;
                this.Width = screen.WorkingArea.Width;
                this.WinState = WindowState.Maximized;
                this.WinStyle = WindowStyle.None;
            }
        }
    }
}

Tried also code-behind approach and set similar copy paste this method (with modifications to use embedded WPF properties, not my properties bond to them) to MainWindow.xaml.cs, but it gave me the same results. My initial problem is that I can set app to display on the second screen but when I try to maximize it programmatically but it makes the app display (maximized) on my primary screen (if I do if with maximize button it works as expected). In the method above I set WindowStyle to normal at first because of this answer, but it also didn't help.

I've used such workaround but still didn't got what I expected. I've hidden taskbar on the second screen and set my window height and width to the height and width of the second screen but for some reasons it doesn't match the screen properly (I'got bars on left and right of the app). It's quite strange, because if I inspect the secondary screen data I got such info:

{
    Bounds = {
        X = -1280 
        Y = 272 
        Width = 1280 
        Height = 800
    } 
    WorkingArea = {
        X = -1280 
        Y = 272 
        Width = 1280 
        Height = 800
    } 
    Primary = false 
    DeviceName = "\\\\.\\DISPLAY2"
}

Why is Y in both WorkingArea and Bounds set to 272?

TL;DR: I can set my app to display on secondary monitor, but whan I maximize it it's displayed on the primary monitor. When I hidden taskbar on secondary monitor, set WindowStyle to None (Borderless) and Window size to screen size it doesn't cover all of the secondary screen (I got uncovered area on right and left of the window).


Solution

  • It seems disabling control resizing solved the problems.