Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/wpf/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Wpf 如何定义从矩形类驱动的自定义矩形类?_Wpf_Derived Class_Rectangles - Fatal编程技术网

Wpf 如何定义从矩形类驱动的自定义矩形类?

Wpf 如何定义从矩形类驱动的自定义矩形类?,wpf,derived-class,rectangles,Wpf,Derived Class,Rectangles,我已经在WPF中编写了一个定制的RichTextBox类。但是我需要在这个RichTextBox的左上角有一个小矩形,以便我可以在任何时候拖动RichTextBox时将其用作拖动手柄 所以我开始是这样的: public class DragHandleRegtangle : Shape { public double len = 5; public double wid = 5; public DragHandleRegtangle()

我已经在WPF中编写了一个定制的
RichTextBox
类。但是我需要在这个
RichTextBox
的左上角有一个小矩形,以便我可以在任何时候拖动
RichTextBox
时将其用作拖动手柄
所以我开始是这样的:

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的定位点完全零相关-即:它的左上角)

到目前为止,我遇到的第一个错误是:

“ResizeblerichTextBox.DragHandleRegtangle”未实现 继承的抽象成员 'System.Windows.Shapes.Shape.DefiningGeometry.get'


如果有人能帮我定义矩形并解决此错误,我将不胜感激。

将此内容写入您的代码中

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

将其写入代码

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

WPF框架有一个类,它完成了您所寻找的任务。
Thumb
类表示一个允许用户拖动和调整控件大小的控件。它通常用于制作自定义控件。

下面是如何实例化thumb并连接一些拖动处理程序

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);
  }

}

WPF框架有一个类,它完成了您所寻找的任务。
Thumb
类表示一个允许用户拖动和调整控件大小的控件。它通常用于制作自定义控件。

下面是如何实例化thumb并连接一些拖动处理程序

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);
  }

}

非常感谢你!在您编写的代码中,我有这样一段:
get{throw new NotImplementedException();}
它解决了错误,但我仍然没有画布中的矩形!我的意思是它没有被渲染或其他什么。我该如何设置这个矩形的宽度/高度/背景颜色等?我从来没有继承过UI控件,因此无法进一步帮助您。但是,当您需要一个矩形时,为什么不使用矩形类呢?非常感谢!在您编写的代码中,我有这样一段:
get{throw new NotImplementedException();}
它解决了错误,但我仍然没有画布中的矩形!我的意思是它没有被渲染或其他什么。我该如何设置这个矩形的宽度/高度/背景颜色等?我从来没有继承过UI控件,因此无法进一步帮助您。但是,当您需要一个矩形时,为什么不使用矩形类呢?非常感谢!我马上就要测试;)谢谢!我马上就要测试;)