LoginSignup
15
16

More than 5 years have passed since last update.

TextBoxでEnter Keyが押されたことを検知して入力値も取得する方法

Posted at

検索ボックスなどでよくわるキーワードを入力し、Enter Key押下で実行みたいなことをやろうとしたときに意外とハマったのでメモ。
最初イベントだけ検知して入力文字列はTextにバインドさせたSearchWordTxtから取得しようとしたのだけれど、TextBoxのTextに値がはいるのはフォーカスが外れた後、つまりEnter Key押下のイベントを検知した段階ではまだSearchWordTxtはNullだった。
なるほどなーと思いどうにかイベント検知時にイベント引数として入力文字列を渡せないかとググってみるとstackoverflow先生が答えを教えてくれました。
http://stackoverflow.com/questions/5556489/capturing-the-enter-key-in-a-textbox

        <TextBox x:Name="SearchWord" Text="{Binding SearchWordTxt, Mode=TwoWay}" Height="23">
            <TextBox.InputBindings>
                <KeyBinding Gesture="Enter" Command="{Binding SearchCommand}" CommandParameter="{Binding ElementName=SearchWord, Path=Text}"/>
            </TextBox.InputBindings>
        </TextBox>
15
16
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
15
16