开发者

Command Binding And The Backspace Key

I need to bind a command to the Backspace key. The command needs to be enabled when keyboard focus is in a textbox, which in turn is inside of the DataTemplate for an ItemsControl. The following code doesn't seem to acknowledge that the backspace key is being depressed, and the CanExecute handler for the CommandBinding isn't getting called. Is it possible to have a command fire in this situation using the Backspace key? Any insight would be very much appreciated...

Here's all of the 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>
        <ItemsControl ItemsSource="{Binding Path=MyList}">
            <ItemsControl.ItemTemplate>
                <DataTemplate>
                    <TextBox Text="{Binding Path=Content}" />
                </DataTemplate>
            </ItemsControl.ItemTemplate>
        </ItemsControl>
    </Grid>
</Window>

And here's the corresponding code-behind:

using System.Collections.Generic;
using System.Windows;
using System.Windows.Input;
using System.ComponentModel;

namespace WpfApplication1
{
    /// <summary>
    /// Interaction logic for Window1.xaml
    /// </summary>
    public partial class Window1 : Window
    {
        public static RoutedCommand BackspaceCommand = new RoutedCommand();

        private List<SimpleString> myList;
        public List<SimpleString> MyList
        {
            get { return myList; }
            set { myList = value; }
        }

        public Window1()
        {
            myList = new List<SimpleString>();

            MyList.Add(new SimpleString("First string."));
            MyList.Add(new SimpleString("Second string."));
            MyList.Add(new SimpleString("Third string."));
            MyList.Add(new SimpleString("Fourth string."));

            InitializeComponent();

            KeyGesture BackspaceKG = new KeyGesture(Key.Back, ModifierKeys.None);
            InputBinding BackspaceIB = new InputBinding(BackspaceCommand, BackspaceKG);
            this.InputBindings.Add(BackspaceIB);
            CommandBinding BackspaceCB = new CommandBinding(BackspaceCommand);
            BackspaceCB.PreviewCanExecute +=
                new CanExecuteRoutedEventHandler(PreviewBackspaceCommandCanExecuteHandler);
            BackspaceCB.PreviewExecuted +=
                new ExecutedRoutedEventHandler(PreviewBackspaceCommandExecutedHandler);
            this.CommandBindings.Add(BackspaceCB);

            DataContext = this;
        }

        private void PreviewBackspaceCommandCanExecuteHandler(object sender, CanExecuteRoutedEventArgs e)
        {
            MessageBox.Show("Inside PreviewBackspaceCommandCanExecuteHandler");
            e.CanExecute = true;
        }

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

    public class SimpleString : INotifyPropertyChanged
    {
        private string content;
        public string Content
        {
            get { return content; }
            set
            {
                content = value;
                OnPropertyChanged("Content");
            }
        }

        public SimpleString(string p_content)
        {
            Content = p_content;
        }

        public event PropertyChangedEventHandler PropertyChanged;
        private void OnPropertyChanged(string PropertyName)
        {
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyC开发者_StackOverflow社区hangedEventArgs(PropertyName));
            }
        }
    }
}

Many thanks in advance!

Andrew

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜