How to make an invisible button to run a command
I have a form with TextBox and two Buttons. One button has IsDefault property set to true, and IsCancel set to true for other button. TextBox is CommandTarget for both buttons. When I'm pressing Enter or ESC keys on TextBox, it works as I'm pressing on corresponding button.
I want to remove buttons from the form. They should not be visible, but the textbox should react on Enter or ESC as before. I cannot just set button's Visible property to collapsed - in this case they does not work at all.开发者_如何转开发 And I prefer to avoid of tracking keyboard events. Is it possible?
While Skeets' and Abe's methods work, they are hacks. You can simply specify that a WPF command should also be invoked by a so called InputGesture, in this case a KeyGesture ("enter", or "escape"). You can set the scope of this KeyGestures by placing the CommandBinding for the command at the appropriate level in the Visual Tree. Like this:
<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)
);
}
}
}
Have you tried other mechanisms to make the buttons invisible? Here are some suggestions:
- Set Opacity to 0
- Set Width/Height to 0
- Set a RenderTransform that moves the buttons off-screen
I would give them an empty ControlTemplate
:
<ControlTemplate x:Key="blankButton" TargetType="{x:Type Button}" />
...
<Button IsDefault="True" ... Template="{StaticResource blankButton}" />
<Button IsCancel="True" ... Template="{StaticResource blankButton}" />
精彩评论