Search code examples
wpfsilverlightbuttonicommanddelegatecommand

Silverlight button on mainpage, linked to command, never calling the method


I'm brand new to Silverlight and I'm in the deep end a little over my head so I'm probably missing something really obvious. I'm working on an image editor and I have a button on my main page that is supposed to rotate images or text on my canvas. The button however isn't calling my rotate method. EDIT: It is now.

Here's all the code I've written related to the button

MainPage.xaml

<Button Command="{Binding Path=Project.RotateCWElementCommand}"..../>

Project.cs -

#region properties

public ICommand RotateCWElementCommand { get; set; }

#endregion

#region methods
public Project(int siteID)
    {
        this.RotateCWElementCommand = new DelegateCommand(RotateCWElement, CanRotateCWElement);
    }

    private void RotateCWElement(object param)
    {
        FrameworkElement element = this.SelectedElement;
        RotateTransform cwRot = new RotateTransform();

        cwRot.Angle = 90;
        cwRot.CenterX = element.ActualWidth * 0.5;
        cwRot.CenterY = element.ActualHeight * 0.5;
        element.RenderTransform = cwRot;

    }

#end region

#region Command conditions

private bool CanRotateCWElement(object param)
    {
        return true;
    }

#endregion

The problem now is that it will only rotate once and some image quality also appears to be lost. The images move strangely when I click and drag them, and sometimes when I click the full image quality returns.

If anybody has any ideas about this it'd be great.


Solution

  • It sounds like the Button.DataContext does not contain a property called Project.RotateCWElementCommand

    Verify that your Button's DataContext has a property called Project, and that Project has a property called RotateCWElementCommand