4

I have been doing Winforms for the last 10 months. Our company is considering switching to WPF. I am having issue with setting the Height and Width of the WPF to match the pixels on the screen. I want the height to be 1080 and width to be 1920 because that is my screen resolution. In Winforms it works perfect because it goes based on screen resolution, so is this not the case for WPF?

<Title="Application" 
   Height="1080" 
   Width="1920" 
   WindowStyle="ThreeDBorderWindow" 
   WindowStartupLocation="CenterScreen" 
   WindowState="Maximized" 
   HorizontalAlignment="Center" 
   VerticalAlignment="Center" 
   SizeToContent="Height">

When I place a dock panel three fourths of the way down on my designer. When I run the application it is at the bottom of my application but if I put it at the bottom it doesn't show and I assume its off the screen. How do I get the same behavior from winforms on this? When I set it on the bottom of my application it is on the bottom when I run it.

Filburt
  • 17,626
  • 12
  • 64
  • 115
  • 3
    [Device Independent Pixels](https://msdn.microsoft.com/en-us/library/windows/desktop/ff684173(v=vs.85).aspx) – Matt Burland Jun 03 '16 at 14:14
  • Are you trying to set width and height for WPF window? – Dennis Jun 03 '16 at 14:14
  • go to the xaml and set `Height` and `Width` in your `UserControl` – Khalil Khalaf Jun 03 '16 at 14:15
  • @FirstStep I have it set as that. When I run the application the content is off the screen as if the XAML is making my page bigger then my screen resolution or the height and width i stated – DotNet Programmer Jun 03 '16 at 14:17
  • Are you sure of your screen resolution and that they match? I am almost sure that xaml's Height and Width is the fix. Or just convert your MainWindows to full screen mode – Khalil Khalaf Jun 03 '16 at 14:20
  • Wouldn't [maximizing](http://stackoverflow.com/q/3121900/205233) be way easier than setting to a specific width and height if you want to cover the entire screen anyway? – Filburt Jun 03 '16 at 14:20
  • height and with refers to the inner size, it does not take in account the borders and title bar – Gusman Jun 03 '16 at 14:20
  • @Filburt I do maximize the window as you see in my line I posted – DotNet Programmer Jun 03 '16 at 14:22
  • Sorry, I missed that part at the end of your code sample. The linked post and related posts suggest that you need to handle this on App startup - it will not work just from xaml as could be expected. – Filburt Jun 03 '16 at 15:00
  • @Filburt I still get the same results when I set it in App.xaml.cs – DotNet Programmer Jun 03 '16 at 15:09
  • WPF uses Device Independent Pixels to try and ensure applications look the same regardless of screen resolutions. Typically sizes are not hardcoded, but instead controls are arranged using different [Layout Panels](http://www.codeproject.com/Articles/30904/WPF-Layouts-A-Visual-Quick-Start), and controls are positioned relatively. If you really want to use the screen size in XAML though, [you can bind to the SystemParameters](http://stackoverflow.com/a/17416804/302677) – Rachel Jun 03 '16 at 15:38

1 Answers1

5

In WPF you’re specifying size in Device-Indepdent Units (DIPs). 1 DIP = 1/96 inch.

To convert from DIPs to pixels or vice versa, you need to know your screen DPI setting (e.g. 96, 120).

pixels = (DIPs) * (DPI / 96)

DIPs = pixels / (DPI/96.0)

F.Koenig
  • 128
  • 4
  • Why would Microsoft make this type of change? It makes sense on how Winforms is done but it makes no sense to change a industry standard to something that makes no sense at all. – DotNet Programmer Jun 06 '16 at 12:59
  • 3
    It makes sense as high-resolution displays with high DPI are readily available. Your Application will have the same "look" across devices and according to individual preferences (DPI settings relative to native resolution). – F.Koenig Jun 06 '16 at 13:28