MVVM Get Control Position - wpf

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.

Related

How to move elements within picturebox?

Is there any simple way to move picture parts (any stuff created with DrawImage or DrawString)?
My smart idea:
Override DrawImage(DrawString) method adding ability to store width, height, left, top, of drawn image (and may be boolean variable 'sizable' if I use DrawImage method) to a list of objects. Attach function "WhatToMove" to PictureBox's MouseMove event, which walks through a list of stored rectangle objects, detects what rectangle cursor is over, in case of resizeable rectangle detect if cursor is over any of rectangle's borders and pass this stuff as parameters to the MouseDown function, when I click. Then MouseDown function will attach new function "MoverResizer" to MouseMove event, which redraws picture box with moved or resized parts of image, updates proper rectangle object in list, and also detachs "WhatToMove" from MouseMove. On MouseUp and Leave events of PictureBox detach "MoverResizer" and attach "WhatToMove".
If you think this idea can live you will really help me if tell me how to override DrawImage.
My dumb idea:
Create Labels without text and with borders and locate them over PictureBox right over proper image parts. Then make them draggable and sizeable with their own events. Then translate positions of labels to positions of parts of picture and redraw picturebox. This is simply but I think not so perfect as method above.

WP7 Managing ScrollViewer within code

Within a WP7 app I am generating a Scrollviewer within the code. This Scrollviewer has a StackPanel as its content and within the Stackpanel I have a grid.
I have not assigned any specific heights to these controls.
As far as the look and the behaviour I am after, all is fine.
I now want to position the scrollviewer to a certain point - after I have generated it and its children.
It seems that the ScrollViewer.ScrollToVerticalOffset method is what I should be using.
But I cannot ascertain the height of the Scrollviewer... both Height and ActualHeight are returning 0. I want to use this height to determine which point I should be scrolling to.
And even when I hardcode a ScrollToVerticalOffet value then it still doesn't reposition itself.
Is there a way that I can get this to position as I want, and how can I determine the height values?
thanks
Got it working ok when I checked for values in the Page loaded event... and the ScrolltoOffset seemed to work fine there too.

GTK: Get pointer position on scroll-event AFTER scrolled_window has scrolled

I've got a drawing area inside a scrolled window (with convenience viewport),
and this drawing area updates itself according to incoming motion-notify-events.
When I scroll the area though obviously no motion events are emitted,
but I wanted to work around this, and so tried to connect the drawing area's
"scroll-event" signal to the same motion-notify callback.
The problem I'm facing here is that this signal is emitted before the scrolled window
has update its viewport, so in the callback, I end up with pointer coordinates that were true just before scrolling, making it look like the drawing area is always "lagging a step behind" the actual pointer when scrolling while not moving the pointer itself.
I thought I could compensate for this by manually extracting the coordinates with gdk_window_get_pointer, but then I realized this cannot work as the pointer is technically still at its old position when the callback is commencing.
I also tried using g_signal_connect_after in hopes it would have the callback get called after the viewport was scrolled, but it didn't help.
My last hope would be to start a timer on scroll-events, and have the callback fire after a minimal amount of time, but this sounds realllly ugly, and I'd like to avoid that at any cost.
Any ideas as to how this could be realized?
Thanks in advance!
A solution would be to connect to the "value-changed" signal of the GtkScrolledWindow
adjustments.
Source: https://mail.gnome.org/archives/gtk-app-devel-list/2011-September/msg00014.html

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: LineGeometry on a Canvas doesn't follow the cursor

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.

Resources