1

我有一堂课 public class NavigableViewport3D : Viewport3D。该类应隐藏该Viewport3D.Camera属性,使其变为只读。这是我到目前为止所拥有的:


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

但我也希望始终返回与/NavigableViewport3D.CameraProperty相同的值...如果没有依赖属性,我会这样做:Viewport3D.CameraPropertybase.Camera

public class NavigableViewport3D : Viewport3D
{
    public new Camera Camera
    {
        get
        {
            return base.Camera;
        }
        protected set
        {
            base.Camera = value;
        }
    }
}
这是一种应该完成双向交换的丑陋方式:


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

我真的希望有更好的方法来实现这一点......有什么建议吗?

4

1 回答 1

0

这个怎么样:

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 ) );
        }
    }
}
于 2010-07-23T21:06:21.197 回答