1

I have a textbox in WPF.

<TextBox Grid.Row="3" FontSize="30"></TextBox>

I need to enter only 10 digit phone numbers to it. Displaying format should be xxx-xxx-xxxx. Is it possible to handle this situation from XAML itself without the help of any code?. If No How it is possible to do it with the help of code?

Alias Varghese
  • 2,104
  • 3
  • 24
  • 52
  • No, pure xaml is not possible. see [this](http://stackoverflow.com/a/1103822/1648849) answer. – Bahman_Aries Jul 14 '15 at 09:44
  • [MaskedTextBox](https://wpftoolkit.codeplex.com/wikipage?title=MaskedTextBox). – Sinatr Jul 14 '15 at 09:44
  • IMO it's not a duplicate because of XAML remark. – Bahman_Aries Jul 14 '15 at 09:48
  • That question didn't provide me answer. – Alias Varghese Jul 14 '15 at 09:58
  • @AliasVarghese An `AttachedBehavior` would be your best option, as described in the [linked question's answers](http://stackoverflow.com/a/7572924/302677). It is some very useful helper code that I would put in my core library to use in other WPF projects as well whenever this functionality was needed. Alternatively, you could try to handle it your own way by using the `PreviewTextInput` event. – Rachel Jul 14 '15 at 15:04

2 Answers2

0

try using

<TextBox Grid.Row="3" MaxLength="10(or your limit)" FontSize="30"></TextBox>

you can set the limit in xaml as well as in your view-model or code-behind file.

DeshDeep Singh
  • 1,817
  • 2
  • 23
  • 43
-2

use this function

   public string phoneformat(string phnumber)
   {
     String phone=phnumber;
     string countrycode = phone.Substring(0, 3); 
     string Areacode = phone.Substring(3, 3); 
     string number = phone.Substring(6,phone.Length); 
     phnumber="("+countrycode+")" +Areacode+"-" +number ;
     return phnumber;
   }
Sabir
  • 235
  • 4
  • 15