From b4bf83e49fdf047e200c0f9a1dafbcd29dbf9924 Mon Sep 17 00:00:00 2001 From: jogibear9988 Date: Sat, 9 Apr 2016 11:58:57 +0200 Subject: [PATCH] Reimplement TimeSpanEditor, fixes #8 --- SimpleSample/TestControl.cs | 10 ++ .../PropertyGrid/Editors/TimeSpanEditor.xaml | 81 ++++++++-- .../Editors/TimeSpanEditor.xaml.cs | 143 +++++++++++++++++- 3 files changed, 217 insertions(+), 17 deletions(-) diff --git a/SimpleSample/TestControl.cs b/SimpleSample/TestControl.cs index 51d5147..edb67ec 100644 --- a/SimpleSample/TestControl.cs +++ b/SimpleSample/TestControl.cs @@ -26,6 +26,16 @@ namespace SimpleSample public static readonly DependencyProperty TestTimeSpanProperty = DependencyProperty.Register("TestTimeSpan", typeof(TimeSpan), typeof(TestControl), new PropertyMetadata(new TimeSpan(4, 3, 55, 34, 345))); + public TimeSpan TestTimeSpan2 + { + get { return (TimeSpan)GetValue(TestTimeSpan2Property); } + set { SetValue(TestTimeSpan2Property, value); } + } + + // Using a DependencyProperty as the backing store for TestTimeSpan2. This enables animation, styling, binding, etc... + public static readonly DependencyProperty TestTimeSpan2Property = + DependencyProperty.Register("TestTimeSpan2", typeof(TimeSpan), typeof(TestControl), new PropertyMetadata(TimeSpan.FromMinutes(-55))); + } } diff --git a/WpfDesign.Designer/Project/PropertyGrid/Editors/TimeSpanEditor.xaml b/WpfDesign.Designer/Project/PropertyGrid/Editors/TimeSpanEditor.xaml index 028904d..1f0ce2a 100644 --- a/WpfDesign.Designer/Project/PropertyGrid/Editors/TimeSpanEditor.xaml +++ b/WpfDesign.Designer/Project/PropertyGrid/Editors/TimeSpanEditor.xaml @@ -2,18 +2,75 @@ xmlns="http://schemas.microsoft.com/netfx/2007/xaml/presentation" xmlns:widgets="clr-namespace:ICSharpCode.WpfDesign.Designer.Controls" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" - xmlns:xctk="http://schemas.xceed.com/wpf/xaml/toolkit" - DataContext="{Binding RelativeSource={RelativeSource Self}}"> - - - - - - - - - - + x:Name="root" + > + + + + + + + + + + + + + + + + + + diff --git a/WpfDesign.Designer/Project/PropertyGrid/Editors/TimeSpanEditor.xaml.cs b/WpfDesign.Designer/Project/PropertyGrid/Editors/TimeSpanEditor.xaml.cs index 5fae21f..f253898 100644 --- a/WpfDesign.Designer/Project/PropertyGrid/Editors/TimeSpanEditor.xaml.cs +++ b/WpfDesign.Designer/Project/PropertyGrid/Editors/TimeSpanEditor.xaml.cs @@ -15,6 +15,7 @@ namespace ICSharpCode.WpfDesign.Designer.PropertyGrid.Editors public TimeSpanEditor() { SpecialInitializeComponent(); + DataContextChanged += new DependencyPropertyChangedEventHandler(NumberEditor_DataContextChanged); } /// @@ -32,6 +33,67 @@ namespace ICSharpCode.WpfDesign.Designer.PropertyGrid.Editors this.InitializeComponent(); } + public PropertyNode PropertyNode + { + get { return DataContext as PropertyNode; } + } + + void NumberEditor_DataContextChanged(object sender, DependencyPropertyChangedEventArgs e) + { + if (PropertyNode == null) + return; + + var value = (TimeSpan) PropertyNode.Value; + + + if (value < TimeSpan.Zero) + { + this.Neagtive = true; + value = value.Negate(); + } + this.Days = value.Days; + this.Hours = value.Hours; + this.Minutes = value.Minutes; + this.Seconds = value.Seconds; + this.MiliSeconds = value.Milliseconds; + } + + private void UpdateValue() + { + var ts = new TimeSpan(this.Days, this.Hours, this.Minutes, this.Seconds, this.MiliSeconds); + if (this.Neagtive) + ts = ts.Negate(); + PropertyNode.Value = ts; + } + + public bool Neagtive + { + get { return (bool)GetValue(NeagtiveProperty); } + set { SetValue(NeagtiveProperty, value); } + } + + // Using a DependencyProperty as the backing store for Neagtive. This enables animation, styling, binding, etc... + public static readonly DependencyProperty NeagtiveProperty = + DependencyProperty.Register("Neagtive", typeof(bool), typeof(TimeSpanEditor), new PropertyMetadata(false)); + + private static void OnNeagtivePropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) + { + var ctl = (TimeSpanEditor)d; + if (ctl.Hours > 23) + { + ctl.Days++; + ctl.Hours = 0; + } + else if (ctl.Hours < 0) + { + ctl.Days--; + ctl.Hours = 23; + } + + ctl.UpdateValue(); + } + + public int Days { get { return (int)GetValue(DaysProperty); } @@ -40,7 +102,14 @@ namespace ICSharpCode.WpfDesign.Designer.PropertyGrid.Editors // Using a DependencyProperty as the backing store for Days. This enables animation, styling, binding, etc... public static readonly DependencyProperty DaysProperty = - DependencyProperty.Register("Days", typeof(int), typeof(TimeSpanEditor), new PropertyMetadata()); + DependencyProperty.Register("Days", typeof(int), typeof(TimeSpanEditor), new PropertyMetadata(OnDaysPropertyChanged)); + + private static void OnDaysPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) + { + var ctl = (TimeSpanEditor)d; + + ctl.UpdateValue(); + } public int Hours { @@ -50,7 +119,24 @@ namespace ICSharpCode.WpfDesign.Designer.PropertyGrid.Editors // Using a DependencyProperty as the backing store for Hours. This enables animation, styling, binding, etc... public static readonly DependencyProperty HoursProperty = - DependencyProperty.Register("Hours", typeof(int), typeof(TimeSpanEditor), new PropertyMetadata()); + DependencyProperty.Register("Hours", typeof(int), typeof(TimeSpanEditor), new PropertyMetadata(OnHoursPropertyChanged)); + + private static void OnHoursPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) + { + var ctl = (TimeSpanEditor) d; + if (ctl.Hours > 23) + { + ctl.Days++; + ctl.Hours = 0; + } + else if (ctl.Hours < 0) + { + ctl.Days--; + ctl.Hours = 23; + } + + ctl.UpdateValue(); + } public int Minutes { @@ -60,9 +146,25 @@ namespace ICSharpCode.WpfDesign.Designer.PropertyGrid.Editors // Using a DependencyProperty as the backing store for Minutes. This enables animation, styling, binding, etc... public static readonly DependencyProperty MinutesProperty = - DependencyProperty.Register("Minutes", typeof(int), typeof(TimeSpanEditor), new PropertyMetadata()); + DependencyProperty.Register("Minutes", typeof(int), typeof(TimeSpanEditor), new PropertyMetadata(OnMinutesPropertyChanged)); + private static void OnMinutesPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) + { + var ctl = (TimeSpanEditor)d; + if (ctl.Minutes > 59) + { + ctl.Hours++; + ctl.Minutes = 0; + } + else if (ctl.Minutes < 0) + { + ctl.Hours--; + ctl.Minutes = 59; + } + ctl.UpdateValue(); + } + public int Seconds { get { return (int)GetValue(SecondsProperty); } @@ -71,8 +173,24 @@ namespace ICSharpCode.WpfDesign.Designer.PropertyGrid.Editors // Using a DependencyProperty as the backing store for Seconds. This enables animation, styling, binding, etc... public static readonly DependencyProperty SecondsProperty = - DependencyProperty.Register("Seconds", typeof(int), typeof(TimeSpanEditor), new PropertyMetadata()); + DependencyProperty.Register("Seconds", typeof(int), typeof(TimeSpanEditor), new PropertyMetadata(OnSecondsPropertyChanged)); + private static void OnSecondsPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) + { + var ctl = (TimeSpanEditor)d; + if (ctl.Seconds > 59) + { + ctl.Minutes++; + ctl.Seconds = 0; + } + else if (ctl.Seconds < 0) + { + ctl.Minutes--; + ctl.Seconds = 59; + } + + ctl.UpdateValue(); + } public int MiliSeconds { get { return (int)GetValue(MiliSecondsProperty); } @@ -81,8 +199,23 @@ namespace ICSharpCode.WpfDesign.Designer.PropertyGrid.Editors // Using a DependencyProperty as the backing store for MiliSeconds. This enables animation, styling, binding, etc... public static readonly DependencyProperty MiliSecondsProperty = - DependencyProperty.Register("MiliSeconds", typeof(int), typeof(TimeSpanEditor), new PropertyMetadata()); + DependencyProperty.Register("MiliSeconds", typeof(int), typeof(TimeSpanEditor), new PropertyMetadata(OnMiliSecondsPropertyChanged)); + private static void OnMiliSecondsPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) + { + var ctl = (TimeSpanEditor)d; + if (ctl.MiliSeconds > 999) + { + ctl.Seconds++; + ctl.MiliSeconds = 0; + } + else if (ctl.MiliSeconds < 0) + { + ctl.Seconds--; + ctl.MiliSeconds = 999; + } + ctl.UpdateValue(); + } } } \ No newline at end of file