依赖属性和WPF设计器

4
class MyLine : Shape {
    public static readonly DependencyProperty X11Property;
    public static readonly DependencyProperty X22Property;
    public static readonly DependencyProperty Y11Property;
    public static readonly DependencyProperty Y22Property;

    static MyLine() {
        X11Property = DependencyProperty.Register("X11", typeof(double), typeof(MyLine), new UIPropertyMetadata(double.NaN));
        X22Property = DependencyProperty.Register("X22", typeof(double), typeof(MyLine), new UIPropertyMetadata(double.NaN));
        Y11Property = DependencyProperty.Register("Y11", typeof(double), typeof(MyLine), new UIPropertyMetadata(double.NaN));
        Y22Property = DependencyProperty.Register("Y22", typeof(double), typeof(MyLine), new UIPropertyMetadata(double.NaN));
    }

    [TypeConverter(typeof(LengthConverter))]
    public double X11 { get { return (double)GetValue(X11Property); } set { SetValue(X11Property, value); } }
    [TypeConverter(typeof(LengthConverter))]
    public double X22 { get { return (double)GetValue(X22Property); } set { SetValue(X22Property, value); } }
    [TypeConverter(typeof(LengthConverter))]
    public double Y11 { get { return (double)GetValue(Y11Property); } set { SetValue(Y11Property, value); } }
    [TypeConverter(typeof(LengthConverter))]
    public double Y22 { get { return (double)GetValue(Y22Property); } set { SetValue(Y22Property, value); } }

    protected override System.Windows.Media.Geometry DefiningGeometry {
        get {
            var geometryGroup = new GeometryGroup();

            // Add line
            geometryGroup.Children.Add(new LineGeometry(new Point(X11, Y11), new Point(X22, Y22)));
            return geometryGroup;
        }
    }
}

为什么我在WPF设计器(VS 2010)中更新“myLine”坐标时,它不会自动更新(实时更新)?
当使用默认的WPF“Line”对象时,在XAML / Design视图中更改(编辑)XAML代码时,它们会自动更新。
1个回答

6

由于这些属性会影响渲染,您应该在元数据中指定:

X11Property = DependencyProperty.Register("X11", typeof(double), typeof(DirectionLine), new FrameworkPropertyMetadata(double.NaN, FrameworkPropertyMetadataOptions.AffectsRender));

我不确定这是否足以让设计师考虑,但值得一试...


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接