주로 로그인 화면에서 필요한 기능이죠. 꼭 로그인 화면이 아니더라도 은근히 자주 쓰입니다. MVVM 패턴으로 구현 시 InputBinding 클래스를 사용하여 Command와 Binding 하는 코드입니다.
✅ MVVM 패턴에서 Enter 입력 시 Command 실행
InputBinding을 사용한 Command Binding
<PasswordBox VerticalContentAlignment="Center"
x:Name="AzurePasswordBox"
FontSize="16"
Height="50"
TabIndex="1"
PasswordChanged="AzurePasswordBox_PasswordChanged">
<PasswordBox.InputBindings>
<KeyBinding Gesture="Enter" Command="{Binding CmdLogin}"/>
</PasswordBox.InputBindings>
</PasswordBox>
<!-- TextBox도 위 예제와 동일하므로 코드는 생략하겠습니다. -->
KeyBinding 의 Gesture 발생 시 Command="{Binding CmdLogin}" 의 CmdLogin 이 호출됩니다.
Command 속성의 Binding에 대해서는 [WPF] - MVVM 기초 - Command Binding 포스팅을 참고하세요.
PasswordChanged 속성에 Codebehind가 쓰인 이유
PasswordBox의 Password 속성은 SecureString으로 TextBox의 Text처럼 ViewModel에 바로 Binding 되지 않습니다. 이런 이유로 사용한 것으로 다음 포스팅에서 다루도록 하겠습니다.
KeyBinding의 Gesture 속성에는 여러 조합이 가능한데 추후에 정리할 예정입니다.
✅ MVVM 패턴에서 Enter 입력 시 Command 실행 - 끝
관련 포스팅
[WPF] - MVVM 기초 - Command Binding