[ValueConversion(typeof(DateTime), typeof(string))]
class DateTimeConverter1 : Freezable, IValueConverter
{
// Convertは問題なし。
public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
// DateTime型 -> HH:ss (時刻だけを表示する)
var d = System.Convert.ToDateTime(value);
return d.ToString("HH:ss");
}
public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
// valueにはテキストボックスに入力したstring型の時刻(HH:mm)が入っている。
DateTime time = System.Convert.ToDateTime(value.ToString());
DateTime date = Date; // HogeDateの値
// return ~
}
public DateTime Date
{
get { return (DateTime)GetValue(DayProperty); }
set { SetValue(DayProperty, value); }
}
public static readonly DependencyProperty DayProperty =
DependencyProperty.Register("Date", typeof(DateTime), typeof(DateTimeConverter1));
protected override Freezable CreateInstanceCore()
{
return new DateTimeConverter1();
}
}
<Window.Resources>
<v:DateTimeConverter1 x:Key="converter1" Date="{Binding HogeDate}" />
</Window.Resources>
<TextBox Text="{Binding HogeDate,Converter={StaticResource converter1}}"/>
ICAgIFtWYWx1ZUNvbnZlcnNpb24odHlwZW9mKERhdGVUaW1lKSwgdHlwZW9mKHN0cmluZykpXQogICAgY2xhc3MgRGF0ZVRpbWVDb252ZXJ0ZXIxIDogRnJlZXphYmxlLCBJVmFsdWVDb252ZXJ0ZXIKICAgIHsKICAgICAgICAvLyBDb252ZXJ044Gv5ZWP6aGM44Gq44GX44CCCiAgICAgICAgcHVibGljIG9iamVjdCBDb252ZXJ0KG9iamVjdCB2YWx1ZSwgVHlwZSB0YXJnZXRUeXBlLCBvYmplY3QgcGFyYW1ldGVyLCBTeXN0ZW0uR2xvYmFsaXphdGlvbi5DdWx0dXJlSW5mbyBjdWx0dXJlKQogICAgICAgIHsKICAgICAgICAgICAgLy8gRGF0ZVRpbWXlnosgLT4gSEg6c3MgKOaZguWIu+OBoOOBkeOCkuihqOekuuOBmeOCiykKICAgICAgICAgICAgdmFyIGQgPSBTeXN0ZW0uQ29udmVydC5Ub0RhdGVUa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