我创建了一个自定义行,旁边有一些文字。形状是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;
}
}
}