如何在 xaml 中不使用值转换器将字符串属性格式化为电话号码格式。我也在用 数据注释[电话]。 我试过了,但没有用。
<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();
}
}
}
然后在 XAML 中,我只绑定(bind)到 Phone
属性,但使用这个 View 模型适配器层而不是直接绑定(bind)到模型对象上。这是 View 模型最突出的特性:使模型适应 View 。
https://stackoverflow.com/questions/27694319/