Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

WPF window layout looks different from VS designer when running

Tags:

.net

wpf

I've recently been playing around with WPF a little bit. I'm a little confused as my program looks different when it's running from what I created in the designer.

I'm certain that there is a valid reason for this but I can't wrap my head around why something so basic has to be so "mysterious".

To be specific, I mean the bottom and right margin between the button and the inner border of the window.

Designer:

Running program:

Hope someone can help with this.

XAML:

<Window x:Class="WpfApplication1.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="MainWindow" Height="350" Width="525">
<Grid>
    <Button Content="Button" HorizontalAlignment="Left" Margin="432,289,0,0"
            VerticalAlignment="Top" Width="75" RenderTransformOrigin="-1.387,-0.75"/>
</Grid>

like image 337
bitfrickler Avatar asked Sep 30 '13 18:09

bitfrickler


People also ask

Is WPF still in demand?

However, some industries still rely on WPF concepts and tools like XAML & MVVM to develop futuristic web, mobile, and desktop applications. None of Microsoft's GUI frameworks ever really die. WPF has top-notch performance, with a high level of customization, and if you aim for Windows, both WPF is critical.

What is the difference between WPF window and WPF page?

Window is the root control that must be used to hold/host other controls (e.g. Button) as container. Page is a control which can be hosted in other container controls like NavigationWindow or Frame. Page control has its own goal to serve like other controls (e.g. Button). Page is to create browser like applications.

What is difference between user control and window in WPF?

A window is managed by the OS and is placed on the desktop. A UserControl is managed by wpf and is placed in a Window or in another UserControl. Applcations could be created by have a single Window and displaying lots of UserControls in that Window.

Can we design web based app in WPF without XAML as well?

But WPF without XAML is possible. You don't even need Visual Studio installed for this, just the . NET CLI and the Developer Command Prompt. Drop these two files in a folder and run msbuild and then you can run the file in the Bin directory.


2 Answers

That's simply because :

  1. There are d:Width and d:Height that affect design time and Height and Width that affect the run time. so verify that they are both the same.
  2. If you want to keep the margin from the bottom you have to specify it in XAML or click on the little margin holders from the bottom and the right.
  3. Unless you have Expression Blend, Don't rely on the VS drag and drop, instead, write your own XAML.

Something like this would be very logical:

 HorizontalAlignment="Right" VerticalAlignment="Bottom" Margin="0,0,10,10" 
like image 81
HichemSeeSharp Avatar answered Oct 14 '22 05:10

HichemSeeSharp


I had this issue as well and found a pretty easy solution for anyone else who runs into this.

  1. Place a grid in the window, size as desired.
  2. Set window height and width to auto
  3. Set the window's SizeToContent as WidthAndHeight

Presto, the grid is a set size and the window will now size to it, no need to worry about the border or title bar.

like image 38
Lightor Avatar answered Oct 14 '22 05:10

Lightor