Skip to content

Commit 67081ab

Browse files
committed
update in TimePicker
update in TimePicker
1 parent 2869db4 commit 67081ab

File tree

7 files changed

+45
-29
lines changed

7 files changed

+45
-29
lines changed

src/WPFDevelopers.Net40/Themes/Theme.xaml

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5305,23 +5305,23 @@
53055305
<Border x:Name="PART_EditBar" Panel.ZIndex="99" Background="{DynamicResource WD.BackgroundSolidColorBrush}" Effect="{StaticResource WD.PopupShadowDepth}" Visibility="Hidden">
53065306
<WrapPanel Margin="10,5" VerticalAlignment="Center">
53075307
<RadioButton x:Name="PART_RadioButtonRectangle" Margin="4,0" Style="{DynamicResource WD.PathRadioButton}" ToolTip="方框">
5308-
<controls:PathIcon Width="18" Height="18" Foreground="{StaticResource WD.RegularTextSolidColorBrush}" Kind="Rectangle" />
5308+
<controls:PathIcon Width="18" Height="18" Foreground="{DynamicResource WD.RegularTextSolidColorBrush}" Kind="Rectangle" />
53095309
</RadioButton>
53105310
<RadioButton x:Name="PART_RadioButtonEllipse" Margin="4,0" Style="{DynamicResource WD.PathRadioButton}" ToolTip="椭圆">
53115311
<Ellipse Width="19" Height="19" SnapsToDevicePixels="True" Stroke="{DynamicResource WD.RegularTextSolidColorBrush}" StrokeThickness="1.5" UseLayoutRounding="True" />
53125312
</RadioButton>
53135313
<RadioButton x:Name="PART_RadioButtonArrow" Margin="4,0" Style="{DynamicResource WD.PathRadioButton}" ToolTip="箭头">
5314-
<controls:PathIcon Width="18" Height="18" Foreground="{StaticResource WD.RegularTextSolidColorBrush}" Kind="ArrowRightTop" />
5314+
<controls:PathIcon Width="18" Height="18" Foreground="{DynamicResource WD.RegularTextSolidColorBrush}" Kind="ArrowRightTop" />
53155315
</RadioButton>
53165316
<RadioButton x:Name="PART_RadioButtonInk" Margin="4,0" Style="{DynamicResource WD.PathRadioButton}" ToolTip="画笔">
5317-
<controls:PathIcon Width="18" Height="18" Foreground="{StaticResource WD.RegularTextSolidColorBrush}" Kind="Ink" />
5317+
<controls:PathIcon Width="18" Height="18" Foreground="{DynamicResource WD.RegularTextSolidColorBrush}" Kind="Ink" />
53185318
</RadioButton>
53195319
<RadioButton x:Name="PART_RadioButtonText" Margin="4,0" Style="{DynamicResource WD.PathRadioButton}" ToolTip="文字">
5320-
<controls:PathIcon Width="18" Height="18" Foreground="{StaticResource WD.RegularTextSolidColorBrush}" Kind="Text" />
5320+
<controls:PathIcon Width="18" Height="18" Foreground="{DynamicResource WD.RegularTextSolidColorBrush}" Kind="Text" />
53215321
</RadioButton>
53225322
<Rectangle Width="1" Height="18" Margin="4,0" Stroke="{DynamicResource WD.SecondaryTextSolidColorBrush}" />
53235323
<Button x:Name="PART_ButtonSave" Margin="4,0" Style="{DynamicResource WD.PathButton}" ToolTip="保存">
5324-
<controls:PathIcon Width="18" Height="18" Foreground="{StaticResource WD.RegularTextSolidColorBrush}" Kind="Save" />
5324+
<controls:PathIcon Width="18" Height="18" Foreground="{DynamicResource WD.RegularTextSolidColorBrush}" Kind="Save" />
53255325
</Button>
53265326
<Button x:Name="PART_ButtonCancel" Margin="4,0" Style="{DynamicResource WD.PathButton}" ToolTip="取消">
53275327
<controls:PathIcon Width="14" Height="14" Foreground="{StaticResource WD.DangerSolidColorBrush}" Kind="Cancel" />
@@ -6491,6 +6491,7 @@
64916491
<Setter Property="ScrollViewer.HorizontalScrollBarVisibility" Value="Hidden" />
64926492
<Setter Property="ScrollViewer.VerticalScrollBarVisibility" Value="Hidden" />
64936493
<Setter Property="BorderThickness" Value="0" />
6494+
<Setter Property="VirtualizingStackPanel.IsVirtualizing" Value="False" />
64946495
<Setter Property="ItemContainerStyle" Value="{StaticResource WD.TimeSelectorItem}" />
64956496
<Style.Triggers>
64966497
<Trigger Property="IsMouseOver" Value="True">

src/WPFDevelopers.Net45x/Themes/Theme.xaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6492,6 +6492,7 @@
64926492
<Setter Property="ScrollViewer.HorizontalScrollBarVisibility" Value="Hidden" />
64936493
<Setter Property="ScrollViewer.VerticalScrollBarVisibility" Value="Hidden" />
64946494
<Setter Property="BorderThickness" Value="0" />
6495+
<Setter Property="VirtualizingStackPanel.IsVirtualizing" Value="False" />
64956496
<Setter Property="ItemContainerStyle" Value="{StaticResource WD.TimeSelectorItem}" />
64966497
<Style.Triggers>
64976498
<Trigger Property="IsMouseOver" Value="True">

src/WPFDevelopers.Samples.Shared/ExampleViews/TimePickerExample.xaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
<wd:TimePicker
2020
Width="200"
2121
VerticalAlignment="Center"
22-
IsCurrentTime="True"/>
22+
IsCurrentTime="True" />
2323
<wd:TimePicker
2424
Width="200"
2525
VerticalAlignment="Center"

src/WPFDevelopers.Shared/Controls/TimePicker/TimePicker.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using System;
2+
using System.Threading;
23
using System.Windows;
34
using System.Windows.Controls;
45
using System.Windows.Controls.Primitives;
@@ -113,7 +114,9 @@ public override void OnApplyTemplate()
113114
private void Popup_Opened(object sender, EventArgs e)
114115
{
115116
if (_timeSelector != null)
117+
{
116118
_timeSelector.SetTime();
119+
}
117120
}
118121

119122
private void TimeSelector_SelectedTimeChanged(object sender, RoutedPropertyChangedEventArgs<DateTime?> e)

src/WPFDevelopers.Shared/Controls/TimePicker/TimeSelector.cs

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using System;
2+
using System.Collections.Generic;
23
using System.Linq;
34
using System.Windows;
45
using System.Windows.Controls;
@@ -42,21 +43,21 @@ static TimeSelector()
4243

4344
public DateTime? SelectedTime
4445
{
45-
get => (DateTime?) GetValue(SelectedTimeProperty);
46+
get => (DateTime?)GetValue(SelectedTimeProperty);
4647
set => SetValue(SelectedTimeProperty, value);
4748
}
4849

4950

5051
public double ItemHeight
5152
{
52-
get => (double) GetValue(ItemHeightProperty);
53+
get => (double)GetValue(ItemHeightProperty);
5354
set => SetValue(ItemHeightProperty, value);
5455
}
5556

5657

5758
public Thickness SelectorMargin
5859
{
59-
get => (Thickness) GetValue(SelectorMarginProperty);
60+
get => (Thickness)GetValue(SelectorMarginProperty);
6061
set => SetValue(SelectorMarginProperty, value);
6162
}
6263

@@ -77,7 +78,7 @@ private static void OnSelectedTimeChanged(DependencyObject d, DependencyProperty
7778
{
7879
var ctrl = d as TimeSelector;
7980
if (ctrl != null)
80-
ctrl.OnSelectedTimeChanged((DateTime?) e.OldValue, (DateTime?) e.NewValue);
81+
ctrl.OnSelectedTimeChanged((DateTime?)e.OldValue, (DateTime?)e.NewValue);
8182
}
8283

8384

@@ -108,17 +109,16 @@ private int GetFirstNonEmptyItemIndex(ListBox listBox)
108109
public override void OnApplyTemplate()
109110
{
110111
base.OnApplyTemplate();
111-
112112
var minuteSecondList = Enumerable.Range(0, 60).Select(num => num.ToString("D2"));
113113
var emptyData = Enumerable.Repeat(string.Empty, 4);
114114
var result = emptyData.Concat(minuteSecondList).Concat(emptyData);
115115
_listBoxHour = GetTemplateChild(ListBoxHourTemplateName) as ListBox;
116116
if (_listBoxHour != null)
117117
{
118118
var hours = Enumerable.Range(0, 24).Select(num => num.ToString("D2"));
119-
_listBoxHour.ItemsSource = emptyData.Concat(hours).Concat(emptyData);
120119
_listBoxHour.SelectionChanged -= ListBoxHour_SelectionChanged;
121120
_listBoxHour.SelectionChanged += ListBoxHour_SelectionChanged;
121+
_listBoxHour.ItemsSource = emptyData.Concat(hours).Concat(emptyData);
122122
_listBoxHour.Loaded += (sender, args) =>
123123
{
124124
var h = GetItemHeight(_listBoxHour);
@@ -186,16 +186,20 @@ public void SetTime()
186186
_listBoxHour.SelectionChanged -= ListBoxHour_SelectionChanged;
187187
var hour = _hour.ToString("D2");
188188
_listBoxHour.SelectedItem = hour;
189+
//(_listBoxHour as TimeSelectorListBox).Positioning();
189190
_listBoxHour.SelectionChanged += ListBoxHour_SelectionChanged;
190191

192+
191193
_listBoxMinute.SelectionChanged -= ListBoxMinute_SelectionChanged;
192194
var minute = _minute.ToString("D2");
193195
_listBoxMinute.SelectedItem = minute;
196+
//(_listBoxMinute as TimeSelectorListBox).Positioning();
194197
_listBoxMinute.SelectionChanged += ListBoxMinute_SelectionChanged;
195198

196199
_listBoxSecond.SelectionChanged -= ListBoxSecond_SelectionChanged;
197200
var second = _second.ToString("D2");
198201
_listBoxSecond.SelectedItem = second;
202+
//(_listBoxSecond as TimeSelectorListBox).Positioning();
199203
_listBoxSecond.SelectionChanged += ListBoxSecond_SelectionChanged;
200204

201205
SetSelectedTime();

src/WPFDevelopers.Shared/Controls/TimePicker/TimeSelectorListBox.cs

Lines changed: 23 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
using System.Windows;
1+
using System.Diagnostics;
2+
using System.Windows;
23
using System.Windows.Controls;
34
using System.Windows.Input;
45
using WPFDevelopers.Helpers;
@@ -14,9 +15,9 @@ public class TimeSelectorListBox : ListBox
1415
public TimeSelectorListBox()
1516
{
1617
Loaded += TimeSelectorListBox_Loaded;
18+
PreviewMouseWheel -= ScrollListBox_PreviewMouseWheel;
1719
PreviewMouseWheel += ScrollListBox_PreviewMouseWheel;
1820
}
19-
2021
protected override bool IsItemItsOwnContainerOverride(object item)
2122
{
2223
return item is TimeSelectorItem;
@@ -32,18 +33,28 @@ private void ScrollListBox_PreviewMouseWheel(object sender, MouseWheelEventArgs
3233
if (Items != null && Items.Count > 0)
3334
{
3435
var delta = e.Delta;
35-
var itemCount = Items.Count;
3636
var scrollCount = delta > 0 ? -1 : 1;
37-
var newIndex = SelectedIndex + scrollCount;
38-
if (newIndex < 4)
39-
newIndex = 5;
40-
else if (newIndex >= itemCount - 4)
41-
newIndex = itemCount;
42-
SelectedIndex = newIndex;
37+
ItemPositioning(scrollCount);
4338
e.Handled = true;
4439
}
4540
}
46-
41+
void ItemPositioning(int scrollCount)
42+
{
43+
var itemCount = Items.Count;
44+
var newIndex = SelectedIndex + scrollCount;
45+
if (newIndex < 4)
46+
newIndex = 5;
47+
else if (newIndex >= itemCount - 4)
48+
newIndex = itemCount;
49+
SelectedIndex = newIndex;
50+
}
51+
void Positioning()
52+
{
53+
if (SelectedIndex <= 0 || scrollViewer == null) return;
54+
var index = SelectedIndex - (int)lastIndex;
55+
var offset = scrollViewer.VerticalOffset + index;
56+
scrollViewer.ScrollToVerticalOffset(offset);
57+
}
4758
private void TimeSelectorListBox_Loaded(object sender, RoutedEventArgs e)
4859
{
4960
scrollViewer = ControlsHelper.FindVisualChild<ScrollViewer>(this);
@@ -58,9 +69,7 @@ private void ScrollViewer_ScrollChanged(object sender, ScrollChangedEventArgs e)
5869
{
5970
var offset = e.VerticalOffset;
6071
if (isFirst == false)
61-
{
6272
lastIndex = offset + 4;
63-
}
6473
else
6574
{
6675
lastIndex = offset == 0 ? 4 : offset + 4;
@@ -70,15 +79,12 @@ private void ScrollViewer_ScrollChanged(object sender, ScrollChangedEventArgs e)
7079

7180
protected override void OnSelectionChanged(SelectionChangedEventArgs e)
7281
{
82+
base.OnSelectionChanged(e);
7383
if (SelectedIndex != -1 && lastIndex != -1)
7484
{
7585
if (SelectedIndex <= 0) return;
76-
var index = SelectedIndex - lastIndex;
77-
var offset = scrollViewer.VerticalOffset + index;
78-
scrollViewer.ScrollToVerticalOffset(offset);
86+
Positioning();
7987
}
80-
81-
base.OnSelectionChanged(e);
8288
}
8389
}
8490
}

src/WPFDevelopers.Shared/Themes/TimePicker.xaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,7 @@
7676
<Setter Property="ScrollViewer.HorizontalScrollBarVisibility" Value="Hidden" />
7777
<Setter Property="ScrollViewer.VerticalScrollBarVisibility" Value="Hidden" />
7878
<Setter Property="BorderThickness" Value="0" />
79+
<Setter Property="VirtualizingStackPanel.IsVirtualizing" Value="False" />
7980
<Setter Property="ItemContainerStyle" Value="{StaticResource WD.TimeSelectorItem}" />
8081
<Style.Triggers>
8182
<Trigger Property="IsMouseOver" Value="True">

0 commit comments

Comments
 (0)