WPF stretch fullscreen app to cover whole screen - c#

I'm creating simple presentation app. It have to be fullscreen and UI have to be responsive eg. it have to scale depending on resolution. Is there a way to strech WPF window to over whole screen?

You should only need to do a couple things:
On your Window, set WindowState="Maximized", ResizeMode="NoResize", and WindowStyle="None".
Wrap your "slide" presenter in a Viewbox with Stretch="Uniform". That will cause the slides to scale uniformly to fill the window, while maintaining their original aspect ratio.
Set the window's background to whichever matte color you want. If the slide aspect ratio does not match the display aspect ratio, there will be empty regions to the left/right or top/bottom of the slides, and you may want to control the color of those regions. I recommend Black.

Setting the WindowState to Maximized should do the trick.
Something like this:
<Window WindowState="Maximized" WindowStyle="None">
...
</Window>

Related

WPF Maximized Window bigger than screen

When creating a WPF window with AllowsTransparency="True" WindowStyle="None" and maximizing it via this.WindowState = WindowState.Maximized; the Window gets bigger than my screen.
When setting AllowTransparency="False" i have a border around my Window, but the window won't get bigger than my screen.
In my Case I have a 1920x1080 screen and the window becomes 1934x1094.
On my 1280x1024 screen the window will become 1294x1038.
The Window will become still as big as this, whether or not AllowTransparency is enabled or not, yet with it disabled it works properly.
Setting AllowTransparency before maximizing doesen't work and throws an InvalidOperationException.
How to get a WPF window without a Windows-style border, but yet to maximize properly?
A couple year late to the party but just increase your this.borderthickness for the SizeChanged event like so:
<Window x:Class="MyApp.MainWindow"
ResizeMode="CanResize"
WindowStyle="SingleBorderWindow"
SizeChanged="Window_SizeChanged">
....
Code
....
</Window>
public void Window_SizeChanged(object sender, SizeChangedEventArgs e)
{
if (this.WindowState == WindowState.Maximized)
{
this.BorderThickness = new System.Windows.Thickness(8);
}
else
{
this.BorderThickness = new System.Windows.Thickness(0);
}
}
It seems a pretty common issue. It appears you'll have to bind your height and width to the actual height/width of the screen as stated in this StackOverflow post:
Borderless window application takes up more space than my screen resolution.
I hope that solves the issue you're facing.
If you only care about the correct dimensions of your window and have no trouble with the appearance, you can wrap the contents of the window in System.Windows.Controls.Canvas like this:
<Canvas Name="MyCanvas" Width="auto" Height="auto">
...
</Canvas>
Then you can use MyCanvas.ActualWidth and MyCanvas.ActualHeight to get the resolution of the current screen, with DPI settings taken into account and in device independent units.
It doesn't add any margins like the maximized window itself does.
It should work with multiple monitors too.
(Canvas accepts UIElements as children, so you should be able to use it with any content.)
Unfortunately there is no good solution to this other than to make a WindowStyle="None" window without resizing, and handle everything yourself. This means your own maximize/minimize/restore buttons that set the window dimensions to fit the screen. Your own live caption area for dragging. Your own borders with the appropriate cursors. Your own double-click handler to maximize/restore. Your own routine for checking the mouse position against the height of the screen for drag-to-dock. Etc. You get the idea. It's a pain in the neck, but if you do it once at least you'll have it for all future projects. Unfortunately you will lose the "Aero" animations, but alas.
I also want to point out one reason why this issue is very important. In at least some cases, WPF can't make full use of accelerated graphics when windows span two monitors (as they normally do any time a window is maximized). That means performance of D3DImage, as well as any Effect, suffers when the window is maximized. It was happening for me, and many of my users, which is what drew my attention to this issue.
set this property of you window.
MinHeight="100"
MinWidth="100"
Height="auto"
Width="auto"
Hope this will work

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

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 to resize a Canvas in WPF?

I'm writing a WPF app that has a Canvas in it. This canvas will be custom rendered at runtime. It's sort of like a game in that it needs to be measured in pixels. I need to be able to set my Canvas to 478x478 pixels (client rectangle size). I don't want any scaling or other resolution-independent steps to take place on my Canvas.
I'm not sure if this is possible in WPF, since its nature is to be resolution independent. My questions:
How do I resize my Canvas at runtime (function to call?)
When I resize my Canvas, is the renderable area (the client rectangle) going to be that size? If not, how can I resize it to make sure the client rectangle is a specific width/height?
Is it possible to set the width/height of the Canvas in Pixels? How does the resolution-independent aspect of WPF interfere with what I'm trying to do?
When I resize my Canvas, will other controls resize appropriately as they have been designed to do in the WPF designer?
Thanks in advance.
Any elements positioned in a canvas will not resize or reposition based upon the size of the canvas. So I don't think there's any advantage to setting the size of the canvas. Maybe you should just set a fixed size of the window instead.
Otherwise, just set the Canvas.Width, Height, and ClipToBounds=True and you have a fixed sized canvas that positions its child elements with X/Y coordinates.
Also you should be sure to set SnapsToDevicePixels=True on the canvas so that child elements will have crisp pixel-aligned bounds.
resizing is cake:
MyCanvas.Width = 350;
MyCanvas.Height = 450;
this sets the size, you CAN render to coordinates outside of this, but it will be clipped. You can wrap your canvas inside a scroller to allow the user to see what is outside the height/width of the canvas.
as for the rest of your questions, i think you can see this SO question for your answers
Update 2020: on WPF .NET CORE: If the Canvas is a child of a Viewbox, the content is automatically scaled to the Viewbox height and width.
<Viewbox Stretch="Fill"
Width="50"
Height="50">
<Canvas>
<Path.... />
</Canvas>
</Viewbox>

Categories