2013-05-12 44 views
0

我已经在WPF中编写了一个定制的RichTextBox类。但我需要在此RichTextBox的左上角有一个小矩形,以便我可以在拖动RichTextBox时将其用作拖动控点。
于是我开始是这样的:如何定义由Rectangle类驱动的自定义Rectangle类?

public class DragHandleRegtangle : Shape 
    { 
     public double len = 5; 
     public double wid = 5; 

     public DragHandleRegtangle() 
     { 
      //what should be here exactly, anyway? 
     } 
    } 
//Here goes my custom RichTextBox 
public class CustomRichTextBox : RichTextBox 
... 

但我不知道我怎么可以指定宽/长/填充它的颜色,以及与RichTextBox(最重要的它的位置,这是完全零零相关的RichTextBox的定位点 - 即:它的左上角)

而且第一个错误到目前为止,我所得到的是:

“ResizableRichTextBox.DragHandleRegtangle”不实现 继承的抽象米烬 “System.Windows.Shapes.Shape.DefiningGeometry.get”

我会很感激,如果有人可以帮助我确定我的矩形,并解决此错误。

回答

2

写这代码

protected override System.Windows.Media.Geometry DefiningGeometry 
    { 
     //your code 
    } 
+0

非常感谢! 在你写的这段代码里面,我有这个: 'get {0} {0} {0}抛出new NotImplementedException(); }' 它解决了错误,但我仍然没有在画布上的矩形!我的意思是它没有渲染或什么。 我该如何设置此矩形的宽度/高度/颜色等? – Ali 2013-05-12 19:24:35

+0

我从来没有继承UI控件,因此我无法进一步帮助您。但是,当你需要一个Rectangle为什么不使用Rectangle类? – 2013-05-12 20:23:34

1

WPF框架有一个类,做你在找什么。 Thumb类表示允许用户拖动并调整控件大小的控件。通常在制作自定义控件时使用。 MSDN Docs for Thumb class

以下是如何实例化一个拇指并连接一些拖动处理程序。

private void SetupThumb() { 
    // the Thumb ...represents a control that lets the user drag and resize controls." 
    var t = new Thumb(); 
    t.Width = t.Height = 20; 
    t.DragStarted += new DragStartedEventHandler(ThumbDragStarted); 
    t.DragCompleted += new DragCompletedEventHandler(ThumbDragCompleted); 
    t.DragDelta += new DragDeltaEventHandler(t_DragDelta); 
    Canvas.SetLeft(t, 0); 
    Canvas.SetTop(t, 0); 
    mainCanvas.Children.Add(t); 
} 

private void ThumbDragStarted(object sender, DragStartedEventArgs e) 
{ 
    Thumb t = (Thumb)sender; 
    t.Cursor = Cursors.Hand; 
} 

private void ThumbDragCompleted(object sender,  DragCompletedEventArgs e) 
{ 
    Thumb t = (Thumb)sender; 
    t.Cursor = null; 
} 
void t_DragDelta(object sender, DragDeltaEventArgs e) 
{ 
    var item = sender as Thumb; 

    if (item != null) 
    { 
    double left = Canvas.GetLeft(item); 
    double top = Canvas.GetTop(item); 

    Canvas.SetLeft(item, left + e.HorizontalChange); 
    Canvas.SetTop(item, top + e.VerticalChange); 
    } 

} 
+0

非常感谢! 我要尽快测试;) – Ali 2013-05-12 20:24:55