locked
Data binding and input validation in 8.1 universal app

    Question

  • I'm building a universal app for windows 8.1 and windows phone 8.1 and after some heavy googling I'm still stuck with user input validation.

    What I'm trying to do is correct user input on the fly. User is to input a phone number in a TextBox and digits should be corrected (using libphonenumber library) as he types.

    Solutions I found on the net don't seem to work for a 8.1 universal project (not sure, whether it's just my bad):

    • PreviewTextInput or PreviewKeyDown events are not available
    • System.ComponentModel.DataAnnotations.ValidationAttribute is not available
    • System.Windows.Data.Binding.ValidationRules is not available
    • System.Windows.Controls.ValidationRule is not available

    So, I resorted to listening to KeyDown event from my TextBox and to using KeyRoutedEventArgs.Handled to block invalid input. This looks to be a very ad-hoc and non-declarative way to me. What is the correct way of doing this?

    Thanks!

    • Edited by Michael Antipin Monday, November 24, 2014 10:50 AM prcise title
    Monday, November 24, 2014 9:32 AM

Answers

  • I don't see what's wrong with using Keydown/handled. 

    Matt Small - Microsoft Escalation Engineer - Forum Moderator
    If my reply answers your question, please mark this post as answered.

    NOTE: If I ask for code, please provide something that I can drop directly into a project and run (including XAML), or an actual application project. I'm trying to help a lot of people, so I don't have time to figure out weird snippets with undefined objects and unknown namespaces.

    Monday, November 24, 2014 8:20 PM
    Moderator

All replies

  • I don't see what's wrong with using Keydown/handled. 

    Matt Small - Microsoft Escalation Engineer - Forum Moderator
    If my reply answers your question, please mark this post as answered.

    NOTE: If I ask for code, please provide something that I can drop directly into a project and run (including XAML), or an actual application project. I'm trying to help a lot of people, so I don't have time to figure out weird snippets with undefined objects and unknown namespaces.

    Monday, November 24, 2014 8:20 PM
    Moderator
  • Tuesday, November 25, 2014 3:05 AM
  • Did you tried TextBox''s InputScope attribute?

    THKAW NTL-Network

    Thanks for pointing this out, this will help on mobile. But I'd like to use shared code for both desktop and mobile versions, and on desktop user has full keyboard anyway.

    Tuesday, November 25, 2014 9:38 AM
  • I don't see what's wrong with using Keydown/handled. 

    Matt Small Microsoft (MCC, MSFT)

    Well, I hoped there's something like ValidationRules and/or Converters I could use for that. I'd prefer to control this through XAML declarations and not through backing code. But if you say this is just Ok...
    Tuesday, November 25, 2014 9:41 AM