ceth
ceth

Reputation: 45345

Creating an attached property

I am want to study how to create a simple attached property. For example, to rotate figure on the canvas. Something like this, but using attached property:

    <Canvas Margin="0 200">
        <Ellipse Fill="Red" Width="100" Height="60"
                 RenderTransformOrigin=".5, .5">
            <Ellipse.RenderTransform>
                <RotateTransform Angle="30"/>
            </Ellipse.RenderTransform>
        </Ellipse>
    </Canvas>

I have created class:

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

And modified my XAML:

  <Canvas Margin="0 200">
        <Ellipse Fill="Red" Width="100" Height="60" 
                 local:RotationManager.Angle="45"/>
 </Canvas>

But if I will add local:RotationManager.Angle="45" the ellipse disappear from canvas. Why ? I set the breakpoints to my RotationManager, but no one of the method executes.

How can I fix it?

Upvotes: 0

Views: 34

Answers (1)

Clemens
Clemens

Reputation: 128181

Apparently, in Windows Runtime the class needs to be public:

public class RotationManager
{
    ...
}

Upvotes: 1

Related Questions