How to make an invisible button to run a command

I have a form with a TextBox and two buttons. One button has the IsDefault property set to true, and IsCancel has the true value for the other button. TextBox - CommandTarget for both buttons. When I press Enter or ESC in a TextBox, it works when I press the corresponding button.

I want to remove buttons from a form. They should not be visible, but the text field should respond to Enter or ESC, as before. I can’t just set the “Visible” property of the button to crash - in this case they don’t work at all. And I prefer to avoid tracking keyboard events. Is it possible?

+3
source share
3 answers

Skeets ' Abe , . , WPF InputGesture, KeyGesture ("enter" "escape" ). KeyGestures, CommandBinding Visual Tree. :

<Window x:Class="CommandSpike.Window1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="clr-namespace:CommandSpike"
    Title="Window1" Height="300" Width="300">
    <StackPanel>
        <Grid>
            <Grid.CommandBindings>
                <CommandBinding x:Name="EnterBinding"
                                Command="{x:Static local:Commands.EnterCommand}"
                                CanExecute="CommandBinding_CanExecute"
                                Executed="EnterBinding_Executed"/>
                <CommandBinding x:Name="CancelBinding"
                                Command="{x:Static local:Commands.CancelCommand}"
                                CanExecute="CommandBinding_CanExecute"
                                Executed="CancelBinding_Executed"/>
            </Grid.CommandBindings>
            <TextBox>
                Press Enter or Cancel when I have focus...
            </TextBox>
        </Grid>
        <TextBox Margin="0,4">
            Pressing Enter or Cancel does nothing while I have focus!
        </TextBox>
    </StackPanel>
</Window>

using System.Windows;
using System.Windows.Input;

namespace CommandSpike
{
    public partial class Window1 : Window
    {
        public Window1()
        {
            InitializeComponent();
        }

        private void CommandBinding_CanExecute(object sender, CanExecuteRoutedEventArgs e)
        {
            e.CanExecute = true;
        }

        private void EnterBinding_Executed(object sender, ExecutedRoutedEventArgs e)
        {
            MessageBox.Show("Enter");
        }

        private void CancelBinding_Executed(object sender, ExecutedRoutedEventArgs e)
        {
            MessageBox.Show("Cancel");
        }
    }
}

using System.Windows.Input;

namespace CommandSpike
{
    public static class Commands
    {
        public static RoutedUICommand EnterCommand { get;private set; }
        public static RoutedUICommand CancelCommand { get; private set; }

        static Commands()
        {
            EnterCommand=new RoutedUICommand("Enter",
                                             "EnterCommand",
                                             typeof(Commands));
            EnterCommand.InputGestures.Add(
                                            new KeyGesture(Key.Enter)
                                           );
            CancelCommand=new RoutedUICommand("Cancel",
                                            "CancelCommand",
                                            typeof(Commands));
            CancelCommand.InputGestures.Add(
                                            new KeyGesture(Key.Escape)
                                            );
        }
    }
}
+4

, ? :

  • 0
  • / 0
  • RenderTransform, .
+2

ControlTemplate:

<ControlTemplate x:Key="blankButton" TargetType="{x:Type Button}" />

...

<Button IsDefault="True" ... Template="{StaticResource blankButton}" />
<Button IsCancel="True" ... Template="{StaticResource blankButton}" />
+1

Source: https://habr.com/ru/post/1739206/


All Articles