0

Hello I'm learning WPF but I always seem to get an error when I try this:

<Window x:Class="Projectosaurus.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="MainWindow" Height="350" Width="525"
    xmlns:local="clr-namespace:Projectosaurus"> //// this is where I mapped my namespace
<Grid>
    <Canvas>
        <Ellipse Fill="DarkGreen" Width="100" Height="75" local:Rotation.Angle="60"/> 

//and when I try to get it here I always get an error that the name (in this case) "Rotation" does not exist in the namespace (in this case) Projectosaurus, yet it does I've created a class "Rotation" inside of this project and its Namespace is Projectosaurus, what am I doing wrong? (I get this every time in every project)

namespace Projectosaurus
{
class Rotation : DependencyObject
{


    public static double GetAngle(DependencyObject obj)
    {
        return (double)obj.GetValue(AngleProperty);
    }

    public static void SetAngle(DependencyObject obj, double value)
    {
        obj.SetValue(AngleProperty, value);
    }

    // Using a DependencyProperty as the backing store for Angle.  This enables animation, styling, binding, etc...
    public static readonly DependencyProperty AngleProperty =
        DependencyProperty.RegisterAttached("Angle", typeof(double), typeof(Rotation), new PropertyMetadata(0.0, OnAngleChanged));

    private static void OnAngleChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
    {
        var element = d as UIElement;
        if (element != null)
        {
            element.RenderTransformOrigin = new Point(.5, .5);
            element.RenderTransform = new RotateTransform(
            (double)e.NewValue);
        }
    }

**FIXED IT! First time I had this problem yesterday, my immediate thought was to make the class "public" but it did not work, but that was because I did not rebuild it! Thanks everyone.

4
  • You need to also show us the Projectosaurus class if you want any help. May 16, 2014 at 19:28
  • Is Rotation in the same assembly as your MainWindow? Also, do you get this error when compiling, or does it just show up in the "Errors" pane while you are editing? If the latter, try recompiling the project. May 16, 2014 at 19:31
  • I get an immediate error when I try to write "local:Rotation..." inside Ellipse Tags.
    – strikan
    May 16, 2014 at 19:33
  • Any time you “get an error” remember to show us the full error message, including stack trace. Do not just pick out a few words from the error message. May 16, 2014 at 19:39

1 Answer 1

1

You need to make Rotation class public. Also make it static and you don't need to inherit from DependencyProperty you are using it as an AttachProperty.

I don't see where you initialize <local:Rotation/>

public static class Rotation 
{


public static double GetAngle(DependencyObject obj)
{
    return (double)obj.GetValue(AngleProperty);
}

public static void SetAngle(DependencyObject obj, double value)
{
    obj.SetValue(AngleProperty, value);
}

// Using a DependencyProperty as the backing store for Angle.  This enables animation, styling, binding, etc...
public static readonly DependencyProperty AngleProperty =
    DependencyProperty.RegisterAttached("Angle", typeof(double), typeof(Rotation), new PropertyMetadata(0.0, OnAngleChanged));

private static void OnAngleChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
    var element = d as UIElement;
    if (element != null)
    {
        element.RenderTransformOrigin = new Point(.5, .5);
        element.RenderTransform = new RotateTransform(
        (double)e.NewValue);
    }
}
4
  • Tried that previous time, it didn't work as a matter of fact as soon as I write "local" inside Ellipse tags I get the error
    – strikan
    May 16, 2014 at 19:35
  • As soon as you write "local" the reason why you get the error because it hasn't build yet. Try rebuilding and the error should go away. Just try making it public and do a rebuild. May 16, 2014 at 19:38
  • yeah I realized that was stupid of me to say, I'll try right now
    – strikan
    May 16, 2014 at 19:39
  • yup it works! my problem was also, that I've tried yesterday with "public" but it didn't work because I did not rebuild it, so I thought the problem was somewhere else, thanks a lot!
    – strikan
    May 16, 2014 at 19:43

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.