如何在WPF中正确刷新自定义形状?
我创建了一条自定义行,旁边有一些文本。该形状是 System.Windows.Shapes.Shape 的子类。由于某种原因,当我更改线的坐标时,文本不会刷新。我了解 InvalidateVisual()
方法,但是每次我移动元素时,我都必须调用它来重绘形状。我确信有更好的方法来做到这一点。我做错了什么? ATM 我没有主意了。
public class MyShape : Shape
{
LineGeometry line;
FormattedText text;
public MyShape()
{
line = new LineGeometry();
text = new FormattedText(
Edge.Length.ToString(),
Thread.CurrentThread.CurrentCulture,
System.Windows.FlowDirection.LeftToRight,
new Typeface("Verdana"), 10, Brushes.Black);
}
// Specify the X1 dependency property:
public static readonly DependencyProperty X1Property =
DependencyProperty.Register("X1",
typeof(double), typeof(MyShape),
new FrameworkPropertyMetadata(0.0,
FrameworkPropertyMetadataOptions.AffectsMeasure));
public double X1
{
set { SetValue(X1Property, value); }
get { return (double)GetValue(X1Property); }
}
// Specify the Y1 dependency property:
public static readonly DependencyProperty Y1Property =
DependencyProperty.Register("Y1",
typeof(double), typeof(MyShape),
new FrameworkPropertyMetadata(0.0,
FrameworkPropertyMetadataOptions.AffectsMeasure));
public double Y1
{
set { SetValue(Y1Property, value); }
get { return (double)GetValue(Y1Property); }
}
/*Some other Dependency Properties.... and*/
protected override Geometry DefiningGeometry
{
get
{
GeometryGroup geometryGroup = new GeometryGroup();
line.StartPoint = new Point(X1, Y1);
line.EndPoint = new Point(X2, Y2);
text.SetFontWeight(FontWeights.ExtraLight);
Geometry geometry = text.BuildGeometry(new Point((X1 + X2) / 2, (Y1 + Y2) / 2));
geometryGroup.Children.Add(geometry);
geometryGroup.Children.Add(line);
return geometryGroup;
}
}
}
I created a custom line with some text next to it. The shape is a subclass of System.Windows.Shapes.Shape
. For some reason the text does not refresh when I change the coordinates for the line. I know about the InvalidateVisual()
approach, but then every time I move elements around I would have to call it to redraw the shape. I am sure there is a better way of doing it. What am I doing wrong? ATM I am out of ideas.
public class MyShape : Shape
{
LineGeometry line;
FormattedText text;
public MyShape()
{
line = new LineGeometry();
text = new FormattedText(
Edge.Length.ToString(),
Thread.CurrentThread.CurrentCulture,
System.Windows.FlowDirection.LeftToRight,
new Typeface("Verdana"), 10, Brushes.Black);
}
// Specify the X1 dependency property:
public static readonly DependencyProperty X1Property =
DependencyProperty.Register("X1",
typeof(double), typeof(MyShape),
new FrameworkPropertyMetadata(0.0,
FrameworkPropertyMetadataOptions.AffectsMeasure));
public double X1
{
set { SetValue(X1Property, value); }
get { return (double)GetValue(X1Property); }
}
// Specify the Y1 dependency property:
public static readonly DependencyProperty Y1Property =
DependencyProperty.Register("Y1",
typeof(double), typeof(MyShape),
new FrameworkPropertyMetadata(0.0,
FrameworkPropertyMetadataOptions.AffectsMeasure));
public double Y1
{
set { SetValue(Y1Property, value); }
get { return (double)GetValue(Y1Property); }
}
/*Some other Dependency Properties.... and*/
protected override Geometry DefiningGeometry
{
get
{
GeometryGroup geometryGroup = new GeometryGroup();
line.StartPoint = new Point(X1, Y1);
line.EndPoint = new Point(X2, Y2);
text.SetFontWeight(FontWeights.ExtraLight);
Geometry geometry = text.BuildGeometry(new Point((X1 + X2) / 2, (Y1 + Y2) / 2));
geometryGroup.Children.Add(geometry);
geometryGroup.Children.Add(line);
return geometryGroup;
}
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
尝试使用 FrameworkPropertyMetadataOptions.AffectsRender
Try to use FrameworkPropertyMetadataOptions.AffectsRender