Search code examples
.netwpfxamlwpf-controlsrouted-commands

RoutedUICommands and currently focused element


Does a RoutedUICommand automatically reach the currently focused control (provided the control has the right command binding)? E.g.:

<Button Focusable="False" Command="ApplicationCommands.Open" Content="Open" />

<UserControl Name="ctl" Focusable="True" IsTabStop="True">
    <UserControl.CommandBindings>
        <CommandBinding Command="ApplicationCommands.Open" CanExecute="CanOpen" Executed="OpenExecuted"/>
        <!-- ... -->
    </UserControl.CommandBindings>
</UserControl>

Will ApplicationCommands.Open reach UserControl when it has focus, without declaring explicit CommandTarget on Button? Thank you.


Solution

  • EDITED I tried it and it doesn't seem to be the case, at least not for UserControl.

    It DOES indeed, when you specify FocusManager.IsFocusScope="true" on the source of the command. A similar question answered.

    XAML:

    <Window x:Class="TestApp.MainWindow"
            xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
            xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
            Title="MainWindow" Height="200" Width="320">
        <StackPanel>
            <Button FocusManager.IsFocusScope="true" Focusable="False" Command="ApplicationCommands.Open" Content="Open: Binding MainWindow" Width="200" CommandTarget="{Binding MainWindow}"/>
            <Button FocusManager.IsFocusScope="true" Focusable="False" Command="ApplicationCommands.Open" Content="Open: no CommandTarget" Width="200" />
            <Button FocusManager.IsFocusScope="true" Focusable="False" Command="ApplicationCommands.Open" Content="Open: Binding ElementName=ctl" Width="200" CommandTarget="{Binding ElementName=ctl}"/>
            <Button FocusManager.IsFocusScope="true" Focusable="False" Content="Where is the focus?" Click="Button_Click"  Width="200"/>
            <UserControl Name="ctl" Focusable="True" IsTabStop="True">
                <UserControl.CommandBindings>
                    <CommandBinding Command="ApplicationCommands.Open" CanExecute="CanOpen" Executed="OpenExecuted"/>
                </UserControl.CommandBindings>
            </UserControl>
        </StackPanel>
    </Window>
    

    C#:

    using System.Windows;
    using System.Windows.Input;
    
    namespace TestApp
    {
        public partial class MainWindow : Window
        {
            public MainWindow()
            {
                InitializeComponent();
                this.Loaded += MainWindow_Loaded;
            }
    
            void MainWindow_Loaded(object sender, RoutedEventArgs e)
            {
                ctl.Focus();
            }
    
            private void CanOpen(object sender, CanExecuteRoutedEventArgs e)
            {
                e.CanExecute = true;
            }
    
            private void OpenExecuted(object sender, ExecutedRoutedEventArgs e)
            {
                MessageBox.Show("OpenExecuted");
            }
    
            private void Button_Click(object sender, RoutedEventArgs e)
            {
                var focusedElement = FocusManager.GetFocusedElement(this);
                MessageBox.Show(focusedElement != null ? focusedElement.GetType().ToString() : "none");
            }
        }
    }