Click or drag to resize

DateTimeSource Class

Provides values for date and time suitable for bindings.
Inheritance Hierarchy
SystemObject
  TomsToolbox.WpfDateTimeSource

Namespace: TomsToolbox.Wpf
Assembly: TomsToolbox.Wpf (in TomsToolbox.Wpf.dll) Version: 2.21.0+44d18b541fc9419ec3c549350a832394661b2a4d
XMLNS for XAML: Not mapped to an xmlns.
Syntax
public class DateTimeSource : INotifyPropertyChanged

The DateTimeSource type exposes the following members.

Constructors
 NameDescription
Public methodDateTimeSource Initializes a new instance of the DateTimeSource class.
Top
Properties
 NameDescription
Public propertyNow Gets a DateTime object that is set to the current date and time on this computer, expressed as the local time.
Public propertyToday Gets the current date.
Public propertyUpdateInterval Gets or sets the interval in which the PropertyChanged event is raised for all properties.
Public propertyUtcNow Gets a DateTime object that is set to the current date and time on this computer, expressed as the Coordinated Universal Time (UTC).
Top
Methods
 NameDescription
Protected methodFinalize Finalizes an instance of the DateTimeSource class.
(Overrides ObjectFinalize)
Top
Events
 NameDescription
Public eventPropertyChangedOccurs when a property value changes.
Top
Fields
 NameDescription
Public fieldStatic memberDefault The default singleton object. Use this as a source for binding that supports manual updating.
Top
Extension Methods
 NameDescription
Public Extension MethodSafeCastT Performs a cast from object to T, avoiding possible null violations if T is a value type.
(Defined by ObjectExtensions)
Top
Remarks
This expression in XAML would be static, since the Source is never updated and would always have it's initial value, UpdateTarget won't have any effect.

MyDayOfWeek="{Binding Path=DayOfWeek, Source={x:Static system:DateTime.Today}}"

Using DateTimeSource instead, UpdateTarget will work, and MyDayOfWeek will be updated with the actual value:

MyDayOfWeek="{Binding Path=Today.DayOfWeek, Source={x:Static toms:DateTimeSource.Default}}"

Another usage is to use a local instance with timer triggered property change events:

XAML
<FrameworkElement.Resources>
  <toms:DateTimeSource x:Key="TimeSource" UpdateInterval="00:00:01" />
</FrameworkElement.Resources>
...
<TextBox Text=Binding Path=Now, Source=StaticResource TimeSource}}&quot; /&gt;
See Also