我有一堂课
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.CameraProperty
base.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);
}
)));
}
}
我真的希望有更好的方法来实现这一点......有什么建议吗?