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

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.
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
}
}