【问题标题】:TextBox KeyDown Trigger Event not working for Backspace and Delete keyTextBox KeyDown 触发事件不适用于 Backspace 和 Delete 键
【发布时间】:2014-09-27 09:19:38
【问题描述】:

我有一个文本框,我为该文本框附加了一个 keydown 事件。一切正常,但我只是注意到当我按下“Backspace”和“Delete”键时,没有调用绑定命令。

我的视图 xaml 文件:-

<TextBox x:Name="textBox" Width="500" Text="{Binding TextBoxText, UpdateSourceTrigger=PropertyChanged}">
<i:Interaction.Triggers>                
    <i:EventTrigger EventName="KeyDown">  
        <cmd:EventToCommand Command="{BindingPath=TextBoxKeyDownEvent}" PassEventArgsToCommand="True" />                
    </i:EventTrigger>            
</i:Interaction.Triggers>
</TextBox>

我的 ViewModel cs 文件:-

    //TextBox Key Down Event Handler
    private DelegateCommand _textBoxKeyDownEvent;
    public ICommand TextBoxKeyDownEvent
    {
        get
        {
            if (_textBoxKeyDownEvent == null)
            {
                _textBoxKeyDownEvent = new DelegateCommand(TextBoxKeyDownEventHandler);
            }
            return _textBoxKeyDownEvent;
        }
        set { }
    }

谁能给我一些建议

【问题讨论】:

  • EventToCommand 是您需要深入研究的黑匣子。
  • 您可以使用 KeyBinding - 但我猜您的 TextBoxText 属性已更新。为什么在文本更改时收到通知时需要捕获 Keys?
  • @kallocaion:感谢您的回答。 Bur 对于我的情况 Binding textProperty 将不起作用。因为程序会动态更改文本框的内容,然后我不想调用我的 eventHandler。我有其他事件与同一组件相关联,我无法切换到其他事件,例如 keyup。

标签: c# wpf mvvm


【解决方案1】:

编辑: 您必须使用PreviewKeyDown 它才能工作。 KeyDown 不会在 Space 和 Delete 上触发。如果您忽略 MVVM 并将 KeyDown 的处理程序放在代码隐藏中 也会失败


如何将文本属性绑定到视图模型中的字符串?

我为我的想法构建了一个快速、简单的示例。

结果

左侧文本框中的文本被简单地填充到右侧的文本块中。

查看

<Window x:Class="WpfApplication1.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="350" Width="525">
    <StackPanel Orientation="Horizontal">
        <TextBox Text="{Binding TextBoxValue, UpdateSourceTrigger=PropertyChanged}"  Width="250"/>
        <StackPanel Orientation="Horizontal">
            <TextBlock>"</TextBlock>
            <TextBlock Text="{Binding TextBoxValue, UpdateSourceTrigger=PropertyChanged}" />
            <TextBlock>"</TextBlock>
        </StackPanel>
    </StackPanel>
</Window>

视图模型

public class MainWindowViewModel : INotifyPropertyChanged
{
    private string textBoxValue;

    public string TextBoxValue
    {
        get { return textBoxValue; }
        set
        {
            textBoxValue = value;
            OnTextBoxValueChanged();
            RaisePropertyChanged();
        }
    }

    void OnTextBoxValueChanged()
    {
        // you logic here, if needed.
    }

    #region INotifyPropertyChanged implementation

    public event PropertyChangedEventHandler PropertyChanged;

    void RaisePropertyChanged([CallerMemberName] string propertyName = null)
    {
        if (PropertyChanged != null)
            PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
    }

    #endregion
}

【讨论】:

  • 感谢您的回答。 Bur 对于我的情况 Binding textProperty 将不起作用。因为程序会动态更改文本框的内容,然后我不想调用我的 eventHandler。我有其他事件与同一组件相关联,我无法切换到其他事件,例如 keyup。
  • 感谢 ec80r。你是对的。我必须使用 PreviewKeyDown。它现在可以工作了。
【解决方案2】:

你最常使用 PreviewKeyDown 事件。

像这样:

 <EventSetter Event="PreviewKeyDown" Handler="TextBox_PreviewKeyDown"/>

【讨论】:

    【解决方案3】:

    编辑:你是对的——默认行为没有被执行。您应该使用 ec8ors 解决方案,无论如何这要好得多:

    <TextBox x:Name="textBox" Width="500" Text="{Binding TextBoxText, UpdateSourceTrigger=PropertyChanged}">
        <i:Interaction.Triggers>
           <i:EventTrigger EventName="PreviewKeyDown">
               <i:InvokeCommandAction Command="{Binding TextBoxKeyDownEvent, Mode=OneWay}"/>
           </i:EventTrigger>
        </i:Interaction.Triggers>
    </TextBox>
    

    原创: 您可以在按下“特殊”键时使用 InputBindings 调用您的命令:

    <TextBox x:Name="textBox" Width="500" Text="{Binding TextBoxText, UpdateSourceTrigger=PropertyChanged}">
      <i:Interaction.Triggers>                
          <i:EventTrigger EventName="KeyDown">  
              <cmd:EventToCommand Command="{BindingPath=TextBoxKeyDownEvent}" PassEventArgsToCommand="True" />                
          </i:EventTrigger>            
       </i:Interaction.Triggers>
    
       <TextBox.InputBindings>
          <KeyBinding Command="{Binding TextBoxKeyDownEvent}" Key="Delete" />
          <KeyBinding Command="{Binding TextBoxKeyDownEvent}" Key="Back" />
       </TextBox.InputBindings>
    </TextBox>
    

    【讨论】:

    • 我试过你的方法。但在这种情况下,键绑定方法被调用并执行,但 Delete 和 Back 键失去了它们的默认行为。这意味着他们不会从光标位置删除内容。
    猜你喜欢
    • 2012-09-04
    • 2011-05-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-02-20
    • 1970-01-01
    相关资源
    最近更新 更多