1

我有这个:

<TextBox x:Name="txbPhone" Text="+420" 
         InputScope="TelephoneNumber" Margin="9,0,9,0"/>

现在我想让用户写下他的电话号码,比如: +420123456789我想在 TextBox 中显示它,就像+420 123 456 789 我想在用户更改 TextBox 的值时通过在代码中手动添加空格来创建它一样。然后当我使用它时,我只需删除字符串中的所有空格。但我认为这是一个“肮脏”的解决方案。有没有办法为此设置一些模板?谢谢

编辑: 我创建转换器作为评论中的提及Frederik Winstrup Johansen。所以我创建了我的类(它需要向 ConvertBack 方法添加代码):

public class PhoneNumberConverter : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        var str = (string)value;
        string result;
        if (str[0] == '+')
            result = String.Format("{0:+### ### ### ###}", System.Convert.ToInt64(value));
        else
            result = String.Format("{0:### ### ### ###}", (Int64)value);
        return result;
    }

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

并将我的 TextBox 更改为:

<TextBox x:Name="txbPhone" Text="{Binding Path=Phone, Converter={StaticResource phoneNumberConverter}, Mode=TwoWay}" 
                 InputScope="TelephoneNumber" Margin="9,0,9,0" TextChanged="txbPhone_TextChanged"/>

这是我的绑定:

        viewModel = new PhonePageViewModel();
        viewModel.Phone = "+420123456";
        this.DataContext = viewModel;

转换器正在工作,但当我添加一些数字时它不会改变文本。不再调用 Convert 方法。我怎样才能做到这一点?每次我在 TextBox 调用转换器中更改字母时?

我的视图模型类:

namespace SpeedCarsWP.ViewModels
{
    public class PhonePageViewModel : INotifyPropertyChanged
    {
        private string phone;
        public event PropertyChangedEventHandler PropertyChanged;

        public string Phone
        {
            get { return phone; }
            set
            {
                phone = value;
                OnPropertyChanged("Phone");
            }
        }

        public PhonePageViewModel()
        {
        }

        protected void OnPropertyChanged(string name)
        {
            PropertyChangedEventHandler handler = PropertyChanged;
            if (handler != null)
            {
                handler(this, new PropertyChangedEventArgs(name));
            }
        }


    }
}
4

2 回答 2

1

如果您想使用带有数据绑定 + 转换器的解决方案,请实现它,它将为您工作。(我试过了,它奏效了。)

首先,在 View the XAML-Part 中:

    <TextBox Height="23" HorizontalAlignment="Left" Margin="78,60,0,0" Name="textBox1" VerticalAlignment="Top" Width="120" 
             Text="{Binding MyProperty, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged, Converter={StaticResource MyConverter}}" 
             TextChanged="textBox1_TextChanged" />

UpdateSourceTrigger代表逐个字符地格式化文本。

、ViewModel-Part:

通过 VM 中的 MyProperty,您需要使用NotifyPropertyChanged来触发更新。(如果你不这样做,转换器的 Convert-Method 将只运行一次,仅在第一次运行。)

、转换器部分:

您可以实现一个转换器主体,如:

        if (value != null)
        {
            string str = value.ToString();
            string result = str;

            if (str.Length == 1)
            {
                result = str.Insert(0, "+");
            }
            if (str.Length == 5 || (str.Length > 5 && (str.Length -1) % 4 == 0))
            {
                result = str.Insert(str.Length-1, " ");
            }

            return result;
        }
        return value;

如果这样做,文本框光标将不在文本末尾,因此您需要从 View 的代码隐藏中更正它。

    private void textBox1_TextChanged(object sender, TextChangedEventArgs e)
    {
        textBox1.Select(textBox1.Text.Length, 0);
    }

这只是使用 Converter 解决方案中的一种快速实现思维,但我希望它可以帮助您。

于 2013-02-05T15:09:00.633 回答
0

转换器正在工作,但当我添加一些数字时它不会改变文本。不再调用 Convert 方法。我怎样才能做到这一点?每次我在 TextBox 调用转换器中更改字母时?

转换器应自动更新。

你确定你PhonePageViewModel实现了INotifyPropertyChanged吗?您是否在setter中
引发PropertyChanged事件?PhonePageViewModel.Phone

你有绑定错误吗?(调试时检查 Visual Studio 中的输出窗格)。

于 2013-02-05T14:14:31.447 回答