DependencyProperty and WPF Designer

五迷三道 提交于 2019-12-10 16:39:52

问题


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;
        }
    }
}

Why when I update the "myLine" coordinates in WPF designer(VS 2010), it does not update it automatically(live)?

When using default "Line" WPF objects, they are automatically updated when the XAML code is changed(edited) in XAML/Design view.


回答1:


Since these properties affect rendering, you should specify it in the metadata:

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

I'm not sure it will be enough for the designer to take it into account, but it's worth a try...



来源:https://stackoverflow.com/questions/3770310/dependencyproperty-and-wpf-designer

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!