Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>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.</p> <p>XAML:</p> <pre><code>&lt;UserControl.Resources&gt; &lt;mine:DateTimeConverter x:Key="MyDateTimeConverter" /&gt; &lt;/UserControl.Resources&gt; &lt;Grid x:Name="LayoutRoot"&gt; &lt;TextBox x:Name="myTextBox" Text="{Binding Path=TestDateTime, Converter={StaticResource MyDateTimeConverter}, Mode=TwoWay}" /&gt; &lt;/Grid&gt; </code></pre> <p>C# Code behind:</p> <pre><code> public partial class Page : UserControl { private TestClass m_testClass = new TestClass(); public Page() { InitializeComponent(); myTextBox.DataContext = m_testClass; } } </code></pre> <p>C# TestClass where the property setter restriction is used:</p> <pre><code>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) =&gt; {}; } </code></pre> <p>C# IValueConverter:</p> <pre><code>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; } } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload