命令绑定和退格键

时间:2010-05-10 23:34:16

标签: c# wpf command

我需要将命令绑定到Backspace键。当键盘焦点位于文本框中时,需要启用该命令,而文本框又位于DataConmplate的ItemsControl内。以下代码似乎不承认正在按下退格键,并且未调用CommandBinding的CanExecute处理程序。在这种情况下使用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>
        <ItemsControl ItemsSource="{Binding Path=MyList}">
            <ItemsControl.ItemTemplate>
                <DataTemplate>
                    <TextBox Text="{Binding Path=Content}" />
                </DataTemplate>
            </ItemsControl.ItemTemplate>
        </ItemsControl>
    </Grid>
</Window>

这是相应的代码隐藏:

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 PropertyChangedEventArgs(PropertyName));
            }
        }
    }
}

非常感谢提前!

安德鲁

0 个答案:

没有答案