如何在xaml中将字符串属性格式化为电话号码格式,不使用值转换器。我还使用了数据注释[Phone]。 我尝试了这个方法,但它没有起作用。
<TextBlock Text="{Binding Person.Phone, StringFormat=\{0:(000)000\-0000\}}"/>
如何在xaml中将字符串属性格式化为电话号码格式,不使用值转换器。我还使用了数据注释[Phone]。 我尝试了这个方法,但它没有起作用。
<TextBlock Text="{Binding Person.Phone, StringFormat=\{0:(000)000\-0000\}}"/>
以下是我在第一次尝试时的做法:
public class ContactViewModel : INotifyPropertyChanged
{
private Contact _contact;
public ContactViewModel(Contact contact)
{
_contact = contact;
}
public String Phone
{
get
{
var builder = new StringBuilder(_contact.Phone);
builder.Insert(6, '-');
builder.Insert(3, ')');
builder.Insert(0, '(');
return builder.ToString();
}
}
}
Phone
属性,但是使用这个视图模型适配器层而不是直接绑定到模型对象。这是视图模型最突出的特点:将模型适应到视图。