防止按Enter键提交WPF表单 [英] Preventing WPF form submit on pressing Enter key

查看:83
本文介绍了防止按Enter键提交WPF表单的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好,

Hello,

我有一个C#/WPF XAML屏幕,其中有多个控件...文本框,组合框,按钮等.

I've a C#/WPF XAML screen having multiple controls...textboxes, comboboxes, buttons etc.

我保留了IsDefault ="True"因此,如果用户在任意一个文本框中按Enter键,则表单将被提交.

I've kept IsDefault="True" for one of the buttons.Due to this, if user presses Enter key in any of the textboxes, the form is getting submitted.

我只需要为一个特定的文本框使用Enter键提交表单...如果用户在其他任何文本框中按Enter键,则我不希望提交该表单.我知道可以使用code-behind(xaml.cs)实现此目的.但是需要知道如何实现 请使用MVVM设计模式?

I need to submit the form using Enter key only for one specific textbox...if user presses Enter key in any of the other textboxes,I dont want the form to be submitted. I know I can achieve this using code-behind(xaml.cs).But need to know how do I achieve this using MVVM design pattern please?

推荐答案

>>我知道我可以使用code-behind(xaml.cs)实现但是,需要知道如何使用MVVM设计模式来实现这一目标吗?

不能. XAML是一种标记语言.它不打算用作 用于定义应用程序行为的 programming 语言.

You can't. XAML is a markup language. It is not intended to be used as a programming language that you use to define the behaviour of your application.

>>我只需要针对一个特定的文本框使用Enter键提交表单

然后,您可以在视图的代码隐藏文件中或在视图模型类(如果使用MVVM)中处理TextBox的PreviewKeyDown事件.  有关我如何在MVVM中处理事件的更多信息,请参阅我的博客文章: https://blog.magnusmontin.net/2013/06/30/handling-events-in-an-mvvm-wpf-application/.在这种情况下,可以在引发PreviewKeyDown事件时使用交互触发器来调用视图模型的命令.请阅读我的博客文章并参考 我在以下线程中的答案,以了解如何执行此操作的另一个示例:

Then you could handle the PreviewKeyDown event for the TextBox, either in the code-behind file of the view or in the view model class if you are using MVVM.  Please refer to my blog post for more information about how to handle events in MVVM: https://blog.magnusmontin.net/2013/06/30/handling-events-in-an-mvvm-wpf-application/. In this case you could use an interaction trigger to invoke a command of the view model when the PreviewKeyDown event is raised. Please read my blog post and refer to my answer in the following thread for yet another example of how to do this: https://social.msdn.microsoft.com/Forums/vstudio/en-US/b677f798-46d4-4add-9303-e1b9e8da1189/how-to-handle-event-when-any-cell-has-been-modified-in-data-grid-using-mvvm-pattern?forum=wpf

希望有帮助.

请记住,通过将有用的帖子标记为答案来关闭话题,然后在遇到新问题时开始新话题.请不要在同一线程中问几个问题

Please remember to close your threads by marking helpful posts as answer and then start a new thread if you have a new question. Please don't ask several questions in the same thread


这篇关于防止按Enter键提交WPF表单的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆