我想学习如何创建一个简单的附加属性。例如,要在画布上旋转图形。像这样的东西,但使用附加属性:
<Canvas Margin="0 200">
<Ellipse Fill="Red" Width="100" Height="60"
RenderTransformOrigin=".5, .5">
<Ellipse.RenderTransform>
<RotateTransform Angle="30"/>
</Ellipse.RenderTransform>
</Ellipse>
</Canvas>
我创建了类:
class RotationManager : DependencyObject
{
public static double GetAngle(DependencyObject obj)
{
return (double) obj.GetValue(AngleProperty);
}
public static void SetAngle(DependencyObject obj, double value)
{
obj.SetValue(AngleProperty, value);
}
private static void OnAngleChanged(DependencyObject obj, DependencyPropertyChangedEventArgs e)
{
var element = obj as UIElement;
if (element != null)
{
element.RenderTransformOrigin= new Point(.5, .5);
var transform = new RotateTransform();
transform.Angle = (double) e.NewValue;
element.RenderTransform = transform;
}
}
public static readonly DependencyProperty AngleProperty =
DependencyProperty.RegisterAttached("Angle",
typeof (double), typeof (RotationManager),
new PropertyMetadata(0.0, OnAngleChanged));
}
并修改了我的XAML:
<Canvas Margin="0 200">
<Ellipse Fill="Red" Width="100" Height="60"
local:RotationManager.Angle="45"/>
</Canvas>
但如果我要添加local:RotationManager.Angle="45"
,椭圆会从画布中消失。为什么?
我将断点设置为我的RotationManager,但没有一个方法执行。
我该如何解决?
答案 0 :(得分:1)
显然,在Windows运行时,该类需要公开:
public class RotationManager
{
...
}