这是装饰器BUG吗?
我在使用装饰器时遇到了一个奇怪的问题。通过使用继承自装饰器的类,我尝试在 ScrollViewer 中动态绘制一条线。当线短于特定长度(大约 600)时,它效果很好,但是如果我尝试滚动ScrollViewer并绘制更长的线,问题来了。线似乎被剪断了,其余超过600的部分似乎看不见了。我检查了线路的X1,X2,Y1,Y2,似乎是正确的。您能给一些建议吗?非常感谢!
这是我的装饰器类
internal class LinkGanttTaskAdorner : Adorner
{
private readonly Line child = null;
private double x2 = 0;
private double y2 = 0;
private double x1 = 0;
private double y1 = 0;
public LinkGanttTaskAdorner(UIElement adornedElement)
: base(adornedElement)
{
var brush = new VisualBrush(adornedElement);
child = new Line
{
Stroke = Brushes.Black,
StrokeThickness = 1
};
}
protected override Size MeasureOverride(Size constraint)
{
child.Measure(constraint);
return child.DesiredSize;
}
protected override Size ArrangeOverride(Size finalSize)
{
child.Arrange(new Rect(finalSize));
return finalSize;
}
protected override Visual GetVisualChild(int index)
{
return child;
}
protected override int VisualChildrenCount
{
get
{
return 1;
}
}
public double X1
{
get
{
return x1;
}
set
{
x1 = value;
child.X1 = value;
}
}
public double Y1
{
get
{
return y1;
}
set
{
y1 = value;
child.Y1 = value;
}
}
public double X2
{
get
{
return x2;
}
set
{
x2 = value;
child.X2 = value;
UpdatePosition();
}
}
public double Y2
{
get
{
return y2;
}
set
{
y2 = value;
child.Y2 = value;
UpdatePosition();
}
}
private void UpdatePosition()
{
var adornerLayer = this.Parent as AdornerLayer;
if (adornerLayer != null)
{
adornerLayer.Update(AdornedElement);
}
}
通过使用 MouseMove 事件来绘制线条
private void DragLinkStarted()
{
isMoveTask = false;
isLinkTask = true;
linkAdorner = new LinkGanttTaskAdorner(originalElement) { X1 = startMousePoint.X, Y1 = startMousePoint.Y };
var layer = AdornerLayer.GetAdornerLayer(originalElement);
layer.Add(linkAdorner);
Cursor = Cursors.Cross;
}
private void DragLinkMoved()
{
mousePoint = Mouse.GetPosition(originalElement);
linkAdorner.X2 = mousePoint.X;
linkAdorner.Y2 = mousePoint.Y;
}
I've encountered a strange problem working with adorners.By using a class inheriting from Adorner,I try to draw a line in a ScrollViewer dynamically.It works well when the line is shorter than a particular length(about 600),but if I tried to scroll the ScrollViewer and draw the line longer,the problem came.The line appeared to be clipped and the rest part which is longer than 600 seemed to be invisible. I had checked the line's X1,X2,Y1,Y2,it seemed to be correct.Could you please give some advices?Thanks a lot!
Here is my Adorner Class
internal class LinkGanttTaskAdorner : Adorner
{
private readonly Line child = null;
private double x2 = 0;
private double y2 = 0;
private double x1 = 0;
private double y1 = 0;
public LinkGanttTaskAdorner(UIElement adornedElement)
: base(adornedElement)
{
var brush = new VisualBrush(adornedElement);
child = new Line
{
Stroke = Brushes.Black,
StrokeThickness = 1
};
}
protected override Size MeasureOverride(Size constraint)
{
child.Measure(constraint);
return child.DesiredSize;
}
protected override Size ArrangeOverride(Size finalSize)
{
child.Arrange(new Rect(finalSize));
return finalSize;
}
protected override Visual GetVisualChild(int index)
{
return child;
}
protected override int VisualChildrenCount
{
get
{
return 1;
}
}
public double X1
{
get
{
return x1;
}
set
{
x1 = value;
child.X1 = value;
}
}
public double Y1
{
get
{
return y1;
}
set
{
y1 = value;
child.Y1 = value;
}
}
public double X2
{
get
{
return x2;
}
set
{
x2 = value;
child.X2 = value;
UpdatePosition();
}
}
public double Y2
{
get
{
return y2;
}
set
{
y2 = value;
child.Y2 = value;
UpdatePosition();
}
}
private void UpdatePosition()
{
var adornerLayer = this.Parent as AdornerLayer;
if (adornerLayer != null)
{
adornerLayer.Update(AdornedElement);
}
}
By using the MouseMove event to Draw the line
private void DragLinkStarted()
{
isMoveTask = false;
isLinkTask = true;
linkAdorner = new LinkGanttTaskAdorner(originalElement) { X1 = startMousePoint.X, Y1 = startMousePoint.Y };
var layer = AdornerLayer.GetAdornerLayer(originalElement);
layer.Add(linkAdorner);
Cursor = Cursors.Cross;
}
private void DragLinkMoved()
{
mousePoint = Mouse.GetPosition(originalElement);
linkAdorner.X2 = mousePoint.X;
linkAdorner.Y2 = mousePoint.Y;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论