如何以编程方式调整 DrawingVisual 的大小?
How to programatically resize a DrawingVisual?
所以,我是 WPF 绘图的新手。出于性能原因,我不得不从常规控件(如 ContentControl 和 UserControl)切换到更轻量级的元素(如 DrawingVisual)。我正在开发一个图表应用程序,它可能在 canvas 上最多有 1000 个元素,可以拖动、调整大小等。首先,使用 DrawingVisual 而不是 Shape 更好吗?
其次,我的主要问题在这里。我将 DrawingVisual 元素添加到 Canvas 中:
public class SVisualContainer : UIElement
{
// Create a collection of child visual objects.
private VisualCollection _children;
public SVisualContainer()
{
_children = new VisualCollection(this);
_children.Add(CreateDrawingVisualRectangle());
}
// Create a DrawingVisual that contains a rectangle.
private DrawingVisual CreateDrawingVisualRectangle()
{
DrawingVisual drawingVisual = new DrawingVisual();
// Retrieve the DrawingContext in order to create new drawing content.
DrawingContext drawingContext = drawingVisual.RenderOpen();
// Create a rectangle and draw it in the DrawingContext.
Rect rect = new Rect(new System.Windows.Point(160, 100), new System.Windows.Size(320, 80));
drawingContext.DrawRectangle(System.Windows.Media.Brushes.LightBlue, null, rect);
// Persist the drawing content.
drawingContext.Close();
return drawingVisual;
}
// Provide a required override for the VisualChildrenCount property.
protected override int VisualChildrenCount
{
get { return _children.Count; }
}
// Provide a required override for the GetVisualChild method.
protected override Visual GetVisualChild(int index)
{
if (index < 0 || index >= _children.Count)
{
throw new ArgumentOutOfRangeException();
}
return _children[index];
}
}
并在 canvas 内:
public void AddStateVisual()
{
var sVisual = new SVisualContainer();
Children.Add(sVisual);
Canvas.SetLeft(sVisual, 10);
Canvas.SetTop(sVisual, 10);
}
如何通过代码动态增加矩形的大小?我试过设置 Rectangle 的 Height 和 Width,但没有用,我尝试使用 ScaleTransform,但这可能不是我想要的。我需要重新绘制矩形吗?谢谢!
如问题所示,我最终在 UIElement
中使用了 DrawingVisual
,并在调整大小时不断重绘 DrawingVisual
。 UIElement.RenderSize
属性、UIElement.MeasureCore
方法和 UIElement.InvalidateMeasure
方法是其中的核心。这很好用,性能可以接受。
所以,我是 WPF 绘图的新手。出于性能原因,我不得不从常规控件(如 ContentControl 和 UserControl)切换到更轻量级的元素(如 DrawingVisual)。我正在开发一个图表应用程序,它可能在 canvas 上最多有 1000 个元素,可以拖动、调整大小等。首先,使用 DrawingVisual 而不是 Shape 更好吗? 其次,我的主要问题在这里。我将 DrawingVisual 元素添加到 Canvas 中:
public class SVisualContainer : UIElement
{
// Create a collection of child visual objects.
private VisualCollection _children;
public SVisualContainer()
{
_children = new VisualCollection(this);
_children.Add(CreateDrawingVisualRectangle());
}
// Create a DrawingVisual that contains a rectangle.
private DrawingVisual CreateDrawingVisualRectangle()
{
DrawingVisual drawingVisual = new DrawingVisual();
// Retrieve the DrawingContext in order to create new drawing content.
DrawingContext drawingContext = drawingVisual.RenderOpen();
// Create a rectangle and draw it in the DrawingContext.
Rect rect = new Rect(new System.Windows.Point(160, 100), new System.Windows.Size(320, 80));
drawingContext.DrawRectangle(System.Windows.Media.Brushes.LightBlue, null, rect);
// Persist the drawing content.
drawingContext.Close();
return drawingVisual;
}
// Provide a required override for the VisualChildrenCount property.
protected override int VisualChildrenCount
{
get { return _children.Count; }
}
// Provide a required override for the GetVisualChild method.
protected override Visual GetVisualChild(int index)
{
if (index < 0 || index >= _children.Count)
{
throw new ArgumentOutOfRangeException();
}
return _children[index];
}
}
并在 canvas 内:
public void AddStateVisual()
{
var sVisual = new SVisualContainer();
Children.Add(sVisual);
Canvas.SetLeft(sVisual, 10);
Canvas.SetTop(sVisual, 10);
}
如何通过代码动态增加矩形的大小?我试过设置 Rectangle 的 Height 和 Width,但没有用,我尝试使用 ScaleTransform,但这可能不是我想要的。我需要重新绘制矩形吗?谢谢!
如问题所示,我最终在 UIElement
中使用了 DrawingVisual
,并在调整大小时不断重绘 DrawingVisual
。 UIElement.RenderSize
属性、UIElement.MeasureCore
方法和 UIElement.InvalidateMeasure
方法是其中的核心。这很好用,性能可以接受。