Using ApplicationCommands

Dec 5, 2008 at 10:21 PM
I have created a ribbon with the standard clipboard buttons Cut/Copy & Paste.

There is also a TextBox control. The TextBox control knows about ApplicationCommands.

When the TextBox control has focus, I would like the buttons on the ribbon to work.

Is it possible to route ApplicationCommands instead of the RibbonCommands?
Coordinator
Dec 5, 2008 at 11:34 PM
The ribbon should work fully when the TextBox has Focus.  I just tested it out and I got a RibbonCommand to properly route when I clicked on a button and I had focus in a TextBox.  What commands are you seeing that are not functioning properly?

Currently due to implementation the Ribbon and its controls us RibbonCommands.
Dec 8, 2008 at 11:35 AM
Thanks for the reply.

An example of what we are trying to do:-

<r:RibbonWindow x:Class="WpfApplicationRouting.Window1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:my="clr-namespace:WpfApplicationRouting"
    xmlns:r="clr-namespace:Microsoft.Windows.Controls.Ribbon;assembly=RibbonControlsLibrary"
    Title="Window1" Height="381" Width="627"  Background="White">
 <StackPanel FocusManager.FocusedElement="{Binding ElementName=text}">
  <r:Ribbon>
   <r:Ribbon.Resources>
    <r:RibbonCommand x:Key="AppCommands.Cut" x:Name="commandCut"
                           LabelTitle="Cut"
                           ToolTipTitle="Cut"
                           ToolTipDescription="Cut selected object to the Clipboard." CanExecute="commandCut_CanExecute" Executed="commandCut_Executed"  />
   </r:Ribbon.Resources>
   <r:RibbonTab Label="Home">
    <r:RibbonGroup x:Name="clipboard">
     <r:RibbonButton Command="{StaticResource AppCommands.Cut}"/>
    </r:RibbonGroup>
   </r:RibbonTab>
  </r:Ribbon>
  <TextBox x:Name="text"></TextBox>
 </StackPanel>
</r:RibbonWindow>

----------------------------------------------------------------------------------------------------------------------------------------------------------------------

namespace WpfApplicationRouting
{
    /// <summary>
    /// Interaction logic for Window1.xaml
    /// </summary>
    public partial class Window1 : RibbonWindow
    {
        public Window1()
        {
            InitializeComponent();
        }

        private void commandCut_CanExecute(object sender, CanExecuteRoutedEventArgs e)
        {
            ICommand command = ApplicationCommands.Cut;

            e.CanExecute = command.CanExecute(null);
        }

        private void commandCut_Executed(object sender, ExecutedRoutedEventArgs e)
        {
            ICommand command = ApplicationCommands.Cut;

            command.Execute(null);
        }

     }
}


------------------------------------------------------------------------------------------------------------------

commandCut_Executed does not get called. (When you click on the button the focus is lost...)

The key is that we want controls to respond to ApplicationCommands not commands derived from RibbonControl.

Thanks,
Jan 5, 2009 at 6:33 PM

In order not to lose focus, you have to make the ribbon a FocusScope. All you need to do is set an attached property on the ribbon -  FocusManager.IsFocusScope="True"  .

<
r:Ribbon

 

 

x:Name="ribbon1"

 

 

Height="Auto" Width="Auto"

 

 

DockPanel.Dock="Top"

 

 

Title="{Binding RelativeSource={RelativeSource FindAncestor,AncestorType={x:Type Window}},Path=Title}"

 

 

ShowQuickAccessToolbarOnTop="True"

 

 

FocusManager.IsFocusScope="True"

 

 

 

 

 

 

>





 

Jan 7, 2009 at 12:28 PM

Thank you for your reply.

I tried setting the IsFocusScope to true. Now the behaviour has changed when I get the canexecute event.

The originalsource is no longer the ribbon control.

How do I find out the source of the command?

Also after clicking on the buttton I want the focus to return back to where it was. Basically behave like a normal toolbar.

 

Thanks,

 

John

Jan 7, 2009 at 12:36 PM
Yes, we can get RibbonCommands to route correctly.

The problem is that we want an applicationcommand to be routed instead.

The workaround so far is to handle the RibbonCommand and then raise a new ApplicationCommand instead.

Thanks,

John
Jan 7, 2009 at 3:25 PM
I couldn't agree more. Using ApplicationCommands  or any routed commands is very clumsy at the moment ; you have to write dozens of little handlers that do nothing but redirect.  Very tedious and not elegant.  The whole point of routed commands was to reduce clutter .  I'll be disappointed if the ribbon ships with this architecture .
May 27, 2009 at 11:16 AM

I agree that it must be FocusManager.IsFocusScope="True" in order to keep focus on command target. You can see at post ID 52089 how you can use RibbonCommand with DelegateCommand which is part of MVVM template. Look at post of daywalker. Declaration of CommandReference must be something like:

<commands:CommandReference x:Key="CutCommandReference" Command="ApplicationCommands.Cut"/>

I use it with my custom static commands like ApplicationCommands and all works well.

Best regards!!!

Aug 3, 2009 at 10:24 AM

Hi John

I'm having problems getting the Cut, Copy, Paste Ribbon buttons to work. I've used the code you posted and the Cut button (for example) gets enabled when I select some text in the text box. But when I click the Cut ribbon button, the Cut ribbon button gets disabled and the Execute event is never raised.

You said in your previous Email that you had a workaround. Can you please post the work around, so I can see how to do it.

 

Many thanks

Mark

 

Aug 8, 2009 at 8:18 AM

Hi Mark,

I didn't look the code posted by John but as banko said you need to set FocusManager.IsFocusScope="True" for the Ribbon. This will keep input focus onto TextBox. Your Execute event is never raised because at the moment you click the button it will grab the focus and CanExecute for your Cut command will be false. I hope this help you.