Search code examples
.netwpfribboncommandbinding

How to force a RibbonButton to be alway enabled?


I have a Microsoft Ribbonbar inside a Window, and every RibbonButton is associated to a specific command like this:

<ribbon:Ribbon x:Key="MyToolbar"  Name="MyToolbar"  >         
<ribbon:RibbonTab Header="File" >
<ribbon:RibbonGroup Header="File"  >
<ribbon:RibbonButton Command="{x:Static ApplicationCommands.New}"  LargeImageSource="Images/GenericDocument.png" Label="Nuovo" />
<ribbon:RibbonButton Command="{x:Static ApplicationCommands.Open}" SmallImageSource="Images/OpenFolder.png" Label="Apri"  />
</ribbon:RibbonGroup>
</ribbon:RibbonTab>
</ribbon:Ribbon>

I'm handling the commands in this way into the code-behind of a specific Canvas object placed in the same window:

private void BindApplicationCommands()
{
// FILE group
this.CommandBindings.Add(new CommandBinding(ApplicationCommands.New, New_Executed));
this.CommandBindings.Add(new CommandBinding(ApplicationCommands.Open, Open_Executed,Open_Enabled));
}

When I click outside the Canvas component where the handling code is placed, all the buttons inside the Ribbon become to be disabled. This is an undesired behaviour, since i want that some specific buttons (like the "New" and "Open" commands) are always and globally enabled, despite to where i click. How can I achieve this ? Thanks in advance for any suggestion.


Solution

  • Ok, I solved my own problem. Since the CommandBindings where defined inside the Canvas component, they weren't manageable into a wider (global) context, so i moved them into the Window code-behind to delegate their execution/activation in a more flexible way. That's all :)