1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465 |
- using System;
- using System.ComponentModel;
- using System.Globalization;
- using System.Windows;
- using System.Windows.Controls;
- namespace CustomControls
- {
- [DesignTimeVisible(false)]
- public partial class DateTimePicker : DatePicker, INotifyPropertyChanged
- {
- public string CustomFormat
- {
- get { return (string)GetValue(CustomFormatProperty); }
- set { SetValue(CustomFormatProperty, value); }
- }
- public readonly static DependencyProperty CustomFormatProperty = DependencyProperty.Register(
- "CustomFormat", typeof(string), typeof(DateTimePicker), new UIPropertyMetadata(""));
- public event PropertyChangedEventHandler PropertyChanged;
- private DateTime? _value;
- internal DateTime? Value
- {
- get => SelectedDate;
- set
- {
- _value = value;
- SelectedDate = value;
- }
- }
- public string FormattedValue
- {
- get
- {
- if (SelectedDate == null)
- return null;
- DateTime dateTime = SelectedDate.Value;
- if (_value != null)
- {
- DateTime timePart = _value.Value;
- dateTime = new DateTime(dateTime.Year, dateTime.Month, dateTime.Day, timePart.Hour, timePart.Minute, timePart.Second, timePart.Millisecond);
- }
- return dateTime.ToString(CustomFormat);
- }
- set => Value = DateTime.ParseExact(value, CustomFormat, CultureInfo.CurrentCulture);
- }
- protected override void OnPropertyChanged(DependencyPropertyChangedEventArgs e)
- {
- base.OnPropertyChanged(e);
- if (e.Property.Name == "SelectedDate" || e.Property.Name == "CustomFormat")
- {
- PropertyChanged?.Invoke(this, new PropertyChangedEventArgs("FormattedValue"));
- }
- }
- public DateTimePicker()
- {
- InitializeComponent();
- Padding = new Thickness(2, 2, 0, 3);
- }
- }
- }
|