텍스트 상자 안에 Backspace 키 제스처를 캡처하는 사용자 지정 명령을 구현하고 싶지만 어떻게해야할지 모르겠다. 나는 무슨 일이 벌어지는 지 이해하기 위해 테스트 프로그램을 작성했지만 프로그램의 행동은 다소 혼란 스럽다. 기본적으로 키보드 포커스가 텍스트 상자에 있고 텍스트 상자 내의 Backspace 키의 정상적인 동작을 방해하지 않으면 서 wpf 명령을 통해 Backspace 키 제스처를 처리 할 수 있어야합니다. 다음은 메인 창에 대한 XAML 및 대응의 코드 숨김도 (키 입력을 위해 난 그냥 백 스페이스 키의 그것의 행동을 비교, 두 번째 명령을 만든 주) : 여기라우팅 된 명령 질문
<Window x:Class="WpfApplication1.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Window1" Height="300" Width="300">
<Grid>
<TextBox Margin="44,54,44,128"
Name="textBox1" />
</Grid>
</Window>
과 해당 코드 숨김 :
using System.Windows;
using System.Windows.Input;
namespace WpfApplication1
{
/// <summary>
/// Interaction logic for EntryListView.xaml
/// </summary>
public partial class Window1 : Window
{
public static RoutedCommand EnterCommand = new RoutedCommand();
public static RoutedCommand BackspaceCommand = new RoutedCommand();
public Window1()
{
InitializeComponent();
CommandBinding cb1 = new CommandBinding(EnterCommand, EnterExecuted, EnterCanExecute);
CommandBinding cb2 = new CommandBinding(BackspaceCommand, BackspaceExecuted, BackspaceCanExecute);
this.CommandBindings.Add(cb1);
this.CommandBindings.Add(cb2);
KeyGesture kg1 = new KeyGesture(Key.Enter);
KeyGesture kg2 = new KeyGesture(Key.Back);
InputBinding ib1 = new InputBinding(EnterCommand, kg1);
InputBinding ib2 = new InputBinding(BackspaceCommand, kg2);
this.InputBindings.Add(ib1);
this.InputBindings.Add(ib2);
}
#region Command Handlers
private void EnterCanExecute(object sender, CanExecuteRoutedEventArgs e)
{
MessageBox.Show("Inside EnterCanExecute Method.");
e.CanExecute = true;
}
private void EnterExecuted(object sender, ExecutedRoutedEventArgs e)
{
MessageBox.Show("Inside EnterExecuted Method.");
e.Handled = true;
}
private void BackspaceCanExecute(object sender, CanExecuteRoutedEventArgs e)
{
MessageBox.Show("Inside BackspaceCanExecute Method.");
e.Handled = true;
}
private void BackspaceExecuted(object sender, ExecutedRoutedEventArgs e)
{
MessageBox.Show("Inside BackspaceExecuted Method.");
e.Handled = true;
}
#endregion Command Handlers
}
}
어떤 도움을 주시면 감사하겠습니다. 감사! TextBlock을 대신 메인 창에 입력 바인딩 및 명령 바인딩을 추가
앤드류
나는 'e.CanExecute = true;'를 사용한다는 것을 알았습니다. BackspaceCanExecute라는 핸들러에서 'e.Handled = true;'대신 '. 비록이 수정을하더라도, 나는 여전히 프로그램의 행동을 이해할 수 없다. (1) EnterCanExecute는 EnterExecuted를 호출 할 때마다 두 번 호출됩니다. (2) BackspaceCanExecute는 텍스트 상자에 키보드 포커스가 없을 때만 호출됩니다. 그런데도 BackspaceExecuted는 호출되지 않습니다. – Andrew