WPF: Problem with taking command handlers out of code behind

I have some command binding defined in my XAML:

<UserControl.CommandBindings>
    <CommandBinding 
        Command="commands:Commands.GrantAccessCommand"
        Executed="HelpExecuted" />
</UserControl.CommandBindings>

So GrantAccessCommand is located in a seperate class called Commands and HelpExecuted is in my code behind. It works fine. Now when I put HelpExecuted in the other class as well I get an error "...HelpExecuted' is not a valid event handler method name. Only instance methods on the generated or code-behind class are valid." The code:

Executed="commands:Commands.HelpExecuted"
//I also tryed: Executed="x:Static commands:Commands.HelpExecuted"

I know I can do this in code behind using CommandBindings.Add(new CommandBinding(GrantAccessCommand, HelpExecuted)); But I want to do this using XAML

Is there a way?

Answers


My guess is .Net 3.5. From http://www.codeproject.com/KB/WPF/CentralizingWPFCommands.aspx: The current WPF version's XAML does not allow us to bind event handlers in this way. The event handlers must be defined in the code-behind file inside the MainWindow class. I don't know if this is a bug, an accidentally left out feature, or if we are not even supposed to use this functionality, but this stops us from defining a centralized location for handling all commands' Executed and CanExecute events.

The workaround is to define bindings in code:

window.CommandBindings.Add(new CommandBinding(Help, HelpExecuted, HelpCanExecute));


Need Your Help

SQL - for each (if… then… else…)

sql for-loop if-statement

I'm trying to test a sql database that I created. To do so, I've got 2 tables: items and brands. Items store different fields including the name of a brand, and brands store other fields including a

What is the purpose of a value converter in Silverlight?

asp.net wpf silverlight silverlight-4.0

I'm just starting to get my hands dirty with silverlight and "value converter" keeps popping up. I don't remember reading about them in the ASP.NET web app world. Is this something special in

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.