Sancarn
Sancarn

Reputation: 2824

How to use Ribbon Buttons to execute code?

So far I have managed to create a ribbon with several buttons by following this tutorial.

Now I want to make the buttons actually do stuff! I found this example which shows how to execute already existing method. However if I do: Command="myCustomClass.myCustomMethod" and use

Class myCustomClass
    Public Sub myCustomMethod()
        'do stuff
    End Sub
End Class

I get the error myCustomClass is not supported in a Windows Presentation Foundation (WPF) project.

I also noticed this this post from the same post. After converting the code to VB.NET:

Public Class MyCommand
    Implements ICommand
    Public Sub Execute(parameter As Object) Implements ICommand.Execute
        Dim hello As String = TryCast(parameter, String)
        MsgBox(hello)
    End Sub

    Public Function CanExecute(parameter As Object) As Boolean Implements ICommand.CanExecute
        Return True
    End Function

    Public Event CanExecuteChanged As EventHandler Implements ICommand.CanExecuteChanged
End Class

and adding the references to the <Window x:Class="MainWindow" ... element:

<Window.Resources>
    <local:MyCommand x:Key="mycmd"/>
</Window.Resources>

I get the following error: The name 'MyCommand' does not exist in the namespace "clr-namespace:RibbonSample".

I'm at a complete loss as to what I need to do to implement these buttons. Would it be possible to get any full ribbon samples using the same framework, with which I can learn from? I assume the solution to this should be really simple.

Upvotes: 0

Views: 70

Answers (1)

SLaks
SLaks

Reputation: 887345

Rebuild your project.

The WPF designer loads classes from your project's compiled assembly (so that it can actually execute your code).

If you add a new class, it will give you errors until you rebuild your project so the assembly includes that class.

Upvotes: 1

Related Questions