Mar*_*ark 9 xaml windows-phone-7
有没有办法使用XAML为Windows Phone 7格式化日期?
如果尝试使用:
<TextBlock Text="{Binding Date, StringFormat={}{0:MM/dd/yyyy}}" />
Run Code Online (Sandbox Code Playgroud)
但我得到错误:
在'Binding'类型中找不到属性'StringFormat'
Aar*_*ver 20
在SL4内这是可能的......
<TextBlock Text="{Binding Date, StringFormat='MM/dd/yyyy'}}"/>
Run Code Online (Sandbox Code Playgroud)
...在SL3中你需要使用IValueConverter.
public class DateTimeToStringConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
return String.Format("{0:MM/dd/yyyy}", (DateTime)value);
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
throw new NotImplementedException();
}
}
Run Code Online (Sandbox Code Playgroud)
如果你想要一个更强大的方法,你可以使用ConverterParameter.
public class DateTimeToStringConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
if (parameter == null)
return ((DateTime)value).ToString(culture);
else
return ((DateTime)value).ToString(parameter as string, culture);
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
throw new NotImplementedException();
}
}
Run Code Online (Sandbox Code Playgroud)
然后在您的XAML中,您首先将转换器定义为资源...
<namespace:DateTimeToStringConverter x:Key="MyDateTimeToStringConverter"/>
Run Code Online (Sandbox Code Playgroud)
..然后引用它以及用于格式化DateTime值的可接受参数...
<TextBlock Text="{Binding Date,
Converter={StaticResource MyDateTimeToStringConverter},
ConverterParameter=\{0:M\}}"/>
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
13044 次 |
| 最近记录: |