如何在DataGrid中获取电话号码格式 [英] How to get phone number format in DataGrid
本文介绍了如何在DataGrid中获取电话号码格式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
大家好。
在我的数据网格中,我尝试使用stringformat格式化电话号码:
In my datagrid I try to use stringformat to format phone number:
<DataGridTextColumn Header="Phone" Binding="{Binding Path=Phone, StringFormat={}{0:(###) ###-####}}" Width="100" />
但无法获得结果。哪里出错?
But cannot get result. Where is mistake?
谢谢。
推荐答案
您好zleug,
Hi zleug,
如果您的电话号码属性是字符串类型,我认为StringFormat不起作用,您可以尝试使用IvalueConverter。
If your Phone number property is of type string, I think the StringFormat is not working, you can try to use IvalueConverter.
<Window.Resources>
<converter:PhoneConverter x:Key="phoneconverter" />
</Window.Resources>
<Grid>
<DataGrid AutoGenerateColumns="False" ItemsSource="{Binding phones}">
<DataGrid.Columns>
<DataGridTextColumn
Width="200"
Binding="{Binding phonen, Converter={StaticResource phoneconverter}}"
Header="Phone number" />
</DataGrid.Columns>
</DataGrid>
</Grid>
public partial class Window26 : Window
{
public ObservableCollection<PhoneModel> phones { get; set; }
public Window26()
{
InitializeComponent();
phones = new ObservableCollection<PhoneModel>()
{
new PhoneModel(){phonen="1231231234"},
new PhoneModel(){phonen="1231231234"}
};
this.DataContext = this;
}
}
public class PhoneModel
{
public string phonen { get; set; }
}
class PhoneConverter:IValueConverter
{
public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
double a = System.Convert.ToInt64(value);
return a.ToString("(###) ###-####");
}
public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
throw new NotImplementedException();
}
}
最好的问候,
Best Regards,
Cherry
这篇关于如何在DataGrid中获取电话号码格式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文