Silverlight horizontal stretch and get position issue - silverlight

I have a Grid (container) wich in turn has several grids(subContainers) arranged by rows. Each one of those "subContainers" has diferent columns and controls. And each of those "subContainers" has the horizontal alignment set to stretch, and it has to stay that way, since the layout this viewer depends on it.
I use the "container" to set each control on it's adequate position. So far so good. Now comes my headache... I want to remove the control from the grid and put it in a canvas, at the same exact position, only, the position it returns is as if the control is set to the beggining of the grid and not it's true position.
For testing purposes, I've set the "subContainters" horizontal alignment to center and (despite the layout is totally wrong) every control is in it's right position when sent to a canvas, wich it doesn't happen when HA = stretch.
Here's the code I'm using to get position:
GeneralTransform gt = nc.TransformToVisual(gridZoom);
Point offset = gt.Transform(new Point());
So you can understand, for example, my first control should be somewhere like (80, 1090), but the point that I get is (3,3).
Can anyone help me? Thanks

You should try using the top level container (which contains all other subcontainers) to get the offset point.
Also you can try:
var transform = mySubSubSubElement.TransformToVisual(Application.Current.RootVisual);
var position = transform.Transform(new Point(0, 0));

Related

Scaling multiple objects relative to a bounding rectangle

I'm trying to figure out the algorithm for scaling multiple selected objects on a canvas (similar to Visio's behavior). Say for instance I have the following selected objects in my application:
I then drag the lower-right handle of the bounding box to increase the size of the selected objects and thus produce the following results:
My questions are as follows:
How do I get the amount of scaling to be applied to each object?
How do I get the amount of translation to be applied to each object?
I hope this question makes sense. And I hope you could help.
Hi I dont think there is any Translation , There is only Scaleing . One easy way to do that is preserve the Width and Height of your object like (TextBoxes above) and then when you want to get Scaleing values of that object
ScaleTransform scale = new ScaleTransform();
//_text is the scaled object
scale.ScaleX = text.ActualWidth - _width; //_width is width of the textbox at beginning.
scale.ScaleY = text.ActualHeight - _height; //_height is the height of textbox at the beginning.
This will give you the amount by which object is scaled corressponding to the Width and Height of TextBox at the beginning (i.e When window initialized) . I hyope this will give you an idea.

Print With No Margin

I am attempting to print a WPF control 4" tall by 3" wide.
I have used a ScaleTransform on the control (a Canvas) to scale it accordingly; however, when I print to a printer part of the image is cut off (the top and left edges).
According to this thread:
The reason of this problem is that the printer provides an unprinted margin around the edge of the paper, but the PrintDialog.PrintVisual method intends to print to the edge of the paper. So the area that lies in the unprinted margin around the edge of the paper is clipped.
The thread fails to mention how to retrieve the margins or how to force the printer to ignore these margins. How do I obtain these values so that I can print using WPF without clipping?
You'll need to combine information from the PrintDocumentImageableArea with the Measure and Arrange members on your UIElement:
// I could not find another way to change the margins other than the dialog
var result = printDialog.ShowDialog();
if (result.HasValue && result.Value)
{
var queue = printDialog.PrintQueue;
// Contains extents and offsets
var area = queue.GetPrintCapabilities(printDialog.PrintTicket)
.PageImageableArea;
// scale = area.ExtentWidth and area.ExtentHeight and your UIElement's bounds
// margin = area.OriginWidth and area.OriginHeight
// 1. Use the scale in your ScaleTransform
// 2. Use the margin and extent information to Measure and Arrange
// 3. Print the visual
}

How to Change the Position of a Canvas'es Origin Point

I'm working on a project that allows users to drawing something, and save it in the database.
Basically, the user draws on a canvas, the canvas is 5 time bigger than the screen size, and the canvas is still in the center of the screen (or the screen is in the center of the canvas).
In order to make it flexible for the canvas size (we want to change the size of the canvas in the future), we want to set the center of the canvas as the Origin Point, that is, (0, 0) is in the center of the canvas. So when the mouse clicks somewhere, the point I get is measured based on the center of the canvas.
Maybe you would say:
Canvas.RenderTransformOrigin = (.5,.5). But I'm not asking the rotating center, so it's not the solution.
Canvas.RenderTransform = TranslateTransform (Canvas.Width/2.0, Canvas.Height/2.0). But this just move all the objects on the canvas to the center.
You may also say that, why don't we just translate the MouseClick points with an offset Canvas.Width/2.0, Canvas.Height/2.0, and then translate them back when rendering. We could do that, but too much effort, because we have a lot of other operations to be implemented, so every time we have to translate back and forth.
It looks like you have thought of almost every possible approach to this problem, however, I will see if I can add just one more!
The Canvas properties, Left and Top, which dictate an elements position are attached properties. You could create your own attached properties, OffsetLeft, OffsetTop which allow you to position elements based on the origin you require. These would be simple to implement, when they are get / set, just handle their change event to set the respective Canvas attached properties.

Take object "out of the document flow" in Silverlight, with the same effect as CSS Absolute Positioning?

When using Silverlight 4, is it possible to set an Image to an absolute position, and bring it out of the document flow, allowing it to be positioned freely of any grids etc?
With CSS you can set an element to use absolute positioning, and then it will be positioned absolute, based on the first relative parent above it.
I want to be able to place an Image, anywhere on the screen, above anything else on the page, but in Silverlight.
I tried absolute positioning (In the code behind) and it doesn't seem to be positioned correctly, it looks as though it defaults to both Horizontal Alignment and Vertical alignment as
"Centre"
CustomIcon.Source = new BitmapImage(new Uri("http://media.trueachievements.com/imagestore/0000149800/149834.jpg", UriKind.Absolute));
CustomIcon.SetValue(Canvas.LeftProperty, Pt.X);
CustomIcon.SetValue(Canvas.TopProperty, Pt.Y);
CustomIcon.Visibility = System.Windows.Visibility.Visible;
Pt is set correctly elsewhere (Checked this when Debugging).
The image is in the Xaml with the x:Name attribute set, and is set to Collapsed visibility by default.
Any ideas if it's possible to get the same effect I described (CSS) but using Silverlight 4?
The canvas Left and Top attached properties only have an effect if you actually add the control to an Canvas element.
Just add a Canvas element to your xaml as the last element in the "LayoutRoot" grid. You do not need to set its width or height nor should you set it Grid.Row or Column.
Now when you add items to this canvas they can be positioned anywhere.

scrollviewer and canvas - scrolling view to given position

I have some problems with implementing autoscrolling in WPF (I think I could call it that way).
I have a canvas placed inside a scrollviwer. On my canvas I can dynamicly add different shapes. The position of this shapes can be changed with mouse. Everytime I add new shape on canvas or change position of shape I fire measureOverride function.Thanks to this scrollview "know" the real size of canvas and the scrollbars appear. However even if scrolbars appear, the view doesn't "follow" shape which I currently move. I mean if I reach visible part of canvas I would like canvas to srcoll.
I was trying to use this function
ScrollToHorizontalOffset()
However I have problem with proper use of that function. I was trying to use (as a parameter) canvas actualwidth but it didn't work well. I also was trying to use as a parameter current position of shape (which I move) but it works only one way. I the viewer follow the moving element if I was moving this element to right side of canvas. However if I move shape back(to the left) the view don't follow the shape.
I hope somebody will understand this :) It is hard to explain my problem.
I also was trying to use as a
parameter current position of shape
That is the correct way to implement. Waht you need is a converter, which will returns the position according to the direction you move the object.

Resources