具有 MVVM 模式的数字文本框
我已经在 WPF
中看到了带有代码隐藏的数字 TextBox
的实现。我们如何在 MVVM 模式中做到这一点?
I have seen implementations of numeric TextBox
with code behind in WPF
. How do we do this in MVVM
pattern?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
在 WPF 中,如果将 TextBox 绑定到 Decimal 或 Int 属性,它将仅接受该 int 或decimal,否则它将显示红色边框,表明它在绑定中没有正确的值。如果您正在谈论数字向上文本框,那么可以通过 此处 使用 WPF 工具包轻松使用它
In WPF if you bind the TextBox to a Decimal or a Int Property it will accept only that int or decimal otherwise it will show a red border that it does not have proper value in the binding. And if you are talking about the numeric updown textbox then it is readily available with WPF toolkit over here
老实说 - MVVM 和数字文本框有什么共同点?
如果您想要一个数字文本框,您可以创建一个新的文本框或 AttachedProperty 或行为。
这里是一个 MaskedTextbox 行为的示例我的意思是。
现在到你的 MVVM 部分。我假设您想验证您的输入只是数字。如果您的视图模型有一个 int 类型的属性,那么当您的视图获得可转换为 int 的输入时,您的绑定就会起作用。否则你的视图模型将永远不会被通知。现在有两种方法:
第一:确保您的视图只能接受数字输入(使用数字文本框)并且 viewmodel 属性可以是 int 。
或第二:您的 viewmodel 属性类型是 typeof string 并且您使用 IDataErrorInfo 让视图知道输入何时不是数字。
honestly - what does MVVM and numeric textbox have in common?
if you want a numeric textbox you create a new TextBox or AttachedProperty or a Behaviour.
Here is an example for a MaskedTextbox behaviour to see what i mean.
now to your MVVM part. i assume that you want to validate your input to be just numeric. if your viewmodel has an Property of type int, then your binding just works if your view got input which is convertable to int. otherwise your viewmodel will never be informed. there are 2 ways now:
first: you make sure that your view just can take numeric input (with your numeric textbox) and the viewmodel property can be int.
or second: your viewmodel property type is typeof string and you use IDataErrorInfo to let the view know when the input is not numeric.
根据 MVVM 的标准定义,您不希望在自定义控件后面使用 ViewModel。您应该做的就是扩展 TextBox 控件并确保只输入数字。您还应该添加一个返回数字输入的 DependencyProperty。
当在窗口或复合控件中使用该控件时,ViewModel 就会出现。您可以将 Text 或 Numeric DependencyProperty 绑定到 ViewModel 中的公共属性。
By the standard definition of MVVM you would not want a ViewModel behind a custom control. All you should do is extend the TextBox control and ensure only numeric input is entered. You should also add a DependencyProperty that returns the numeric input.
The ViewModel would come in when that control is used in a window or a composite control. You would bind the Text or Numeric DependencyProperty to a public property in your ViewModel.
好吧...如果您想在数字文本框的文本属性更改时在视图模型中收到通知,只需绑定到它即可。如果数字文本框的 .Text 属性不是依赖属性,请扇编码器一巴掌!
这一个: http://wpftoolkit.codeplex.com/wikipage?title=DecimalUpDown& referencingTitle=Home
我可以推荐,您可以通过以下方式从视图模型绑定到它:
Well... if you want to be notified in your viewmodel when the text property of the numeric textbox changes just bind to it. If the .Text property of the numeric textbox is not a dependency property slap the coder!
this one: http://wpftoolkit.codeplex.com/wikipage?title=DecimalUpDown&referringTitle=Home
I can recommend and you can bind to it from the viewmodel via:
如果您确实想在 ViewModel 中执行此操作,则必须将绑定属性设置为字符串。确保每次击键时绑定都会更新(使用
UpdateSourceTrigger
)。在您的设置器中,通过引发异常或删除非数字字符来拒绝非数字值。后一种方法的优点是适用于复制/粘贴操作,其中粘贴的文本可能包含数字和字母的混合,但仅必须保留数字。
话虽这么说,我同意其他建议,即拥有一个仅公开数字属性的专用控件是一种更简洁的方法。
问候,
埃里克。
If you really wanted to do this in the ViewModel, you'd have to make your bound property a string. Ensure that the binding updates at each keystroke (using the
UpdateSourceTrigger
).In your setter, reject non-numeric values by either raising an exception or trimming out non-numerical characters. The latter approach has the benefit of working for copy/paste operations where the pasted text may contain a mix of digits and letters, but only the digits must be retained.
That being said, I would agree with other suggestions that having a specialized control that only exposes a numeric property is a cleaner approach.
Regards,
Eric.