Window not Resizing to content - c#

I have a window:
<Window x:Class="HarryPotter.MainWindow"
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:HarryPotter"
mc:Ignorable="d"
Title="Harry Potter" SizeToContent="WidthAndHeight" Background="Black" WindowStartupLocation="CenterScreen"
>
<Viewbox Stretch="Uniform">
<Canvas Height="640" Width="480" Background="DodgerBlue">
</Canvas>
</Viewbox>
</Window>
In the designer, the ViewBox's Width = 480 and Height = 640. So the Window's Width and Height are 488 and 671. This is exactly what I want.
I want the ViewBox to be of the same size as Canvas at startup and window to size to ViewBox's size. Then I want the window to be freely resizable. However, this is not what happens. When the window opens, it's height is my monitor's height.
How can I fix this?

You are using a ViewBox to fit the content into the available space by stretching it uniformly. However, since you did not define a height or width for the available space (SizeToContent = "WidthAndHeight") it takes up all the space so that it can stretch your canvas.
To experiment you can set Height="50" and see that the window takes all the horizontal space.
To prevent it you have a few options:
Set Stretch property of ViewBox to None instead of Uniform.
Set a predefined Width or Height for your Window.
Set MaxHeight or MaxWidth property of ViewBox.

Related

Can set design height if height is already set

c# can set design height if height is set
<Controls:MetroWindow x:Class="SQL_Reports.MainWindow"
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:SQL_Reports"
xmlns:Controls="clr-namespace:MahApps.Metro.Controls;assembly=MahApps.Metro"
mc:Ignorable="d"
Width="669"
Title="Daily SQL Reports"
WindowStartupLocation="CenterScreen"
ScrollViewer.VerticalScrollBarVisibility="Auto"
ScrollViewer.CanContentScroll="True"
MinHeight="530"
TitleAlignment="Center"
MinWidth="677"
d:DesignHeight="927" >
I am trying to set the design height so i can view my content in Visual Studio
but i also want the height of the WPF to be 530 but when I set the height the designer stays to 530 and not what I set the Design Height to be.
Just get rid of the MinHeight="530" when you are designing.
And add it again when youre done.
I don't believe you can define a runtime height and a design height at the same time.
Maybe you can try the d:LayoutOverrides attribute.

How to make opacity darker in wpf

My main window backgrond color is Background="#005075" when the user click on some button some small window pops up, and then I'm chanching the main window opacity like Opacity="0.5", the problem is that it makes the background lighter, how do I make a dark opcity in wpf?
I do something similar a lot. The best way to do this is by having a couple of top-level grid's in your Window and work with those rather than the Window itself.
For example, Ill do something like this:
<Window x:Class="WpfApp9.MainWindow"
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:WpfApp9"
mc:Ignorable="d"
Title="MainWindow" Height="450" Width="800">
<Grid>
<!-- Background grid with 100% opacity -->
<Grid Background="#005075"/>
<!-- main grid, which sits on top of the background grid -->
<Grid x:Name="MainGrid" Background="Transparent" Opacity="0.5">
<!-- Windows controls go here -->
</Grid>
</Grid>
</Window>
Opacity will not make it darker. What you can do is you can make some kind of panel which is stretched all over your main window, and has a black color and opacity of 0.2 or something like that, and mainly is hidden by default. And when you want to make the main window disabled you make that panel visible. This should solve your problem.
You can also use it as a loading panel, if you add some animation in center or something like this. So it will be over all of the content of your main window and will not allow the user to interact with your main window while it's disabled.
P.S. The black panel with opacity 0.2 should be positioned over all of the controls in main window

Draggable and Auto-Resized UserControls in WPF Window

I'm making a WPF Hud, and I'm not such a WPF expert.
I need a Window with draggable usercontrols and which are auto-resized when I expand/reduce the Window.
I can achieve the first feature by using a Canvas container and using MouseMove event of the Window.
I can achieve the second feature by using a Grid container and setting childrens both properties HorizontalAlignment VerticalAlignment to Strech.
Of course I cannot use both containers, so is there a third container that can help me on doing such what requested?
Actually I'm trying to use the Canvas and determining the width/height of the childrens manually, but I don't like this approach.
Any suggestion?
Thanks,
Luca
Leaving out the details on dragging controls, etc., try something like:
<Window x:Class="AdHocWpf.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Height="439" Width="616"
Title="MainWindow">
<Viewbox>
<Canvas Width="600" Height="400" Background="Blue">
<Rectangle Stroke="Orange" Width="200" Height="100" Canvas.Left="10" Canvas.Top="5"/>
<Ellipse Stroke="Yellow" Width="350" Height="200" Canvas.Left="225" Canvas.Top="150"/>
</Canvas>
</Viewbox>
</Window>
This will create a Canvas object with an initial size of 600x400. When you resize the window the canvas and the objects within it will be scaled so that they take up the same amount of relative space. You'll need to size the containing Window appropriately as well...normal resizable windows have 39 pixels of chrome vertically and 16 pixels horizontally, so take that into account.

Position of Primary Screen in Virtual Screen

I can center a wpf window in somewhere in the primary screen or in the virtual screen. However, I want to stretch the window over the whole virtual screen and place e.g. a grid in the window in a way that it fills the primary screen. The following snipped shows how I stretch the window, add a grid and stretch the grid to fit the primary screen size, but how do I determine the position (i.e. margin) for the grid?
<Window x:Class="BC.Citrium.Screensaver.Saver.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow"
Height="{x:Static SystemParameters.VirtualScreenHeight}"
Width="{x:Static SystemParameters.VirtualScreenWidth}"
Top="{x:Static SystemParameters.VirtualScreenTop}"
Left="{x:Static SystemParameters.VirtualScreenLeft}"
ResizeMode="NoResize" WindowStyle="None">
<Grid Name="mainGrid"
Height="{x:Static SystemParameters.MaximizedPrimaryScreenHeight}"
Width="{x:Static SystemParameters.MaximizedPrimaryScreenWidth}"
Margin="???"
Background="Bisque">
</Grid>
</Window>
I could draw two windows, one to fit the virtual screen and one above that to fit the primary screen, but that's not the nice way.
The upper left corner of the primary screen is always at screen position (0, 0). Which makes the margin you want to use (-VirtualScreenLeft, -VirtualScreenTop). With adjustment for the window border width and the window caption height.

C# WPF Window Width, MaxWidth, MinWidth ignored

I have a WPF Window defined in XAML like this:
<Window x:Class="com.some.company.window"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="My Cool Window"
x:Name="CoolWindow"
Height="435"
Width="70"
MinWidth="70"
MaxWidth="70"
Left="{PropertyState Default=0}"
Top="{PropertyState Default=0}"
Initialized="InitializeWindow"
ResizeMode="NoResize"
Style="{DynamicResource DefaultWindow}">
.....
.....
</Window>
The problem is that when the Window is created and displayed on the screen - it is ALWAYS larger than the 70 pixels I specified in the width definition. The width is probably 80-90 pixels. My width attributes are ignored. None of the contents inside the Window are larger than 70 pixels either.
Even when I try to resize the window with the grips, it will not let me resize it below a specific width. Is there some reason WPF is not letting me set the width of the window smaller? Is there a hidden minimum width value for every window? and how would I get around this?
EDIT:
When I add WindowsStyle="None" into the Window attribute, the width is correctly set to 70 pixels. However, this is not the style I want for the Window.
Thanks
You have set MinWidth to 70 and therefore size of your Window cant be less than that. BTW because of control box it's width seems to have a minimum limit of 132.
If we set WindowStyle="none" to remove the title and control box, we can make the Window even smaller.
<Window x:Class="WpfApplicationUnleashed.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:sys="clr-namespace:System;assembly=mscorlib" xmlns:local="clr-namespace:WpfApplicationUnleashed"
Title="" WindowStyle="None" Width="70">
<Grid xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation">
</Grid>
</Window>
EDIT
To make the Window width to 70, while the close button, Title text still visible and no-resize use this:
<Window x:Class="WpfApplicationUnleashed.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:sys="clr-namespace:System;assembly=mscorlib" xmlns:local="clr-namespace:WpfApplicationUnleashed"
Title="My Window" WindowStyle="ToolWindow" Width="70" ResizeMode="NoResize">
<Grid xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation">
</Grid>
</Window>
Since 70 is a very small width, you cant have minimize and maximize button along with close button.
70 that you have specified is not in pixels. WPF works on points and it actually sizes window to pixels based on Screen DPI (Dots per Inch).
I was able to do what I wanted by adding WindowStyle="ToolWindow" and ResizeMode="NoResize". This allowed me to keep the titlebar and the close button, while allowing the window's width to be set at 70pt.

Categories

Resources