Silverlight: LineGeometry on a Canvas doesn't follow the cursor - silverlight

In Silverlight, I'm trying to draw a line on a Canvas by setting the LineGeometry element's EndPoint to the cursor position. In the Canvas' MouseMove event, I'm doing this:
line.EndPoint = e.GetPosition(this);
where "this" is the Canvas.
"line" is a LineGeometry element added to a GeometryGroup, and the GeometryGroup is added to a path object, which is added to the Canvas' Children collection.
The line does not show up on the canvas. What causes this?

This isn't an area that I understand very well, and I'm only answering because nobody else has answered yet :-). But I think the issue might be that the line's EndPoint is defined relative to its container (the GeometryGroup), but the Point structure returned by e.GetPosition(this) is relative to the containing control. You may need to apply a Transform to the e.GetPosition(this) to get the correct point. See here for more details on how transforms work.

Related

MVVM Get Control Position

I am a bit stuck in my current project.
I'm using a terrible API that needs to be told where and when to draw at any give time. This API does not expose any controls, and I need a control in order to place the drawn object properly.
What I have done is I've put a canvas in my grid in my view. This canvas takes up the space that my drawn API element needs to take up. So, by getting the canvas actualwidth and actualheight, I can draw my API element at the proper size. The issue i'm having is the position of the API element. The Canvas is in the proper place at all times, but when the program first starts, Canvas.TranslatePoint(new System.Windows.Point(0, 0), App.Current.MainWindow); returns 0,0. As soon as I manipulate the UI and cause the Canvas to Resize, the location function returns the real location and my API element draws in the proper spot. My question is why is the initial location 0? How can I remedy this? I call the initial draw function from the UserControl_Loaded event.
Thanks
P.S. Would I be wrong in thinking that the initial 0,0 is a relative coordinate, and not an absolute coordinate?
I figured out my issue - my control wasn't part of the PresentationSource. Long story short, you can't get the location of a control that isn't shown on the screen yet. My fix was this:
private void WaveformCanvas_Loaded(object sender, RoutedEventArgs e){
if((sender as Canvas).IsVisible)
{
(this.DataContext as StudioControlViewModel).MoveWaveform();
}
}
For me, the MoveWaveform() function calls WaveformCanvas.TranslatePoint(new System.Windows.Point(0, 0), App.Current.MainWindow);
So now the function only gets called when the control is visible, which has solved all of my issues.

Canvas.GetTop not updated after Element drag

I have a Rectangle nested within a Canvas. The rectangle has a MouseDragElementBehavior attached to it, so it can move freely in the canvas.
I need to calculate the position of the rectangle after each drag. The problem is that the
Canvas.GetTop(rectangle1)
only works for the first time, i.e. before the rectangle is dragged. After the drag, the method call returns the initial position.
Those behaviors usually work by applying a RenderTransform, if it uses a TranslateTransform you can get the offset value from that and add it to the canvas position.
Alternatively you might just want to implement your own dragging logic.

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.

Silverlight Drag and Drop (without a Canvas)

I'm trying to drag and drop (slide) a Silverlight element from one part of a window to another.
I've implemented the MouseLeftButtonDown, MouseMove, and MouseLeftButtonUp event handlers on the element, but I've run into a bit of a problem.
All of the examples I've seen involve moving the element by setting the Canvas.Left and Canvas.Top properties. None of the elements I'm trying to manipulate live inside a Canvas. Is there a way to set the absolute position of the element being dragged, based on the coordinates of the mouse? Or is there a prepackaged solution to this problems somewhere that I've missed?
All panels but Canvas use some kind of constraint to position their children. Only Canvas lets you use absolute positioning. That's why I think it is the only way to implement drag and drop .
Feel free to use a Canvas on top of your existing panel. Just remember to remove the dragged element from its original parent and put it in the Canvas (or drag some kind of copy) and do the reverse on mouse up.
One way of achieving absolutely positioning an item inside any container, not only Canvas, is to use transformation instead of Left/Top properties. For instance, to set at Left=50 Top=80 you can modify the margin values through transformation.

Resources