2

I am attempting to create a TextBox that only allows numeric characters and a decimal point. I don't need assistance in writing the code, but on the concept. I am using MVVM to design the WPF application and I'm not sure whether to use an event or event-to-command.

I have read several different viewpoints regarding this topic:

  • (I have found this to be a little extreme and as some have called it "counter-productive", but it upholds the "purity" of MVVM): Never have any code behind your View. To prevent this, use MVVM Light Library. In short, convert events to commands so that everything can be controlled in the ViewModel.
  • (The second argument does not uphold the (maybe over excessive) "purity" of MVVM): Not everything must be handled in the ViewModel and it is ok to create Events to handle certain UI requirements.

I was leaning more towards the second option because of simplicity and, as I have stated previously, the first option seems a little extreme. In my specific case (creating a numeric only TextBox) would most people prefer either of the above options or one I have not discovered?

Community
  • 1
  • 1
Jordan Carroll
  • 696
  • 2
  • 11
  • 29

2 Answers2

5

You should handle this as an event in .cs file. You are trying to add functionality in a control. Like Text in a TextBox .They all are handeld in .cs file. ViewModel is resposible for holding the data and Behavior based on that Data for View not for the functionality of Control.

yo chauhan
  • 12,079
  • 4
  • 39
  • 58
  • Ok, that makes sense. Would the method for verifying the text also be in the .cs file? – Jordan Carroll Feb 21 '13 at 16:09
  • Yes It should also be in .cs file. It should be generic like it accepts only numeric values and expose Dependencies Properties like Range Max and Min value . Now to handle this Range is the responsibility of your ViewModels , Like in one VM you have Age property that can have Value from 10-99 and another VM have another Property that Range from 1-99999.Its responsibilty of your control to expose these Properties for Binding. – yo chauhan Feb 21 '13 at 16:23
  • Got yah, thanks a lot. Last question, I'm feeling that if the user inputs an Age greater than 99 it should "down-convert" it automatically to 99. Is there a way to do that in the `ViewModel`? I know how to use `Validation.HasError` where the `TextBox` is wrapped in red, but I want something more "dummy proof". – Jordan Carroll Feb 21 '13 at 16:48
  • Yes you are correct through ViewModel you can show Validation, but if you want to down convert like user cant enter greater than Max Range you will have to handel PreviewKeyDown before you give it up to base.PreviewKeyDown that is PreviewKeyDown of your TextBox if your control inherits from TextBox. – yo chauhan Feb 21 '13 at 16:55
0

This should be handled directly in the View rather than involving the ViewModel, but there's no need to reinvent the wheel.

Depending on your exact numeric requirements, use a control such as DoubleUpDown or IntegerUpDown from the Extended WPF Toolkit (available via NUGet)

Peregrine
  • 4,287
  • 3
  • 17
  • 34