我正在尝试显示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;
}
}
感谢。