格式化TextBox中的电话号码

时间:2023-01-14 14:24:16

I have a DataGrid in a WPF window. How can I display a phone number string column in the DataGrid in a format of "(999)999-9999"?

我在WPF窗口中有一个DataGrid。如何在DataGrid中以“(999)999-9999”格式显示电话号码字符串列?

The Phone number column in the DataGrid uses a TextBlock in the CellTemplate and a TextBox in the CellEditingTemplate. The phone number is stored as a string with no formating such as "9995551234".

DataGrid中的电话号码列使用CellTemplate中的TextBlock和CellEditingTemplate中的TextBox。电话号码存储为不带格式的字符串,例如“9995551234”。

Is it possible to display the phone as:(999)555-1234 and edit it as (999)555-1234?

是否可以将手机显示为:(999)555-1234并将其编辑为(999)555-1234?

3 个解决方案

#1


9  

Try using Text="{Binding PhoneNumber, StringFormat={}{0:(###)###-####}}"

尝试使用Text =“{Binding PhoneNumber,StringFormat = {} {0:(###)### - ####}}”

Edit

编辑

If your PhoneNumber property is of type string, then there's not really a lot you can do with StringFormat to format it.

如果你的PhoneNumber属性是string类型,那么你可以用StringFormat来格式化它。

In the past when I've wanted to do something like this, I expose a property called FormattedPhoneNumber which returns the formatted phone number for display purposes, and the edit box just binds to plain old unformatted PhoneNumber

在过去,当我想做这样的事情时,我公开了一个名为FormattedPhoneNumber的属性,它返回格式化的电话号码以供显示,编辑框只是绑定到普通的未格式化的PhoneNumber

public string FormattedPhoneNumber
{
    get
    {
        if (PhoneNumber == null)
            return string.Empty;

        switch (PhoneNumber.Length)
        { 
            case 7:
                return Regex.Replace(PhoneNumber, @"(\d{3})(\d{4})", "$1-$2");
            case 10:
                return Regex.Replace(PhoneNumber, @"(\d{3})(\d{3})(\d{4})", "($1) $2-$3");
            case 11:
                return Regex.Replace(PhoneNumber, @"(\d{1})(\d{3})(\d{3})(\d{4})", "$1-$2-$3-$4");
            default:
                return PhoneNumber;
        }
    }
}

#2


2  

after a short google search i found this two links the second one is in german

经过短暂的谷歌搜索,我发现这两个链接第二个是德语

WPF – Masked Textbox Behavior http://marlongrech.wordpress.com/2007/10/28/masked-textbox/

WPF - 蒙面文本框行为http://marlongrech.wordpress.com/2007/10/28/masked-textbox/

Masked TextBox http://blindmeis.wordpress.com/2010/06/01/wpf-masked-textbox-behavior/

Masked TextBox http://blindmeis.wordpress.com/2010/06/01/wpf-masked-textbox-behavior/

hope this helps

希望这可以帮助

#3


0  

I would like to extend what already Rachel has responded. If a phone number is an integer, StringFormat would work just fine. In case a phone number is a string, I found Converter to be quite handy. This removes the need to create additional property for a class.

我想扩展Rachel已经做出的回应。如果电话号码是一个整数,StringFormat可以正常工作。如果电话号码是一个字符串,我发现转换器非常方便。这消除了为类创建附加属性的需要。

Here is an example:

这是一个例子:

public class StringToPhoneConverter : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
    {
        if (value == null)
            return string.Empty;

        //retrieve only numbers in case we are dealing with already formatted phone no
        string phoneNo = value.ToString().Replace("(", string.Empty).Replace(")", string.Empty).Replace(" ", string.Empty).Replace("-", string.Empty);

        switch (phoneNo.Length)
        {
            case 7:
                return Regex.Replace(phoneNo, @"(\d{3})(\d{4})", "$1-$2");
            case 10:
                return Regex.Replace(phoneNo, @"(\d{3})(\d{3})(\d{4})", "($1) $2-$3");
            case 11:
                return Regex.Replace(phoneNo, @"(\d{1})(\d{3})(\d{3})(\d{4})", "$1-$2-$3-$4");
            default:
                return phoneNo;
        }
    }

    public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
    {
        return value;
    }
}

xaml:

XAML:

<TextBox Text="{Binding SelectedParticipant.PhoneNumber, UpdateSourceTrigger=PropertyChanged, Converter={StaticResource StringToPhoneConverter}}" />

#1


9  

Try using Text="{Binding PhoneNumber, StringFormat={}{0:(###)###-####}}"

尝试使用Text =“{Binding PhoneNumber,StringFormat = {} {0:(###)### - ####}}”

Edit

编辑

If your PhoneNumber property is of type string, then there's not really a lot you can do with StringFormat to format it.

如果你的PhoneNumber属性是string类型,那么你可以用StringFormat来格式化它。

In the past when I've wanted to do something like this, I expose a property called FormattedPhoneNumber which returns the formatted phone number for display purposes, and the edit box just binds to plain old unformatted PhoneNumber

在过去,当我想做这样的事情时,我公开了一个名为FormattedPhoneNumber的属性,它返回格式化的电话号码以供显示,编辑框只是绑定到普通的未格式化的PhoneNumber

public string FormattedPhoneNumber
{
    get
    {
        if (PhoneNumber == null)
            return string.Empty;

        switch (PhoneNumber.Length)
        { 
            case 7:
                return Regex.Replace(PhoneNumber, @"(\d{3})(\d{4})", "$1-$2");
            case 10:
                return Regex.Replace(PhoneNumber, @"(\d{3})(\d{3})(\d{4})", "($1) $2-$3");
            case 11:
                return Regex.Replace(PhoneNumber, @"(\d{1})(\d{3})(\d{3})(\d{4})", "$1-$2-$3-$4");
            default:
                return PhoneNumber;
        }
    }
}

#2


2  

after a short google search i found this two links the second one is in german

经过短暂的谷歌搜索,我发现这两个链接第二个是德语

WPF – Masked Textbox Behavior http://marlongrech.wordpress.com/2007/10/28/masked-textbox/

WPF - 蒙面文本框行为http://marlongrech.wordpress.com/2007/10/28/masked-textbox/

Masked TextBox http://blindmeis.wordpress.com/2010/06/01/wpf-masked-textbox-behavior/

Masked TextBox http://blindmeis.wordpress.com/2010/06/01/wpf-masked-textbox-behavior/

hope this helps

希望这可以帮助

#3


0  

I would like to extend what already Rachel has responded. If a phone number is an integer, StringFormat would work just fine. In case a phone number is a string, I found Converter to be quite handy. This removes the need to create additional property for a class.

我想扩展Rachel已经做出的回应。如果电话号码是一个整数,StringFormat可以正常工作。如果电话号码是一个字符串,我发现转换器非常方便。这消除了为类创建附加属性的需要。

Here is an example:

这是一个例子:

public class StringToPhoneConverter : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
    {
        if (value == null)
            return string.Empty;

        //retrieve only numbers in case we are dealing with already formatted phone no
        string phoneNo = value.ToString().Replace("(", string.Empty).Replace(")", string.Empty).Replace(" ", string.Empty).Replace("-", string.Empty);

        switch (phoneNo.Length)
        {
            case 7:
                return Regex.Replace(phoneNo, @"(\d{3})(\d{4})", "$1-$2");
            case 10:
                return Regex.Replace(phoneNo, @"(\d{3})(\d{3})(\d{4})", "($1) $2-$3");
            case 11:
                return Regex.Replace(phoneNo, @"(\d{1})(\d{3})(\d{3})(\d{4})", "$1-$2-$3-$4");
            default:
                return phoneNo;
        }
    }

    public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
    {
        return value;
    }
}

xaml:

XAML:

<TextBox Text="{Binding SelectedParticipant.PhoneNumber, UpdateSourceTrigger=PropertyChanged, Converter={StaticResource StringToPhoneConverter}}" />