I've dynamically placed a user control on a Canvas as the VisualTree root in a Thumb control. I can move the thumb control correctly, but can only correctly re-size the control while resizing from the east, southeast, and south rectangles defined in the user control.
//Instantiate a new Thumb control to be placed on the canvas.
var myThumb = new Thumb();
myThumb.Name = "myThumb";
//Instantiate a new ThumbTemplate and ElementFactory with my user control.
var myThumbTemplate = new ControlTemplate(typeof(Thumb));
myThumbTemplate.VisualTree = new FrameworkElementFactory(typeof(MyUserControl), "myUserControl");
//Set the thumb template to the newly instantiated template.
myThumb.Template = myThumbTemplate;
//Point the DragDelta and DragCompleted events to local methods.
myThumb.DragDelta += new DragDeltaEventHandler(myThumb_DragDelta);
myThumb.DragCompleted += new DragCompletedEventHandler(myThumb_DragCompleted);
//Add the thumb to the canvas control.
this.myCanvas.Children.Add(myThumb);
Canvas.SetZIndex(myThumb, 1);
Canvas.SetTop(myThumb, 75);
Canvas.SetLeft(myThumb, 50);
In the DragDelta method, this approach works to resize the user control from the east resizing rectangle:
var myThumb = sender as Thumb;
if (myThumb != null)
{
var myUserControl = myThumb.Template.FindName("myUserControl", myThumb) as MyUserControl;
if (myUserControl != null)
{
myUserControl.Width = myUserControl.OldWidth + e.HorizontalChange;
}
}
But wherever I attempt to do that plus set the Canvas.Top of the Thumb control (Ie. from the North re-size rectangle), I don't get the results I expected:
double top = Canvas.GetTop(finderThumb) + e.VerticalChange;
myUserControl.Height = myUserControl.OldHeight - e.VerticalChange;
Canvas.SetTop(myThumb, top);
What happens then is that the control moves up and down in the canvas control but only ever re-sizes by one pixel, regardless of the Vertical or Horizontal Change. Any ideas as to why this happens and suggestions for resolving the issue? Thanks!