The only way I could think of to do it is to have a DateTime property where you only allow the setter change the time.
XAML:
<UserControl.Resources>
<mine:DateTimeConverter x:Key="MyDateTimeConverter" />
</UserControl.Resources>
<Grid x:Name="LayoutRoot">
<TextBox x:Name="myTextBox" Text="{Binding Path=TestDateTime, Converter={StaticResource MyDateTimeConverter}, Mode=TwoWay}" />
</Grid>
C# Code behind:
public partial class Page : UserControl
{
private TestClass m_testClass = new TestClass();
public Page()
{
InitializeComponent();
myTextBox.DataContext = m_testClass;
}
}
C# TestClass where the property setter restriction is used:
public class TestClass : INotifyPropertyChanged
{
private DateTime m_testDateTime = DateTime.Now;
public DateTime TestDateTime
{
get { return m_testDateTime; }
set
{
m_testDateTime = m_testDateTime.Date.Add(value.TimeOfDay);
PropertyChanged(this, new PropertyChangedEventArgs("TestDateTime"));
}
}
public event PropertyChangedEventHandler PropertyChanged = (t, e) => {};
}
C# IValueConverter:
public class DateTimeConverter : IValueConverter
{
public object Convert(object value,
Type targetType,
object parameter,
CultureInfo culture)
{
DateTime date = (DateTime)value;
return date.ToString("HH:mm");
}
public object ConvertBack(object value,
Type targetType,
object parameter,
CultureInfo culture)
{
string strValue = value.ToString();
DateTime resultDateTime;
if (DateTime.TryParse(strValue, out resultDateTime))
{
return resultDateTime;
}
return value;
}
}