Can set design height if height is already set - c#

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.

Related

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

Custom Expression to Define Width and Height in WPF

I'm new in WPF so this is a very dummy question.
In Visual Studio -> Properties when I select a StackPanel (for example), I have the property Width. In this property I can click in the little square on the right and a menu is open. One of the option in the menu is "Custom Expression"
So here is my question:
Is possible to define Width and Height base in a Mathmatic expression?
<StackPanel Width="{Parent.Width - 100}">
</StackPanel>
Or something like that?
EDIT
I'm asking this because i'm intend to create a StackPanel that need to have a width 100 pixels lower than the Window. When window size was changed the StackPanel need to change to corresponding to this rule.
By default you cannot, and you have to use converters. Of course, especially since you are new in WPF, writing converters over and over for every simple operation like that might feel painful to you. So there are some custom markup extensions to reduce that pain. For example: https://quickconverter.codeplex.com/ (but there are others). With them it looks like that:
<Window x:Class="WpfApplication2.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:qc="http://QuickConverter.CodePlex.com/"
mc:Ignorable="d"
Title="MainWindow" Height="500" Width="500" x:Name="self">
<Grid x:Name="LayoutRoot">
<Rectangle Fill="Red"
Width="{qc:Binding '$P-100', P={Binding ElementName=self, Path=ActualWidth}}"
Height="{qc:Binding '$P-100', P={Binding ElementName=self, Path=ActualHeight}}" />
</Grid>
</Window>
Here we bound width and height of a rectangle to parent "self" element dimensions minus 100, without use of explicit converters.
You can do this with a binding to the parent element's width/height. However, instead of doing that, why not use the dynamic layout and just define your child container with a margin - in your example 50px - so that its width would be 100px less than the parent container.
So the answer to the theoretical question of "Can I set a calculated value of a property based on another element?" is yes, use a binding with a converter that performs the necessary calculation.
To the more important question of "Is there a simpler way to do create a responsive layout that takes the parent container into account?" the answer is also yes, use container composition with margins, padding and alignment to get the desired effect.

Window not Resizing to content

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.

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