How to get the Design time height/width of a wpf usercontrol - c#

Is there a way to get the size at runtime ?
I need to display the usercontrol in a dialog and need to size the window accordingly
Since there are multiple usercontrols, looking for make it generic if possible !!

Sounds like you might need to rethink the problem? If you need the design time height and width, then just set the Width and Height of the control you are working with explicitly. This will 'default' the control to a specific size. Normally, you'd probably want the control's width and height to be set to Auto and have the container or layout manager decide what the size should be. So if you put the control in a Grid, assign it to a quadrant and set the size there.
Finally, if you're asking because you are designing with Blend and resizing the user control sets the design time, you can resize the control explicitly by selecting the inside corner notch instead of the outside corner notch with the bigger handle. The inside notch will result in Height and Width being set explicitly.

You can use SizeToContent
<Window x:Class="WindowSample.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="WPF Window"
SizeToContent="WidthAndHeight" >
This will make your window automatically resize itself to fit the preferred size of your window contents.

Related

WPF change window height

I am using a WPF Window with contains a frame where I add the content of different pages I have.
The problem is that all of these pages have the same Width but different Height.
So what I would like to do is to be able to set the window's height, to the height of the page inside of the window's frame.
Thanks
Changing the window height, aside from looking horrible to the end user, comes with all sorts of complications. For example, what if your content has a greater height than the screen can display? Would your window go off the screen?
A better option may be to look into a control that gives a scroll bar if the content exceeds the window height.
In wpf you can set window property
SizeToContent="WidthAndHeight"
or
SizeToContent="Height"
As #Joeb454 already answered this may looks horrible during navigation (user-experience-wise).

Create a portable WPF UI deployed on different resolution

I have tried viewbox which stretch the views to the border of the window distorting the views.
What are the methods out there to resize the views of the all controls, textfont dynamically when the application runs on different monitors?
SystemParameters Class allows you to retrieve primary screen size (and many other values). With a simple binding like
<Window ...
Height="{x:Static SystemParameters.PrimaryScreenHeight}"
Width="{x:Static SystemParameters.PrimaryScreenWidth}" />
you will be able to change the size of your view (Window or UserControl doesn't make difference). But, if you need an adaptive layout (e.g. your Grid must fill the entire Window) it's necessary to set Dock properties, Alignment values and so on.
I would go on with HorizontalAlignment, VerticalAlignment and MinSize in this case. One of the main principle in WPF is a sizeless design. If you define them right it does not matter what is the current resolution. You also can define ratio, when you design grid columns and rows.
And as #Francesco De Lisi said, docking.
Beware of the zoom factor of your windows (100% / 125% / 150% / 200%). You can get the real screen size (visible pixel) by using the following property:
SystemParameters.FullPrimaryScreenHeight
SystemParameters.FullPrimaryScreenWidth

GUI Window Size C# WPF

I want to set the window to an exact size for creating a game, since I will use the X and Y coordinates of the screen to move things. The problem is that when setting the window width and height it includes the border in that size, so your actual size is smaller than what you specify. What can I do about this?
You set the width and height to the panel inside of the window instead. Usually it's a Grid panel unless you have changed it. You can then set your window's SizeToContent property so it can autosize to fit the Grid.
Or you could do like this maybe: myForm.Width = yourValue + borderSize;
If your window is a WPF window try using AllowsTransparency="True" and WindowStyle="None"

SizeToContent on UserControl

In fact the UserControl lacks the property 'SizeToContent' that we have in Window.
So the question is:
what's the easiest and right way to simulate SizeToContent=WidthAndHeight behavior on UserControl?
UPD... yeah I know it suppose to get that automatically if no Height and Width defined in the container where you're placing a user control.
But it doesn't work when you placing one userControl with defined sizes, into another with no sizes, and altogether they go inside the container.
it this case your second control will take all the space it can get.
Use a Grid and set either the Row and Column height to * for the items you want to size to the window.
Just don't set the Width and Height properties. It will then take on whatever width and height its child requires.

How do I create a user control that can be sized larger than its created size with WPF

I've created a user control using WPF and I want to add it to window. I've done that, but I can't make my control have a height higher than the height it has in its own xaml file. My MaxWidth and MaxHeight are both infinity, but I can't make the control any taller than what it is in its xaml file.
To get around this, I have to make all my user control enormous so I'll be able to size them to whatever I want. This doesn't seem right, I have to be missing something.
Removing the height and width is the way to go. The designer(blend) has some special designer width and height properties that they can use to design in, but won't set the height for runtime.
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d" d:DesignWidth="412" d:DesignHeight="230"
That is the xaml that will be at the top of the Window/UserControl. This should help explain things.
Why do you want your control to have a height higher than the height it has in its own XAML file? Couldn't you just remove the height in the control's XAML file, and explicitily set the height of the control when you declare it in the other XAML files (or code) that use it?
If I remove the height and width in the controls XAML file I lose the ability to use the designer for my user control. So short answer, that did solve my problem, but now I can't use the designer for user controls. Doesn't seem like I'm any better off.
The problem could be that the inner controls in your user control aren't stretching to your control. Try setting HorizontalAlignment="Stretch" or Width="Auto" on the inner controls, or you could try binding the Width property.
Ok, after some further investigation, I've misspoken. its the Grid thats causing the problem. If I set the grid Width and Heigth to Auto then everything works fine, but I lose the ability to use the designer.
I have all of the alignments set to Stretch for both the Grid and its controls.
So in summary, everything works fine if I set Grid.Width = Auto and Grid.Height = Auto, but when i do that, I lose the ability to use the designer.
I'm not aware of any width/height attributes for the VS designer if that's what you're using. I've used the MinWidth/MinHeight attributes in my xaml pretty effectively, however, to deal with the situation that I think you're describing.

Categories