I have added canvas in the xaml file. From code iam creating two rectangles and adding it to the canvas. Here the size of both the triangles are same. When I run the application both the triangles are coming one upon the other I mean they are overlapping. But when I add them to a Stack panel they are coming one after the other?
Can anyone tell me how can I render two rectangles in my example one after the other without overlapping each other using Canvas?
Here is the sample code of my app;
Rectangle rect1 = new Rectangle();
rect1.Margin = new Thickness(1.5, 2, 1, 1);
rect1.Height = 40;
rect1.Width = 60;
rect1.Stroke = new SolidColorBrush(Colors.Black);
myCanvas1.Children.Add(rect1);
Rectangle rect2 = new Rectangle();
rect2.Height = 40;
rect2.Width = 60;
rect2.Stroke = new SolidColorBrush(Colors.Black);
myCanvas1.Children.Add(rect2);
Thanks in advance
Padma
A StackPanel arranges its Child elements automatically so that they don't overlap. A Canvas doesn't, as it arranges its Child elements according to their Canvas.Left and Canvas.Top attached properties.
Assign your Rectangles those properties to arrange them manually how you like.
Related
I need to add markers to my map. Problem: I'm using WPF, not WinForms.
GMapMarker marker = new GMapMarker(new PointLatLng(-25.966688, 32.580528));
gmap.Markers.Add(marker);
Now according to this question the solution is:
marker.Shape = new MarkerShape(....);
Could someone explain to me, how to I initalize this shape?
Thanks!
I resolved the problem with:
marker.Shape = new Ellipse
{
Width = 10,
Height = 10,
Stroke = Brushes.Black,
StrokeThickness = 1.5
};
That's a little black circle.
You have to add a new UserControl - your own, and inside the control put a image you like (for example pin image). Note that all the events (like Click event) must be implement inside a control.
After that you can add the marker like:
GMapMarker marker = new GMapMarker(new PointLatLng(##, ##));
marker.Shape = new PinControl();
gmap.Markers.Add(marker);
I need to animate multiple properties of one ui element at the same time.
For example, decreasing width and height of windows synchronously.
Any idea?
DoubleAnimation widthAnimation = new DoubleAnimation
{
To = 0,
Duration = TimeSpan.FromSeconds(5)
};
DoubleAnimation heightAnimation = new DoubleAnimation
{
To = 0,
Duration = TimeSpan.FromSeconds(5)
};
Storyboard.SetTargetProperty(widthAnimation, new PropertyPath(Window.WidthProperty));
Storyboard.SetTarget(widthAnimation, this);
Storyboard.SetTargetProperty(heightAnimation, new PropertyPath(Window.HeightProperty));
Storyboard.SetTarget(heightAnimation, this);
Storyboard s = new Storyboard();
s.Completed += FadeOut_Completed;
s.Children.Add(widthAnimation);
s.Children.Add(heightAnimation);
this.BeginStoryboard(s, HandoffBehavior.SnapshotAndReplace, true);
It will do animations step by step; height will change after width changes are complete! :|
After looking at your code I understand that you are trying to animate the width and height of Window simultenousely
But I would regret to tell you that since window is not an actual wpf component but a platform component. however content of window is completely controllable as expected via your code, but window is not. any such changes are routed through Pinvoke. and the issue you are facing is a known issue and the work around are bit complex
one solution is here, this uses pinvoke to animate the window's height and width
Animating a WPF window width and height
here is a bug for similar issue created at microsoft, result is (Closed, as Won't Fix)
https://connect.microsoft.com/VisualStudio/feedback/details/715415/window-width-height-animation-in-wpf-got-broken-on-net-framework-4-0
Extra
below is a sample which is not actually solving your problem but will help you to reduce the number of lines you need to perform such animations for other elements. It is a rewrite of your code in less lines
DoubleAnimation anim = new DoubleAnimation
{
To = 0,
Duration = TimeSpan.FromSeconds(5)
};
border.BeginAnimation(Border.HeightProperty, anim);
border.BeginAnimation(Border.WidthProperty, anim);
try this code with any element except window, I used a border with some color filled
apologies for overlooking the Window in your code at first sight
I have a canvas which is in scrollviewer. This canvas is a dynamic canvas on which many other controls can be dropped. The scrollviewer will be shown when controls are dragged & dropped out of canvas regions ie., right and bottom.
I have implemented the code for zoom in and zoom out of the canvas which works fine, but failed to find the code to implement fitting dynamic canvas content to the actual canvas size.
I'm looking for something to automatically fit the canvas content to its size, ie calculate the zoomscale as to fit the content to the canvas size.
This is a complicated one.
You should work with some properties of the ScrollViewer to get the desired result: ContentHorizontalOffset, ContentVerticalOffset, ViewportWidth, ViewportHeight (among others existing in the component..)
Here is a snippet of code that helped me:
Point realSize = new Point(this.canvasAreas.ActualHeight, this.canvasAreas.ActualWidth);
Point sizeAvailable = new Point(this.scroolMain.ActualHeight, this.scroolMain.ActualWidth);
double scaleX = sizeAvailable.X / realSize.X;
double scaleY = sizeAvailable.Y / realSize.Y;
double newScale = Math.Round(Math.Min(scaleX, scaleY), 2);
this.gridScaleZoom.ScaleX = newScale;
this.gridScaleZoom.ScaleY = newScale;
There are several articles on CodeProject and looking on google that can assist you in working with ScrollViewer zoom.
Although most are complicated and complex. Hope this helps you a bit.
Here is my xaml code:
and on Button click I would like to scale accordingly.I have tried the below code.
Point realSize = new Point(this.formCanvas.ActualHeight, this.formCanvas.ActualWidth);
Point sizeAvailable = new Point(this.myScrollViewer.ActualHeight, this.myScrollViewer.ActualWidth);
double scaleX = sizeAvailable.X / realSize.X;
double scaleY = sizeAvailable.Y / realSize.Y;
double newScale = Math.Round(Math.Min(scaleX, scaleY), 2);
this.canvasScale.ScaleX = newScale;
this.canvasScale.ScaleY = newScale;
It also results the same.
Please correct the code if it is incorrect.
Render a WPF control to a bitmap image is not a trivial task as I found out today. As I know now dealing with the parent control margin is a problem, as Rick Strahl wrote in his blog
http://www.west-wind.com/weblog/posts/2007/Sep/10/Rendering-a-WPF-Container-to-Bitmap
So far I am able to create bitmaps of any control that is visible inside a window but what I really need to do is create bitmaps of invisible controls. I just create them in code - simples shapes like rectangle and ellipse - and want to save them as bitmaps to disk.
For me this turn out to be a personal nightmare. Since my ActualHeight and ActualWidth is always 0 I use Height and Width instead. But all I get is a empty image in the size of my control.
How can I create a bitmap of any control without adding it to a visible window?
New elements haven't had their layout performed. You need to call Measure and Arrange on the control before you render it.
Canvas c = new Canvas();
Rectangle r = new Rectangle
{
Fill = Brushes.Orange,
Width = 200,
Height = 100
};
Ellipse e = new Ellipse
{
Fill = Brushes.DodgerBlue,
Width = 100,
Height = 100
};
Canvas.SetLeft(e, 150);
c.Children.Add(r);
c.Children.Add(e);
var size = new Size(250,100);
c.Measure(size);
c.Arrange(new Rect(size));
RenderTargetBitmap bmp = new RenderTargetBitmap(250, 100, 96, 96, PixelFormats.Pbgra32);
bmp.Render(c);
PngBitmapEncoder enc = new PngBitmapEncoder();
enc.Frames.Add(BitmapFrame.Create(bmp));
using(var s = File.OpenWrite("image.png"))
enc.Save(s);
How do you get an image to be reflected in wpf without using the xaml? I have a rectangle with a BitmapImageBrush as its fill. I want to be able to regularly flip this image (X-axis) back and forth at will in C#. I've read about using the ScaleTransform property but this only seems to work in the xaml and that's not what I want.
This is how the image is created:
ImageBrush l = new ImageBrush(new BitmapImage(new Uri(uriPath, UriKind.Relative)));
_animation.Add(l);
_visual = new Rectangle();
_visual.Fill = _animation[0];
_visual.Width = width;
_visual.Height = height;
_visual.Visibility = Visibility.Visible;
_window.GameCanvas.Children.Add(_visual);
_animation is a list of ImageBrushes.
This is really simple, yet I can't seem to figure out how to do it. Please help.
You can still add a scale transform programmatically, rather than using xaml.
For instance, in this article.
To do the flip, set your scale transform to be negative in the direction you want to flip (ie, if horizontal, set x to -1, or -desiredScale if you also want to resize).