转换器 - >将日期时间转换为HH:mm:ss格式
问题描述:
我试图显示hh:mm:ss格式,但我总是得到显示的完整日期。转换器 - >将日期时间转换为HH:mm:ss格式
从我的转换器类/依赖属性中是否有错误/丢失? 我只收到Datetime.now显示,如何将其更改为00:00:00?
我真正想要的是显示00:00:00作为初始化。之后它会增加低谷定时器。
XAML:
<ListBox.ItemTemplate>
<DataTemplate>
DownTime="{Binding DownTime, Converter={StaticResource DateTimeConverter},
ConverterParameter=\{0:hh:mm:ss\}}
</DataTemplate>
</ListBox.ItemTemplate>
依赖属性:
public static readonly DependencyProperty DownTimeProperty =
DependencyProperty.Register("DownTime", typeof(DateTime), typeof(RegistrationButton), new UIPropertyMetadata(DateTime.Now));
public DateTime DownTime
{
get { return (DateTime)GetValue(DownTimestandProperty); }
set { SetValue(DownTimeProperty, value); }
}
转换器类:
[ValueConversion(typeof(DateTime), typeof(String))]
public class DateTimeConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
DateTime dt = (DateTime)value;
return dt;
}
// No need to implement converting back on a one-way binding
public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
string strValue = value as string;
DateTime resultDateTime;
if (DateTime.TryParse(strValue, out resultDateTime))
{
return resultDateTime;
}
return DependencyProperty.UnsetValue;
}
}
谢谢秒。
哪个xaml元素实际上包含日期时间绑定? – 2012-04-19 14:48:50
ListBoxItemTemplate ==> DataTemplate – Jackz 2012-04-19 14:50:38
我不是这个意思。我的意思是类似' ' –
2012-04-19 14:51:56