DateTimePicker binding overwrites time

WPF Studio, Themes, and Shared Library for WPF Forum

Posted 16 years ago by Brandon Simmons - Programmer, AssimilSoft
Version: 3.0.0410
Platform: .NET 3.0
Environment: Windows Vista (32-bit)
Avatar
Hi,

I'm using a DateTimePicker which is bound to an object that has a DateTime property. I've written my own time entry textbox to edit the time portion of the same DateTime property. The problem is when a new date is selected from the DateTimePicker the time portion of the DateTime is overwritten.

Here is a very simple example.
<Window x:Class="WpfApplication1.Window1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:shared="http://schemas.actiprosoftware.com/winfx/xaml/shared"
    xmlns:l="clr-namespace:WpfApplication1"
    Title="Window1" Height="300" Width="300">
    <Window.Resources>
        <l:BoundObject x:Key="boundObject"/>
    </Window.Resources>
    <Grid DataContext="{StaticResource boundObject}">
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="*"/>
            <ColumnDefinition Width="Auto"/>
            <ColumnDefinition Width=".5*"/>
            <ColumnDefinition Width="Auto"/>
            <ColumnDefinition Width=".5*"/>
        </Grid.ColumnDefinitions>
        <Grid.RowDefinitions>
            <RowDefinition Height="Auto"/>
            <RowDefinition Height="*"/>
        </Grid.RowDefinitions>
        <shared:DateTimePicker Grid.Column="0" Value="{Binding Path=DateTime}"/>
        <TextBlock Grid.Column="1" Text="Hour:"/>
        <TextBlock Grid.Column="2" Text="{Binding Path=DateTime.Hour}"/>
        <TextBlock Grid.Column="3" Text="Minute:"/>
        <TextBlock Grid.Column="4" Text="{Binding Path=DateTime.Minute}"/>
    </Grid>
</Window>
using System;
using System.ComponentModel;

namespace WpfApplication1
{
    public class BoundObject : INotifyPropertyChanged
    {
        private DateTime _dateTime;
        public BoundObject()
        {
            _dateTime = DateTime.Now;
        }

        public DateTime DateTime
        {
            get { return _dateTime; }
            set { _dateTime = value; NotifyPropertyChanged("DateTime"); }
        }

        #region INotifyPropertyChanged Members
        public event PropertyChangedEventHandler PropertyChanged;

        private void NotifyPropertyChanged(string propertyName)
        {
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
            }
        }
        #endregion
    }
}

Comments (5)

Posted 16 years ago by Actipro Software Support - Cleveland, OH, USA
Avatar
Hi Brandon,

Yes the code currently creates a new DateTime object from the text or from the value in the drop-down calendar. Is there a reason why you don't just use the mode where it displays the time too?

I suppose we could add something to maintain the time, but I would think this should be an option, right?


Actipro Software Support

Posted 16 years ago by Brandon Simmons - Programmer, AssimilSoft
Avatar
I'd expect it to be an option. But perhaps the mode that displays time will work.

I've looked in the docs and nothing is jumping out at me on how to change this. Could I get an example? Thanks!
Posted 16 years ago by Brandon Simmons - Programmer, AssimilSoft
Avatar
I just wanted to say that I did figure out how to display the time using the ValueConverterParameter property. However there is some functionality that my application needs that this does not provide.

The application is meant to make entry of date and time information as quickly as possible. So I have written a time entry textbox that takes input for hours and minutes only and automatically inserts the colon between. Also it has built in functionality for typing A or P and changing the time meridiem.

This is a fairly specialized use and I wouldn't expect your DateTimePicker to go to these lengths, but preserving the time of the previous entry would be nice. Maybe it only does so under certain DateTimeToStringPatterns and if a property has been set.

I think for now I'll have to have two separate DateTime properties to bind to and reconcile them when they are needed.
Posted 16 years ago by Actipro Software Support - Cleveland, OH, USA
Avatar
We do hope to improve the control in the future to allow for some improved entry of data like what you are talking about. And I will make a note on our TODO list to add an option for the time persistence.

In the meantime, your workaround sounds good.


Actipro Software Support

Posted 14 years ago by Actipro Software Support - Cleveland, OH, USA
Avatar
Fyi, this issue has been addressed by the DateTimeEditBox control in our Editors for WPF product.


Actipro Software Support

The latest build of this product (v24.1.1) was released 1 month ago, which was after the last post in this thread.

Add Comment

Please log in to a validated account to post comments.