WPF的日期怎样格式化?如:<TextBlock Text="{Binding Path=YYQX2}" />时显示为12/31/2006 12:00:00 AM
WPF的日期怎样格式化?
如TextBlock绑定了数据库的日期字段:
<TextBlock Text= "{Binding Path=YYQX2} " />
时显示为12/31/2006 12:00:00 AM,
我想显示为:2006-12-31
HOW? HELP!
------解决方案--------------------实现IValueConverter
namespace XXX
{
[ValueConversion(typeof(DateTime), typeof(string))]
public class DateConverter : IValueConverter
{
public void Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo info)
{
DateTime date = (DateTime)value;
return date.ToShortDateString();
}
public void ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo info)
{
return null; //如果只是显示,return null就可以了。
}
}
***.xaml里
<...
xmlns:src= "clrnamespace:XXX "
...>
<***.Resources>
<src:DateConverter x:Key= "myDateConverter " />
</***.Resources>
<TextBlock Text= "{Binding Path=***, Converter={StaticResource myDateConverter}} " />
不过郁闷的是vs2005会提示警告信息(在Expression Blend里OK)!
------解决方案--------------------你可以写成三个TextBlock.
<TextBlock Text= "{Binding Path=YYQX2.Year} " />
<TextBlock Text= "{Binding Path=YYQX2.Month} " />
<TextBlock Text= "{Binding Path=YYQX2.Day} " />