我创造我自己
FrameworkElement
超驰
VisualChildrenCount{get;}
和
GetVisualChild(int index)
通过归还我自己的
DrawingVisual
实例。
如果在初始呈现后修改视觉内容(例如在计时器处理程序中),请使用
DrawingVisual.RenderOpen()
在上下文中,元素不会被刷新。
下面是最简单的示例:
using System;
using System.Windows;
using System.Windows.Media;
using System.Windows.Threading;
namespace VisualTest
{
public class TestControl : FrameworkElement
{
private readonly DrawingVisual _visual = new DrawingVisual();
public TestControl()
{
Draw(false);
var timer = new DispatcherTimer {Interval = new TimeSpan(0, 0, 2)};
timer.Tick += (sender, args) =>
{
Draw(true);
InvalidateVisual();
timer.Stop();
};
timer.Start();
}
protected override Visual GetVisualChild(int index)
{
return _visual;
}
protected override int VisualChildrenCount
{
get { return 1; }
}
private void Draw(bool second)
{
DrawingContext ctx = _visual.RenderOpen();
if (!second)
ctx.DrawRoundedRectangle(Brushes.Green, null, new Rect(0, 0, 200, 200), 20, 20);
else
ctx.DrawEllipse(Brushes.Red, null, new Point(100, 100), 100, 100);
ctx.Close();
}
}
}
InvalidateVisual()
什么也不做。但是,如果您调整包含元素的窗口的大小,它将得到更新。
关于如何正确刷新内容有什么想法吗?更可取地
没有
为我的元素引入新的依赖属性。