繁体   English   中英

TextBox KeyDown触发器事件不适用于Backspace和Delete键

[英]TextBox KeyDown Trigger Event not working for Backspace and Delete key

我有一个文本框,对于该文本框,我附加了一个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 { }
    }

有人可以给我一些建议

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


如何将Text-Property绑定到viewmodel中的字符串?

我构建了一个快速,简单的例子。

结果

左侧TextBox中的文本只是填充到右侧的Textblock。

在此输入图像描述

视图

<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
}

你最常使用的是PreviewKeyDown事件。

像这样:

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

编辑 :您是正确的 - 不执行默认行为。 您应该使用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>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM