(精华)2020年01月26日 WPF课程管理系统项目实战(登陆界面-input聚焦和enter事件监听)

169 阅读1分钟

监听enter事件触发

<TextBox.InputBindings>
                        <KeyBinding Key="Enter" Command="{Binding LoginCommand}"
                        CommandParameter="{Binding ElementName=window}"/>
                    </TextBox.InputBindings>

聚焦

<Grid Grid.Row="1" Margin="30,10" FocusManager.FocusedElement="{Binding ElementName=txtUserName}">
                <TextBox Height="42" Text="{Binding LoginModel.UserName,UpdateSourceTrigger=PropertyChanged}" 
                         FontSize="16" Foreground="#555" Template="{StaticResource UserNameTextBoxTemplate}"
                         Name="txtUserName">
                    <TextBox.InputBindings>
                        <KeyBinding Key="Enter" Command="{Binding LoginCommand}"
                        CommandParameter="{Binding ElementName=window}"/>
                    </TextBox.InputBindings>
                </TextBox>
</Grid>