2013-05-12 43 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