WPF change window height - c#

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).

Related

Set Width of TextBlock to Screen size in XAML UWP

I have some textblocks included in a scroll viewer. I don't want to use the horizontal scrollbar so I came up with this idea but I don't know how to do this.
If you're referring to Screen size as the immediate window or page or just view of whatever in XAML you can easily bind the TextBlock.Width to the ActualWidth of that hosting element.
If you're talking about the actual monitor screen size then you just need to create a ViewModel with that property exposed and then bind that the same way.
If you need code let me know.
TextBlock has a property called TextWrapping. This will allow you the data in your TextBlock Not to overflow but increase the Height of TextBlock Itself. Also make sure to set VerticalAlignment to Stretch.

Keyboard Behavior on ContentDialog with Windows 10 Mobile (UWP)

When I open a content dialog programmatically, the first objet catching the focus and the content dialog's shape was distorted.
Are any way available show the keyboard on the content dialog without distort the general shape ?
Thanks.
Screenshot:
When the keyboard shows, the ContentDialog will adjust its height automatically. And this will cause the change of ContentDialog's Content's height. So when the keyboard shows, the Content's height becomes small and the rest of the Content is blocked.
If you want the keyboard shows without distorting the general shape, you can set the MinHeight property of ContentDialog. For example, you can give ContentDialog a large MinHeight like "500"
<ContentDialog x:Name="contentDialog" MinHeight="500" />
Or
contentDialog.MinHeight = 500;
After this, when the ContentDialog adjust its height, its height will be 500 at least and if this height is large enough, it will not distort the general shape. You may set the MinHeight equal to ContentDialog's default height to make sure it's large enough.

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"

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

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.

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