Bind DateTime to date and time EditFields

I am trying to create a gui that includes editing the value of a DateTime object. The DateTime property is bound to a DataPicker and a regular TextBox for Time. When I change the value in the TextBox of the time, the value in the DateTime property is Today with the time entered, and not just update the Time, keeping the original date.

How can I implement a Time TextBox that only changes the DateTime, but not the date?

Current Binding:

<TextBlock>Time</TextBlock>
<TextBox Template="{StaticResource RoundedTextBoxTemplate}">
    <Binding Path="Delivery.Date" StringFormat="HH:mm">
        <Binding.ValidationRules>
            <v:IsValidTimeRule />
        </Binding.ValidationRules>
    </Binding>
</TextBox>
+3
source share
2 answers

The only way I could think of is to have a DateTime property in which you allow the setter to 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:

 public partial class Page : UserControl
 {
      private TestClass m_testClass = new TestClass();

      public Page()
      {
           InitializeComponent();
           myTextBox.DataContext = m_testClass;
      }
  }

# TestClass, :

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) => {};
}

# 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;
    }
}
+4

sipWiz, Model

: 1. StartDay 2. StartTime, StartDate .

public DateTime? StartTime
{
    get
    {
        return StartDate;
    }
    set
    {
        if (StartDate == null) StartDate = DateTime.Today;
        StartDate = StartDate.Value.Date.Add(value.HasValue ? value.Value.TimeOfDay: TimeSpan.Zero);
    }
}
public DateTime? StartDay
{
    get { return StartDate; }
    set
    {
        DateTime BaseDate = value.HasValue ? value.Value : DateTime.MinValue;
        StartDate = BaseDate.Date.Add(StartDate.HasValue ? StartDate.Value.TimeOfDay : TimeSpan.Zero);
    }
}

DatePicker TextBox StringFormat = "HH: mm".

+2

Source: https://habr.com/ru/post/1705772/


All Articles