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:
Here is an ugly way that should accomplish a two-way exchange:
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);
}
)));
}
}
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 ) );
}
}
}
精彩评论