Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

TextBox KeyDown Trigger Event not working for Backspace and Delete key

Tags:

c#

mvvm

wpf

I have a Textbox and for that textbox I have attached a keydown event. Everything is working fine but I just noticed that when i'm pressing the 'Backspace' and 'Delete' Key, the binding command is not being called.

My View xaml file :-

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

My ViewModel cs file :-

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

Can somebody give me some suggestion

like image 763
ifti24 Avatar asked Sep 27 '14 09:09

ifti24


People also ask

How do you check Backspace in KeyPress?

You can capture a backspace key on the onkeydown event. Once you get the key of the pressed button, then match it with the Backspace key code. A Backspace key keycode it 8. Use keydown instead of keypress .

How do I check my backspace?

Take a straight edge and lay it diagonally across the inboard flange of the wheel. Take a tape measure and measure the distance from where the straight edge contacts the inboard flange to the hub mounting pad of the wheel. This measurement is backspace.


1 Answers

EDIT: You have to use PreviewKeyDown the it works. KeyDown is not fired on Space and Delete. If you ignore MVVM and put the handler of KeyDown in codebehind it will also fail.


How about binding the Text-Property to a string in you viewmodel?

I build a fast, simple example of my idea.

Result

Text from the TextBox on the left side is simply populated to the Textblock on the right side.

enter image description here

View

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

ViewModel

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
}
like image 133
devmb Avatar answered Nov 14 '22 21:11

devmb