Adjusting aspect ratio of my wpf window using Viewbox - wpf

I want to use viewbox for maintaining my wpf window's aspect ratio. i.e if i put my application on large monitor , it sholud automatically fit to that screen and if run it on my laptop it should adjust to its screen. Please help me out on this . it should be done purely on xaml wpf using view box.

Whats the problem with viewbox ?
Just wrap viewbox around you root views Stretch property to one you like. If you want different scaling you need to write your own implementation of viewbox btw.
Example:
<Window x:Class="stackoverflowviewbox.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">
<Viewbox Stretch="Uniform">
<Grid>
<Label>
Hello world.Hello world.Hello world.Hello world.Hello world.Hello world.
</Label>
</Grid>
</Viewbox>
But it is possible to write xaml so views will look nice on different resolutions without viewbox without any problems.

Related

How to show alignment line between two windows

I have windows inside a dock panel which are resizable.
Is there a way to show an alignment line between controls, while dragging or resizing?
What you need to do is to create a new window wihthout decoration like this:
<Window
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Width="640" Height="480"
WindowStyle="None"
AllowsTransparency="True"
ResizeMode="CanResizeWithGrip">
<!-- Content -->
</Window>
Then you calculate the placement of your separator line and draw that line in this window. That is the only way I know to draw stuff "between" windows (regardless of platform, the same is true for any language/toolkit like Qt5, Java Spring etc).

WPF: MediaElement doesn't display at runtime

This concerns WPF. The problem is that my MediaElement (actually a GIF) doesn't show up at runtime (so I only get an empty screen), even though it shows perfectly in the design mode. Before citing the code I note the following:
The GIF-file in question has been added to the solution.
Its BuildAction property is set to Resource (I've checked).
When I replace the MediaElement by an Image element (and either use the same GIF-file or replace that source-file by a .png file), the image/GIF does display at runtime. Just not when it's used as a MediaElement.
Closing and re-starting Visual Studio doesn't help.
As I said (and just to emphasize, if I may), the GIF image does display in the design mode part of the screen - just not at runtime.
And here's the code:
<Window x:Class="Testing.TestWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:Testing"
mc:Ignorable="d"
Title="TestWindow" WindowState="Maximized">
<StackPanel>
<MediaElement Source="pack://application:,,,/Images/untitled.gif" Stretch="Fill" Visibility="Visible" />
</StackPanel>
</Window>
Thanks.

Strange strip at the top of Windows 10 (wpf)

I have this xaml on a project that uses Caliburn micro :
<Window x:Class="Myproject.MainWindowView"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:cal="http://www.caliburnproject.org"
mc:Ignorable="d" d:DesignHeight="800" d:DesignWidth="1024" WindowStyle="None" Background="Black" >
</Window>
but when I run the application, I have a white line at the top of window :
How can I remove the line at the top?
I need a window that has no title bar, but should be resizable.
Well for the sake of easy points I suppose, the window chrome is built into the style templates and still inherited when you define WindowStyle="None" but still allow re-sizing to allow a hit spot for the manipulation event to occur. So like described in another answer you can take control of the base template and edit it to your requirements while still retaining the ability for the user to have point to invoke the re-sizing ability but with the frame thickness set to 0.
Hope this helps, cheers!

Extended WPF Toolkit - Resizing a childwindow

I´m using the librariay Extended WPF Toolkit
https://wpftoolkit.codeplex.com/
Is it possible to resize a ChildWindow like a ordinary Window?
The documentation shows some properties that seem to be related, but they are not accessible via xaml. https://wpftoolkit.codeplex.com/wikipage?title=ChildWindow
This is the example that I´m trying:
<Window x:Class="WpfApplication1.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:WpfApplication1"
xmlns:xctk="http://schemas.xceed.com/wpf/xaml/toolkit"
xmlns:xcad="http://schemas.xceed.com/wpf/xaml/avalondock"
xmlns:s="clr-namespace:System;assembly=mscorlib">
<xctk:WindowContainer>
<xctk:ChildWindow WindowBackground="Blue"
Left="75"
Top="50"
Width="275"
Height="125"
WindowState="Open" Canvas.Top="52">
<TextBlock Text="This is a Child Window" Padding="10"/>
</xctk:ChildWindow>
</xctk:WindowContainer>
</Window>
It appears as though the Resize functionality is only available in the Plus Edition of the software, the documentation can be found at the link below.
Based on the link you provided, it looks like you are using the same version as I am, which is the free one.
https://wpftoolkit.codeplex.com/wikipage?title=ChildWindow%20Plus
Kind of disappointing, we don't utilize the toolkit enough to warrant the cost.

How to relative scale size of User Control?

How to relative scale size of User Control like image (in image editor).
Example (100%):
(source: stegnar.com)
Scaled down UC (70%):
(source: stegnar.com)
Well I achieve this in picture editor, but I would like in WPF. :) I need this to adjust my application to different screen resolution, while nothing hiding (no scrollbars).
You could try the ViewBox control that scales up/down its content so that it fills the available space.
<Window x:Class="WpfApplication1.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:WpfApplication1"
Title="Window1">
<Grid>
<Viewbox StretchDirection="Both" Stretch="Uniform">
<local:UserControl1 Height="600" Width="600"/>
</Viewbox>
</Grid>
you can place the whole container into a ViewBox
<Viewbox StretchDirection="Both" Stretch="Uniform">
<Grid>...</Grid>
</Viewbox>
you don't have to place each single textblock in it!
Use of Viewbox (as said by Milan Nankov) is a great idea. One thing that I must warn you is that it also zooms in or out other visual aspects as well.
For example, a Textbox with dimension 200 X 1000 is very different from a Textbox with dimension 20 X 100 zoomed in 10x.
WPF provides many layouting options which can change dimension of the controls according to the size of the container. But it doesn't change the size of the text. Viewbox overcomes this issue, but it introduces another issue. Check the image below which shows the same textbox in a viewbox before and after zooming.
One trick which could be used is to place every textblock in a viewbox. But I guess that would be an overkill, and I seriously don't having any backing for this trick. Please do check for yourself and reply whether it's practical or not.
Another trick could be to bind the control's height to the font size. We would be needing a converter in that case. Please refer to this reply.. Resize font in TextBox in Grid

Resources