It seems that I am unable to set the Height Property of a Window element to 0. Is there an explanation for that?
This code does not works, nor does forcing Height to 0 in Code behind works. ActualHeight always returns 14.0 for my machine
<Window x:Class="AnimWindow.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="0" Width="525" AllowsTransparency="True" WindowStyle="None" Background="YellowGreen" >
</Window>
Hint appreciated.
Edit: Sorry, forgot an explanation :) I would like to create some sort of notification popup window like outlook does to notify user of something. While opacity works well, animating or setting height to 0 does not.
Since you have no borders, just Hide and Show the window instead of setting it's Height to zero or a positive value.
Well, I spent a little research. First, this code:
<Window x:Class="ZeroHwindow.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Height="0" Width="525"
WindowStyle="None"
AllowsTransparency="True"
Background="Yellow" />
Returns the value of the height of 6, because you have 14. I run this code on Windows XP, I suspect that you have a different OS. Next I set the parameter ResizeMode in NoResize, and got the height 2.
If you set ResizeMode="CanResizeWithGrip", we get as much as 17 pixels, which would fit the Grip. We see therefore that the system itself inserts standard elements, even when the parameters are: WindowStyle="None", AllowsTransparency="True".
I also tried to set the parameters: ShowInTaskbar = False, ShowActivated = False, no avail, the window was not visible, but the height was 2 (it turns out that some people believe nothing to these parameters, in fact, height / width is not zero).
By the way, I forgot to mention: I showed all the values in the
ContentRendered="Window_ContentRendered"
like that:
private void Window_ContentRendered(object sender, EventArgs e)
{
MessageBox.Show(this.Height.ToString());
MessageBox.Show(this.ActualHeight.ToString());
}
Just trying to set the SizeToContent = WidthAndHeight: the same height - 2, but the Window is not visible.
The only thing that somehow helped, it:
private void Window_ContentRendered(object sender, EventArgs e)
{
this.Measure(new Size(double.PositiveInfinity, double.PositiveInfinity));
this.Arrange(new Rect(0, 0, 0, 0));
MessageBox.Show(this.Height.ToString());
MessageBox.Show(this.ActualHeight.ToString());
}
In thic case, ActualHeight return 0.
Perhaps a standard element is drawn, and it is impossible to get 0. I also tried to set Styles / Templates, but the height was not set to zero. In principle, as expected, for sure it is set at the system level.
Still decided to look at it through the Snoop.
Part #1. Standard state
You can see that the local value is set high.
Part #2. Using Arrange and Measure
Some links:
UIElement.Arrange
UIElement.Measure
Related
I need to get the dynamic Height and Width of my Viewbox
The hierarchy of my xaml is like this:
<Grid>
<DockPanel>
<Border>
<ViewBox/>
</border>
</DockPanel>
</Grid>
The Viewbox Width is on "Auto" and the Height is bound to a variable in my Controller.
The Class hierarchy is like this:
Xaml <--> Controller
(the xaml.cs is mostly ignored)
Now my problem is that even in the direct code-behind (xaml.cs), I get 0 as ActualWidth and ActualHeight. If I change the dimension of my mainwindow I don't even get the standard Width and Height not to mention the dynamic one.
If Code-Snippets or information are missing, please tell me.
Here is the image with the red border. You have to look closey at the bottom (a small red Border): enter image description here
Regards
Richard
What I have tried:
I tried to get the Dimensions within the Code-Behind:
Height/Width (Only get the standard Height and NaN for Width)
ActualHeight/ActualWidth (both are 0)
RenderSize.Height/RenderSize.Width (both are 0)
I also tried the solution in this post:
Getting current size of WPF controls
It was the reason why I used the RenderSize solution
I end this question because the mainquestion was answered.
It was answered in the comments from Kamil Solecki
hello I have the following problem: I want to draw a rectangle on the canvas with methods Canvas.SetLeft() and Canvas.SetTop().
I use the method UserControl_Loaded() and everything works.
the problem is that having ActualWidth when resizing the window and therefore the grid, the value does not change and I left with the values no longer accurate.
private void UserControl_Loaded(object sender, RoutedEventArgs e)
{
Rectangle rett = new Rectangle();
rett.Height = grid1.ActualHeight-10;
rett.Width = grid1.ActualWidth -10;
rett.Fill = new SolidColorBrush(Colors.LightBlue);
canv.Children.Add(rett);
Canvas.SetLeft(rett, 10);
Canvas.SetTop(rett, 10);
}
this is the xaml:
<Grid x:Name="grid1">
<Canvas x:Name="canv" Height="auto" Width="auto"></Canvas>
</Grid>
in the first picture it is fine when not resize the window.
the second when I resize the grid remains the previous width.
I want the width of the rectangle was updated when changing the width of the grid.
Thank you.
Without a good, minimal, complete code example that clearly illustrates your question, along with a detailed explanation of what you're actually trying to accomplish (especially in a broader sense), it is impossible to know for sure what the best answer in your case would be.
Taking your question literally, it seems one possible approach would be to bind the Rectangle dimensions to the Grid's dimensions, so that they are updated as the Grid changes size. You can use IValueConverter to subtract the appropriate amount from the actual dimensions.
But that's a fairly complicated solution for what would otherwise be a reasonably simple problem, and especially so given that you seem to be doing this in code-behind for some reason (not ideal in the first place, and setting up bindings in code-behind is particularly tedious).
Idiomatically, what you should probably be doing is not putting the Rectangle in the Canvas at all, but rather making it a child of the Grid directly. Then you can set its alignments to Stretch so that it will fill the grid cell it's in. Finally, you can set its margins so that you have the 10 pixel gap on the top and left, and no gap on the right and bottom.
For example:
private void UserControl_Loaded(object sender, RoutedEventArgs e)
{
Rectangle rett = new Rectangle();
rett.Fill = new SolidColorBrush(Colors.LightBlue);
// NOTE: technically don't need to set these, as Stretch is the default value!
rett.HorizontalAlignment = HorizontalAlignment.Stretch;
rett.VerticalAlignment = VerticalAlignment .Stretch;
// 10 pixels of margin on top and left, none on right and bottom
rett.Margin = new Thickness(10, 10, 0, 0);
grid1.Children.Add(rett);
}
Doing it as above allows the XAML layout engine to automatically handle the resizing behavior you are looking for.
All that said, I would definitely encourage you to implement this in XAML instead of code-behind. There are a lot of things code-behind is good at, but frankly XAML is much better at any of the things directly related to the configuration of your GUI object graph.
Correct me if I'm wrong, but I believe that if I draw a line in WPF and it is small enough, then the line will be aliased out. Also if a line is not drawn on a pixel then it may become dimmer. I assume that if a line is < 1 pixel wide it will also be dimmed by averaging the colors that are in that spot.
My goal is to draw a graph of a bunch of signals without having the lines I draw disappear or become dimmed to the point the user might not notice them.
Questions: if I draw a line with a width of 1 device independent pixels, is it guaranteed to be at least one pixel wide? if not, is there any way to ensure that a line is at least one pixel wide?
You have to speficy
SnapsToDevicePixels="True"
in the header of your XAML
ex:
<Window
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"
SnapsToDevicePixels="True">
You can see the way it changes the line in this link
I'm writing a screensaver in WPF. I have the screensaver working, however, it only displays on my main monitor. Is there a way to "black out" or draw graphics to additional monitors when the user has multiple displays? I've done some searching around, but haven't found anything relevant.
UPDATE
From ananthonline's answer below, I was able to accomplish the "black out" effect on non-primary displays using the following window:
<Window x:Class="ScreenSaver.BlackOut"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Cursor="None" WindowStyle="None" ResizeMode="NoResize" Background="Black">
</Window>
and initializing one for each screen in App.xaml.cs using the following process:
foreach (Screen s in Screen.AllScreens)
{
if (s != Screen.PrimaryScreen)
{
BlackOut blackOut = new BlackOut();
blackOut.Top = s.WorkingArea.Top;
blackOut.Left = s.WorkingArea.Left;
blackOut.Width = s.WorkingArea.Width;
blackOut.Height = s.WorkingArea.Height;
blackOut.Show();
}
}
Note an import to System.Windows.Forms is required to access the Screen class.
You should be able to use the System.Drawing.Screen.* classes to set up multiple windows on each screen. Mind that you don't set each window to be maximized, but a properly sized, border less window.
Also - you might want to remember that the total bounds of the multi monitor setup may not always be a rectangle (if you plan to "union" all the bounds to create a window spanning all monitors).
How to find out size of an Canvas that created in xaml file?
for example I create an Canvas by
<Page
x:Class="MyApp.MainPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:MyApp"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d">
<Canvas x:Name="canvas" Background="White">
</Canvas>
</Page>
and full screen is white or whatever color I specified for that canvas
than in my class I tried but with no luck
double h = canvas.Height; // NaN
h = canvas.ActualHeight; // 0
so how do I found out the actual size of that canvas? or the size is 0 but than how to make the canvas full screen size?
I am new C# and metro developer and so confused how everything works compare to iOS.
Where in your code are you checking the size of the Canvas? I'm assuming you're doing it in the page constructor or somewhere else that is running before the UI layout has run. In this case, all auto sized elements (NaN height or width) still have their default size. If you check the size after layout has completed, like in a Loaded event handler in your page, then you should see the true rendered size.
If your Canvas happens to only contain one Image, you can use:
var Width = ((Image) (MyCanvas.Children[0])).Width;
var Height = ((Image) (MyCanvas.Children[0])).Height;
Of course, you can switch out the two Image casts for another element, if your Canvas contains one of some other item.
Alternatively just give the wrapped Image (or other element) a name, and reference it as MyImage.Width.