我已动态将Canvas
上的用户控件作为VisualTree
控件中的Thumb
根。我可以正确地移动拇指控件,但只能在从用户控件中定义的东,东南和南方矩形调整大小时正确地重新调整控件的大小。
//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);
在DragDelta
方法中,此方法可以从东部调整大小的矩形调整用户控件的大小:
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;
}
}
但无论我在哪里尝试这样做,另外设置Canvas.Top
控件的Thumb
(即来自北方重新调整大小的矩形),我得不到我预期的结果:
double top = Canvas.GetTop(finderThumb) + e.VerticalChange;
myUserControl.Height = myUserControl.OldHeight - e.VerticalChange;
Canvas.SetTop(myThumb, top);
然后发生的是控件在画布控件中上下移动,但只能按一个像素重新调整大小,无论垂直或水平变化如何。关于为什么会发生这种情况的任何想法以及解决问题的建议?谢谢!
答案 0 :(得分:1)
尝试使用此示例中解释的此类(ResizeThumb) WPF Diagram Designer: Part 1
public class ResizeThumb : Thumb
{
public ResizeThumb()
{
DragDelta += new DragDeltaEventHandler(this.ResizeThumb_DragDelta);
}
private void ResizeThumb_DragDelta(object sender, DragDeltaEventArgs e)
{
Control item = this.DataContext as Control;
if (item != null)
{
double deltaVertical, deltaHorizontal;
switch (VerticalAlignment)
{
case VerticalAlignment.Bottom:
deltaVertical = Math.Min(-e.VerticalChange,
item.ActualHeight - item.MinHeight);
item.Height -= deltaVertical;
break;
case VerticalAlignment.Top:
deltaVertical = Math.Min(e.VerticalChange,
item.ActualHeight - item.MinHeight);
Canvas.SetTop(item, Canvas.GetTop(item) + deltaVertical);
item.Height -= deltaVertical;
break;
default:
break;
}
switch (HorizontalAlignment)
{
case HorizontalAlignment.Left:
deltaHorizontal = Math.Min(e.HorizontalChange,
item.ActualWidth - item.MinWidth);
Canvas.SetLeft(item, Canvas.GetLeft(item) + deltaHorizontal);
item.Width -= deltaHorizontal;
break;
case HorizontalAlignment.Right:
deltaHorizontal = Math.Min(-e.HorizontalChange,
item.ActualWidth - item.MinWidth);
item.Width -= deltaHorizontal;
break;
default:
break;
}
}
e.Handled = true;
}
}
答案 1 :(得分:0)
从MSDN抓取的这个示例显示它添加e.VerticalChange而不是减去。也许这就是问题?
void onDragDelta(object sender, DragDeltaEventArgs e)
{
//Move the Thumb to the mouse position during the drag operation
double yadjust = myCanvasStretch.Height + e.VerticalChange;
double xadjust = myCanvasStretch.Width + e.HorizontalChange;
if ((xadjust >= 0) && (yadjust >= 0))
{
myCanvasStretch.Width = xadjust;
myCanvasStretch.Height = yadjust;
Canvas.SetLeft(myThumb, Canvas.GetLeft(myThumb) +
e.HorizontalChange);
Canvas.SetTop(myThumb, Canvas.GetTop(myThumb) +
e.VerticalChange);
changes.Text = "Size: " +
myCanvasStretch.Width.ToString() +
", " +
myCanvasStretch.Height.ToString();
}
}