开发者

WPF: How to make a base class' dependency property read-only_

开发者 https://www.devze.com 2023-01-08 19:41 出处:网络
I have a class public class NavigableViewport3D : Viewport3D. The class should开发者_Go百科 hide the the Viewport3D.Camera property, so that it becomes read-only. Here\'s what I have so far:

I have a class public class NavigableViewport3D : Viewport3D. The class should开发者_Go百科 hide the the Viewport3D.Camera property, so that it becomes read-only. Here's what I have so far:


public class NavigableViewport3D : Viewport3D
{
    protected static readonly DependencyPropertyKey CameraPropertyKey = DependencyProperty.RegisterReadOnly(
        "Camera",
        typeof(Camera),
        typeof(NavigableViewport3D),
        new PropertyMetadata()
        );
    public static readonly new DependencyProperty CameraProperty = CameraPropertyKey.DependencyProperty;

    public new Camera Camera
    {
        get
        {
            return base.Camera;
        }
        protected set
        {
            base.Camera = value;
        }
    }
}

But I would also like the NavigableViewport3D.CameraProperty to alway return the same value as Viewport3D.CameraProperty/base.Camera... If there were no dependency properties I would do it like this:

public class NavigableViewport3D : Viewport3D
{
    public new Camera Camera
    {
        get
        {
            return base.Camera;
        }
        protected set
        {
            base.Camera = value;
        }
    }

}

Here is an ugly way that should accomplish a two-way exchange:


public class NavigableViewport3D : Viewport3D
{
    protected static readonly DependencyPropertyKey CameraPropertyKey = DependencyProperty.RegisterReadOnly(
        "Camera",
        typeof(Camera),
        typeof(NavigableViewport3D),
        new PropertyMetadata(new PropertyChangedCallback(
            delegate(DependencyObject depO, DependencyPropertyChangedEventArgs e)
            {
                if (e.OldValue != e.NewValue) //Not sure whether this is needed
                    depO.SetValue(Viewport3D.CameraProperty, e.NewValue);
            }
        )));
    public static readonly new DependencyProperty CameraProperty = CameraPropertyKey.DependencyProperty;

    public new Camera Camera
    {
        get
        {
            return this.Camera;
        }
        protected set
        {
            this.Camera = value;
        }
    }        

    public NavigableViewport3D()
    {
        Viewport3D.CameraProperty.AddOwner(
            typeof(NavigableViewport3D), 
            new PropertyMetadata(new PropertyChangedCallback(
                delegate(DependencyObject depO, DependencyPropertyChangedEventArgs e)
                {
                    var nv3d = depO as NavigableViewport3D;
                    if (nv3d == null)
                        return;
                    nv3d.SetValue(CameraPropertyKey, e.NewValue);
                }
        )));
    }
}

I seriously hope that there is a better way to accomplish this... Any suggestions?


How about this:

namespace WpfApplication1
{
    /// <summary>
    /// Interaction logic for Window1.xaml
    /// </summary>
    public partial class Window1 : Window
    {
        public Window1()
        {
            InitializeComponent();
        }
    }

    public class NavigableViewport3D : Viewport3D
    {
        public static readonly new DependencyProperty CameraProperty;

        static NavigableViewport3D()
        {
            NavigableViewport3D.CameraProperty = Viewport3D.CameraProperty.AddOwner( typeof( NavigableViewport3D ) );
        }
    }
}
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号