Browse Source

working on TimePicker

pull/1645/head
brianlagunas_cp 15 years ago
parent
commit
cb6f9d1910
  1. 16
      ExtendedWPFToolkitSolution/Src/WPFToolkit.Extended/TimePicker/Implementation/TimeItem.cs
  2. 215
      ExtendedWPFToolkitSolution/Src/WPFToolkit.Extended/TimePicker/Implementation/TimePicker.cs
  3. 40
      ExtendedWPFToolkitSolution/Src/WPFToolkit.Extended/TimePicker/Themes/Generic.xaml
  4. 1
      ExtendedWPFToolkitSolution/Src/WPFToolkit.Extended/WPFToolkit.Extended.csproj

16
ExtendedWPFToolkitSolution/Src/WPFToolkit.Extended/TimePicker/Implementation/TimeItem.cs

@ -0,0 +1,16 @@
using System;
namespace Microsoft.Windows.Controls
{
public class TimeItem
{
public string Display { get; set; }
public TimeSpan Time { get; set; }
public TimeItem(string display, TimeSpan time)
{
Display = display;
Time = time;
}
}
}

215
ExtendedWPFToolkitSolution/Src/WPFToolkit.Extended/TimePicker/Implementation/TimePicker.cs

@ -1,34 +1,73 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Collections;
using System.Globalization;
namespace Microsoft.Windows.Controls
{
public class TimePicker : Control
{
internal static readonly TimeSpan EndTimeDefaultValue = new TimeSpan(23, 59, 0);
internal static readonly TimeSpan StartTimeDefaultValue = new TimeSpan(0, 0, 0);
internal static readonly TimeSpan TimeIntervalDefaultValue = new TimeSpan(1, 0, 0);
#region Members
ListBox _timeListBox;
private DateTimeFormatInfo DateTimeFormatInfo { get; set; }
internal static readonly TimeSpan EndTimeDefaultValue = new TimeSpan(23, 59, 0);
internal static readonly TimeSpan StartTimeDefaultValue = new TimeSpan(0, 0, 0);
internal static readonly TimeSpan TimeIntervalDefaultValue = new TimeSpan(1, 0, 0);
#endregion //Members
#region Properties
#region EndTime
public static readonly DependencyProperty EndTimeProperty = DependencyProperty.Register("EndTime", typeof(TimeSpan), typeof(TimePicker), new UIPropertyMetadata(EndTimeDefaultValue, new PropertyChangedCallback(OnEndTimeChanged), new CoerceValueCallback(OnCoerceEndTime)));
private static object OnCoerceEndTime(DependencyObject o, object value)
{
TimePicker timePicker = o as TimePicker;
if (timePicker != null)
return timePicker.OnCoerceEndTime((TimeSpan)value);
else
return value;
}
private static void OnEndTimeChanged(DependencyObject o, DependencyPropertyChangedEventArgs e)
{
TimePicker timePicker = o as TimePicker;
if (timePicker != null)
timePicker.OnEndTimeChanged((TimeSpan)e.OldValue, (TimeSpan)e.NewValue);
}
protected virtual TimeSpan OnCoerceEndTime(TimeSpan value)
{
// TODO: Keep the proposed value within the desired range.
return value;
}
protected virtual void OnEndTimeChanged(TimeSpan oldValue, TimeSpan newValue)
{
// TODO: Add your property changed side-effects. Descendants can override as well.
}
public TimeSpan EndTime
{
// IMPORTANT: To maintain parity between setting a property in XAML and procedural code, do not touch the getter and setter inside this dependency property!
get
{
return (TimeSpan)GetValue(EndTimeProperty);
}
set
{
SetValue(EndTimeProperty, value);
}
}
#endregion //EndTime
#region Format
public static readonly DependencyProperty FormatProperty = DependencyProperty.Register("Format", typeof(TimeFormat), typeof(TimePicker), new UIPropertyMetadata(TimeFormat.ShortTime, OnFormatChanged));
@ -99,56 +138,98 @@ namespace Microsoft.Windows.Controls
#endregion //Minimum
#region SelectedDate
#region StartTime
public static readonly DependencyProperty SelectedDateProperty = DependencyProperty.Register("SelectedDate", typeof(DateTime?), typeof(TimePicker), new UIPropertyMetadata(DateTime.Now, OnSelectedDateChanged));
public DateTime? SelectedDate
public static readonly DependencyProperty StartTimeProperty = DependencyProperty.Register("StartTime", typeof(TimeSpan), typeof(TimePicker), new UIPropertyMetadata(StartTimeDefaultValue, new PropertyChangedCallback(OnStartTimeChanged), new CoerceValueCallback(OnCoerceStartTime)));
private static object OnCoerceStartTime(DependencyObject o, object value)
{
get { return (DateTime?)GetValue(SelectedDateProperty); }
set { SetValue(SelectedDateProperty, value); }
TimePicker timePicker = o as TimePicker;
if (timePicker != null)
return timePicker.OnCoerceStartTime((TimeSpan)value);
else
return value;
}
private static void OnSelectedDateChanged(DependencyObject o, DependencyPropertyChangedEventArgs e)
private static void OnStartTimeChanged(DependencyObject o, DependencyPropertyChangedEventArgs e)
{
TimePicker timePicker = o as TimePicker;
if (timePicker != null)
timePicker.OnSelectedDateChanged((DateTime?)e.OldValue, (DateTime?)e.NewValue);
timePicker.OnStartTimeChanged((TimeSpan)e.OldValue, (TimeSpan)e.NewValue);
}
protected virtual void OnSelectedDateChanged(DateTime? oldValue, DateTime? newValue)
protected virtual TimeSpan OnCoerceStartTime(TimeSpan value)
{
// TODO: Keep the proposed value within the desired range.
return value;
}
protected virtual void OnStartTimeChanged(TimeSpan oldValue, TimeSpan newValue)
{
// TODO: Add your property changed side-effects. Descendants can override as well.
}
#endregion //SelectedDate
public TimeSpan StartTime
{
// IMPORTANT: To maintain parity between setting a property in XAML and procedural code, do not touch the getter and setter inside this dependency property!
get
{
return (TimeSpan)GetValue(StartTimeProperty);
}
set
{
SetValue(StartTimeProperty, value);
}
}
#region SelectedTime
#endregion //StartTime
public static readonly DependencyProperty SelectedTimeProperty = DependencyProperty.Register("SelectedTime", typeof(TimeSpan?), typeof(TimePicker), new UIPropertyMetadata(null, OnSelectedTimeChanged));
public TimeSpan? SelectedTime
#region TimeInterval
public static readonly DependencyProperty TimeIntervalProperty = DependencyProperty.Register("TimeInterval", typeof(TimeSpan), typeof(TimePicker), new UIPropertyMetadata(TimeIntervalDefaultValue, OnTimeIntervalChanged));
public TimeSpan TimeInterval
{
get { return (TimeSpan?)GetValue(SelectedTimeProperty); }
set { SetValue(SelectedTimeProperty, value); }
get { return (TimeSpan)GetValue(TimeIntervalProperty); }
set { SetValue(TimeIntervalProperty, value); }
}
private static void OnSelectedTimeChanged(DependencyObject o, DependencyPropertyChangedEventArgs e)
private static void OnTimeIntervalChanged(DependencyObject o, DependencyPropertyChangedEventArgs e)
{
TimePicker timePicker = o as TimePicker;
if (timePicker != null)
timePicker.OnSelectedTimeChanged((TimeSpan?)e.OldValue, (TimeSpan?)e.NewValue);
timePicker.OnTimeIntervalChanged((TimeSpan)e.OldValue, (TimeSpan)e.NewValue);
}
protected virtual void OnSelectedTimeChanged(TimeSpan? oldValue, TimeSpan? newValue)
protected virtual void OnTimeIntervalChanged(TimeSpan oldValue, TimeSpan newValue)
{
var current = DateTime.Now;
// TODO: Add your property changed side-effects. Descendants can override as well.
}
#endregion //TimeInterval
var date = SelectedDate ?? current;
var time = SelectedTime ?? current.TimeOfDay;
#region Value
SelectedDate = new DateTime(date.Year, date.Month, date.Day, time.Hours, time.Minutes, time.Seconds, time.Milliseconds);
public static readonly DependencyProperty ValueProperty = DependencyProperty.Register("Value", typeof(DateTime?), typeof(TimePicker), new UIPropertyMetadata(null, OnValueChanged));
public DateTime? Value
{
get { return (DateTime?)GetValue(ValueProperty); }
set { SetValue(ValueProperty, value); }
}
private static void OnValueChanged(DependencyObject o, DependencyPropertyChangedEventArgs e)
{
TimePicker timePicker = o as TimePicker;
if (timePicker != null)
timePicker.OnValueChanged((DateTime?)e.OldValue, (DateTime?)e.NewValue);
}
protected virtual void OnValueChanged(DateTime? oldValue, DateTime? newValue)
{
// TODO: Add your property changed side-effects. Descendants can override as well.
}
#endregion //SelectedTime
#endregion //Value
#endregion //Properties
@ -161,6 +242,7 @@ namespace Microsoft.Windows.Controls
public TimePicker()
{
DateTimeFormatInfo = DateTimeFormatInfo.GetInstance(CultureInfo.CurrentCulture);
Keyboard.AddKeyDownHandler(this, OnKeyDown);
Mouse.AddPreviewMouseDownOutsideCapturedElementHandler(this, OnMouseDownOutsideCapturedElement);
}
@ -174,25 +256,8 @@ namespace Microsoft.Windows.Controls
base.OnApplyTemplate();
_timeListBox = (ListBox)GetTemplateChild("PART_TimeListItems");
_timeListBox.ItemsSource = GenerateItemsSource();
_timeListBox.SelectionChanged += new SelectionChangedEventHandler(_timeListBox_SelectionChanged);
}
void _timeListBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
//if (e.AddedItems.Count > 0)
//{
// TimeSpan newTime = (TimeSpan)e.AddedItems[0];
// var current = DateTime.Now;
// var date = this.SelectedDate.HasValue ? this.SelectedDate.Value : current;
// var time = this.SelectedTime.HasValue ? this.SelectedTime.Value : current.TimeOfDay;
// SelectedDate = new DateTime(date.Year, date.Month, date.Day, time.Hours, time.Minutes, time.Seconds, time.Milliseconds);
//}
CloseTimePicker();
_timeListBox.ItemsSource = GenerateTimeListItemsSource();
_timeListBox.SelectionChanged += TimeListBox_SelectionChanged;
}
#endregion //Base Class Overrides
@ -217,6 +282,20 @@ namespace Microsoft.Windows.Controls
CloseTimePicker();
}
void TimeListBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
if (e.AddedItems.Count > 0)
{
TimeItem selectedTimeListItem = (TimeItem)e.AddedItems[0];
var time = selectedTimeListItem.Time; ;
var date = Value ?? DateTime.MinValue;
Value = new DateTime(date.Year, date.Month, date.Day, time.Hours, time.Minutes, time.Seconds, time.Milliseconds);
}
CloseTimePicker();
}
#endregion //Event Handlers
#region Methods
@ -228,13 +307,10 @@ namespace Microsoft.Windows.Controls
ReleaseMouseCapture();
}
private static IEnumerable GenerateItemsSource()
public IEnumerable GenerateTimeListItemsSource()
{
//TimeSpan time = this.StartTime;
//TimeSpan endTime = this.EndTime;
TimeSpan time = StartTimeDefaultValue;
TimeSpan endTime = EndTimeDefaultValue;
TimeSpan time = StartTime;
TimeSpan endTime = EndTime;
if (endTime <= time)
{
@ -242,19 +318,32 @@ namespace Microsoft.Windows.Controls
time = StartTimeDefaultValue;
}
//TimeSpan timeInterval = this.TimeInterval;
TimeSpan timeInterval = TimeIntervalDefaultValue;
TimeSpan timeInterval = TimeInterval;
if (time != null && endTime != null && timeInterval != null && timeInterval.Ticks > 0)
{
while (time <= endTime)
{
yield return time;
yield return new TimeItem(DateTime.MinValue.Add(time).ToString(GetTimeFormat(), CultureInfo.CurrentCulture), time);
time = time.Add(timeInterval);
}
yield break;
}
}
private string GetTimeFormat()
{
switch (Format)
{
case TimeFormat.Custom:
return FormatString;
case TimeFormat.LongTime:
return DateTimeFormatInfo.LongTimePattern;
case TimeFormat.ShortTime:
return DateTimeFormatInfo.ShortTimePattern;
default:
return DateTimeFormatInfo.ShortTimePattern;
}
}
#endregion //Methods

40
ExtendedWPFToolkitSolution/Src/WPFToolkit.Extended/TimePicker/Themes/Generic.xaml

@ -4,6 +4,10 @@
xmlns:chrome="clr-namespace:Microsoft.Windows.Controls.Chromes"
xmlns:coreConverters="clr-namespace:Microsoft.Windows.Controls.Core.Converters">
<!-- =============================================================================== -->
<!-- ResourceDictionary for TimePicker and related controls -->
<!-- =============================================================================== -->
<coreConverters:InverseBoolConverter x:Key="InverseBoolConverter" />
<coreConverters:TimeFormatToDateTimeFormatConverter x:Key="TimeFormatToDateTimeFormatConverter" />
@ -51,10 +55,33 @@
</Setter>
</Style>
<SolidColorBrush x:Key="ListItemHover" Color="#FFE7F5FD"/>
<Style x:Key="TimeItemStyle" TargetType="ListBoxItem">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="ListBoxItem">
<Border Name="Border" SnapsToDevicePixels="true">
<ContentPresenter Margin="4" />
</Border>
<ControlTemplate.Triggers>
<Trigger Property="IsMouseOver" Value="True">
<Setter TargetName="Border" Property="Background" Value="{StaticResource ListItemHover}" />
</Trigger>
<Trigger Property="IsSelected" Value="True">
<Setter TargetName="Border" Property="Background" Value="{DynamicResource {x:Static SystemColors.HighlightBrushKey}}" />
<Setter Property="Foreground" Value="White" />
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
<!-- =============================================================================== -->
<!-- TimePicker -->
<!-- =============================================================================== -->
<Style TargetType="{x:Type local:TimePicker}">
<Setter Property="Template">
<Setter.Value>
@ -69,7 +96,7 @@
<local:DateTimeUpDown x:Name="PART_TimeUpDown" BorderThickness="1,1,0,1"
Format="{TemplateBinding Format, Converter={StaticResource TimeFormatToDateTimeFormatConverter}}"
FormatString="{TemplateBinding FormatString}"
Value="{Binding SelectedDate, RelativeSource={RelativeSource TemplatedParent}}" />
Value="{Binding Value, RelativeSource={RelativeSource TemplatedParent}}" />
<ToggleButton x:Name="_timePickerToggleButton" Grid.Column="1"
IsChecked="{Binding IsOpen, RelativeSource={RelativeSource TemplatedParent}}"
Style="{StaticResource TimePickerToggleButtonStyle}"
@ -77,10 +104,11 @@
</Grid>
<Popup IsOpen="{Binding IsChecked, ElementName=_timePickerToggleButton}" StaysOpen="False">
<Border BorderThickness="1" Background="{StaticResource PopupBackgroundBrush}" BorderBrush="{StaticResource PopupDarkBorderBrush}" >
<ListBox x:Name="PART_TimeListItems" BorderThickness="0"
SelectedItem="{Binding SelectedTime, RelativeSource={RelativeSource TemplatedParent}}">
</ListBox>
<Grid>
<ListBox x:Name="PART_TimeListItems" BorderThickness="0" Width="150" Height="130"
DisplayMemberPath="Display"
ItemContainerStyle="{StaticResource TimeItemStyle}"/>
</Grid>
</Border>
</Popup>
</Grid>

1
ExtendedWPFToolkitSolution/Src/WPFToolkit.Extended/WPFToolkit.Extended.csproj

@ -222,6 +222,7 @@
<Compile Include="Core\Primitives\UpDownBase.cs" />
<Compile Include="SplitButton\Implementation\SplitButton.cs" />
<Compile Include="TimePicker\Implementation\TimeFormat.cs" />
<Compile Include="TimePicker\Implementation\TimeItem.cs" />
<Compile Include="TimePicker\Implementation\TimePicker.cs" />
<Compile Include="VisualStates.cs" />
<Compile Include="MessageBox\Implementation\VisualStates.MessageBox.cs" />

Loading…
Cancel
Save