|
using System;
using System.Windows.Controls;
using System.Windows.Controls.Primitives;
namespace DragAndResizeControl
{
public class ResizeThumb : Thumb
{
private ResizerPosition position;
public ResizerPosition Position
{
get { return position; }
set { position = value; }
}
public ResizeThumb()
{
base.DragDelta += new DragDeltaEventHandler(ResizeThumb_DragDelta);
}
void ResizeThumb_DragDelta(object sender, DragDeltaEventArgs e)
{
Control item = this.DataContext as Control;
if (item != null)
{
switch (this.Position)
{
case ResizerPosition.Top:
Canvas.SetTop(item, Canvas.GetTop(item) + e.VerticalChange);
item.Height = Math.Max(25, item.ActualHeight - e.VerticalChange);
break;
case ResizerPosition.Bottom:
item.Height = Math.Max(25, item.ActualHeight + e.VerticalChange);
break;
case ResizerPosition.Left:
Canvas.SetLeft(item, Canvas.GetLeft(item) + e.HorizontalChange);
item.Width = Math.Max(25, item.ActualWidth - e.HorizontalChange);
break;
case ResizerPosition.Right:
item.Width = Math.Max(25, item.ActualWidth + e.HorizontalChange);
break;
case ResizerPosition.TopLeft:
Canvas.SetTop(item, Canvas.GetTop(item) + e.VerticalChange);
item.Height = Math.Max(25, item.ActualHeight - e.VerticalChange);
Canvas.SetLeft(item, Canvas.GetLeft(item) + e.HorizontalChange);
item.Width = Math.Max(25, item.ActualWidth - e.HorizontalChange);
break;
case ResizerPosition.TopRight:
Canvas.SetTop(item, Canvas.GetTop(item) + e.VerticalChange);
item.Height = Math.Max(25, item.ActualHeight - e.VerticalChange);
item.Width = Math.Max(25, item.ActualWidth + e.HorizontalChange);
break;
case ResizerPosition.BottomLeft:
item.Height = Math.Max(25, item.ActualHeight + e.VerticalChange);
Canvas.SetLeft(item, Canvas.GetLeft(item) + e.HorizontalChange);
item.Width = Math.Max(25, item.ActualWidth - e.HorizontalChange);
break;
case ResizerPosition.BottomRight:
item.Height = Math.Max(25, item.ActualHeight + e.VerticalChange);
item.Width = Math.Max(25, item.ActualWidth + e.HorizontalChange);
break;
default:
break;
}
}
e.Handled = true;
}
}
public enum ResizerPosition
{
None,
Top,
Bottom,
Left,
Right,
TopLeft,
TopRight,
BottomLeft,
BottomRight
}
}
|
By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.
If a file you wish to view isn't highlighted, and is a text file (not binary), please
let us know and we'll add colourisation support for it.
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.