Skip to content

Commit c721b28

Browse files
authored
Merge pull request #347 from Jack251970/scrollviewer_animation
[Enhancement] ScrollViewerEx: Add EnableScrollAnimation to enable/disable scroll animation
2 parents a9a96da + c9d8319 commit c721b28

File tree

3 files changed

+123
-52
lines changed

3 files changed

+123
-52
lines changed

source/iNKORE.UI.WPF.Modern.Gallery/Pages/Controls/Windows/ScrollViewerPage.xaml

Lines changed: 48 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
xmlns:local="clr-namespace:iNKORE.UI.WPF.Modern.Gallery"
77
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
88
xmlns:sys="clr-namespace:System;assembly=mscorlib"
9-
xmlns:ui="http://schemas.inkore.net/lib/ui/wpf/modern"
9+
xmlns:ui="http://schemas.inkore.net/lib/ui/wpf/modern" xmlns:ikw="http://schemas.inkore.net/lib/ui/wpf"
1010
Title="ScrollViewer Page"
1111
d:DesignWidth="800"
1212
mc:Ignorable="d"
@@ -25,36 +25,39 @@
2525
Height="266"
2626
HorizontalAlignment="Left"
2727
VerticalAlignment="Top"
28+
IsScrollAnimationEnabled="{Binding ElementName=CheckBox_Example1_IsScrollAnimationEnabled, Path=IsChecked, Mode=OneWay}"
2829
HorizontalScrollBarVisibility="{Binding ElementName=hsbvCombo, Path=SelectedItem.Content, Mode=OneWay}"
2930
VerticalScrollBarVisibility="{Binding ElementName=vsbvCombo, Path=SelectedItem.Content, Mode=OneWay}" >
3031
<Grid>
31-
<Image
32-
HorizontalAlignment="Center"
33-
VerticalAlignment="Center"
34-
AutomationProperties.Name="cliff"
35-
Source="/Assets/SampleMedia/cliff.jpg"
36-
Stretch="None" />
32+
<Image Source="/Assets/SampleMedia/cliff.jpg"
33+
AutomationProperties.Name="cliff" Stretch="None" />
3734
</Grid>
3835
</ui:ScrollViewerEx>
3936

4037
<local:ControlExample.Options>
41-
<Grid MinWidth="200">
42-
<Grid.ColumnDefinitions>
43-
<ColumnDefinition Width="Auto" />
44-
<ColumnDefinition />
45-
</Grid.ColumnDefinitions>
46-
<Grid.RowDefinitions>
47-
<RowDefinition Height="Auto"/>
48-
<RowDefinition Height="Auto"/>
49-
<RowDefinition Height="Auto"/>
50-
<RowDefinition Height="Auto"/>
51-
<RowDefinition Height="Auto"/>
52-
<RowDefinition Height="Auto"/>
53-
<RowDefinition Height="Auto"/>
54-
<RowDefinition Height="Auto"/>
55-
</Grid.RowDefinitions>
38+
<ikw:SimpleStackPanel MinWidth="200" Spacing="16">
39+
<ikw:SimpleStackPanel>
40+
<CheckBox x:Name="CheckBox_Example1_IsScrollAnimationEnabled" Content="IsScrollAnimationEnabled"
41+
IsChecked="True" HorizontalAlignment="Stretch" Click="CheckBox_Example1_IsScrollAnimationEnabled_Click"/>
42+
</ikw:SimpleStackPanel>
5643

57-
<!-- ZoomMode
44+
<Grid>
45+
<Grid.ColumnDefinitions>
46+
<ColumnDefinition Width="Auto" />
47+
<ColumnDefinition />
48+
</Grid.ColumnDefinitions>
49+
<Grid.RowDefinitions>
50+
<RowDefinition Height="Auto"/>
51+
<RowDefinition Height="Auto"/>
52+
<RowDefinition Height="Auto"/>
53+
<RowDefinition Height="Auto"/>
54+
<RowDefinition Height="Auto"/>
55+
<RowDefinition Height="Auto"/>
56+
<RowDefinition Height="Auto"/>
57+
<RowDefinition Height="Auto"/>
58+
</Grid.RowDefinitions>
59+
60+
<!-- ZoomMode
5861
<TextBlock Grid.Row="0" VerticalAlignment="Center" Text="ZoomMode" />
5962
<ComboBox x:Name="cmbZoomMode"
6063
Grid.Row="0" Grid.Column="1"
@@ -63,7 +66,7 @@
6366
<ComboBoxItem>Enabled</ComboBoxItem>
6467
<ComboBoxItem>Disabled</ComboBoxItem>
6568
</ComboBox> -->
66-
<!-- ZoomNumberBox
69+
<!-- ZoomNumberBox
6770
<TextBlock Grid.Row="1" VerticalAlignment="Center" Text="ZoomFactor" Margin="0,8,0,0"/>
6871
<ui:NumberBox x:Name="ZoomNumberBox"
6972
Grid.Row="1" Grid.Column="1"
@@ -76,7 +79,7 @@
7679
ValueChanged="ZoomNumberBox_ValueChanged"
7780
Width="120"
7881
Margin="0,8,0,0"/> -->
79-
<!-- ScrollMode
82+
<!-- ScrollMode
8083
<TextBlock Grid.Row="2" Grid.ColumnSpan="2" HorizontalAlignment="Center" Text="ScrollMode" Margin="0,8,0,0"/>
8184
<TextBlock Grid.Row="3" VerticalAlignment="Center" Text="Horizontal" Margin="0,8,0,0"/>
8285
<ComboBox x:Name="cmbHorizontalScrollMode"
@@ -98,32 +101,33 @@
98101
<ComboBoxItem>Disabled</ComboBoxItem>
99102
<ComboBoxItem>Auto</ComboBoxItem>
100103
</ComboBox> -->
101-
<TextBlock Grid.Row="5" Grid.ColumnSpan="2" HorizontalAlignment="Center" Text="ScrollbarVisibility" Margin="0,0,0,0" FontWeight="Bold"/>
102-
<TextBlock Grid.Row="6" Margin="0,8,10,0" VerticalAlignment="Center" Text="Horizontal"/>
103-
<ComboBox x:Name="hsbvCombo"
104+
<TextBlock Grid.Row="5" Grid.ColumnSpan="2" HorizontalAlignment="Center" Text="ScrollbarVisibility" Margin="0,0,0,8" FontWeight="Bold"/>
105+
<TextBlock Grid.Row="6" Margin="0,12,10,12" VerticalAlignment="Center" Text="Horizontal"/>
106+
<ComboBox x:Name="hsbvCombo"
104107
Grid.Row="6" Grid.Column="1"
105108
HorizontalAlignment="Stretch"
106109
SelectedIndex="0"
107110
SelectionChanged="hsbvCombo_SelectionChanged_1"
108-
Margin="12,16,0,0">
109-
<ComboBoxItem Content="Auto"/>
110-
<ComboBoxItem Content="Visible"/>
111-
<ComboBoxItem Content="Hidden"/>
112-
<ComboBoxItem Content="Disabled"/>
113-
</ComboBox>
114-
<TextBlock Grid.Row="7" Margin="0,8,10,0" VerticalAlignment="Center" Text="Vertical"/>
115-
<ComboBox x:Name="vsbvCombo"
111+
Margin="0,0,0,0" VerticalAlignment="Center">
112+
<ComboBoxItem Content="Auto"/>
113+
<ComboBoxItem Content="Visible"/>
114+
<ComboBoxItem Content="Hidden"/>
115+
<ComboBoxItem Content="Disabled"/>
116+
</ComboBox>
117+
<TextBlock Grid.Row="7" Margin="0,12,10,12" VerticalAlignment="Center" Text="Vertical"/>
118+
<ComboBox x:Name="vsbvCombo"
116119
Grid.Row="7" Grid.Column="1"
117-
Margin="12,16,0,0"
120+
Margin="0,0,0,0"
118121
HorizontalAlignment="Stretch"
119122
SelectionChanged="vsbvCombo_SelectionChanged_1"
120-
SelectedIndex="0">
121-
<ComboBoxItem Content="Auto"/>
122-
<ComboBoxItem Content="Visible"/>
123-
<ComboBoxItem Content="Hidden"/>
124-
<ComboBoxItem Content="Disabled"/>
125-
</ComboBox>
126-
</Grid>
123+
SelectedIndex="0" VerticalAlignment="Center">
124+
<ComboBoxItem Content="Auto"/>
125+
<ComboBoxItem Content="Visible"/>
126+
<ComboBoxItem Content="Hidden"/>
127+
<ComboBoxItem Content="Disabled"/>
128+
</ComboBox>
129+
</Grid>
130+
</ikw:SimpleStackPanel>
127131
</local:ControlExample.Options>
128132
</local:ControlExample>
129133
</StackPanel>

source/iNKORE.UI.WPF.Modern.Gallery/Pages/Controls/Windows/ScrollViewerPage.xaml.cs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,12 @@ private void vsbvCombo_SelectionChanged_1(object sender, SelectionChangedEventAr
9393
UpdateExampleCode();
9494
}
9595

96+
private void CheckBox_Example1_IsScrollAnimationEnabled_Click(object sender, RoutedEventArgs e)
97+
{
98+
UpdateExampleCode();
99+
}
100+
101+
96102
private void Page_SizeChanged(object sender, SizeChangedEventArgs e)
97103
{
98104
if (Grid.GetColumnSpan(Control1) == 1)
@@ -122,13 +128,14 @@ public void UpdateExampleCode()
122128

123129
public string Example1Xaml => $@"
124130
<ui:ScrollViewerEx x:Name=""Control1"" Width=""{400}"" Height=""{266}""
125-
VerticalAlignment=""Top"" HorizontalAlignment=""Left""
131+
VerticalAlignment=""Top"" HorizontalAlignment=""Left"" IsScrollAnimationEnabled=""{CheckBox_Example1_IsScrollAnimationEnabled.IsChecked}""
126132
HorizontalScrollBarVisibility=""{((ComboBoxItem)hsbvCombo.SelectedItem).Content}"" VerticalScrollBarVisibility=""{((ComboBoxItem)vsbvCombo.SelectedItem).Content}"">
127133
<Image Source=""/Assets/SampleMedia/cliff.jpg"" AutomationProperties.Name=""cliff""
128134
HorizontalAlignment=""Center"" VerticalAlignment=""Center"" Stretch=""None"" />
129135
</ui:ScrollViewerEx>
130136
";
131137

132138
#endregion
139+
133140
}
134141
}

source/iNKORE.UI.WPF.Modern/Controls/ScrollViewerEx.cs

Lines changed: 67 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,23 @@ public double WheelSensitivity
117117

118118
#endregion
119119

120+
#region IsScrollAnimationEnabled
121+
122+
public static readonly DependencyProperty IsScrollAnimationEnabledProperty =
123+
DependencyProperty.Register(
124+
nameof(IsScrollAnimationEnabled),
125+
typeof(bool),
126+
typeof(ScrollViewerEx),
127+
new PropertyMetadata(true));
128+
129+
public bool IsScrollAnimationEnabled
130+
{
131+
get => (bool)GetValue(IsScrollAnimationEnabledProperty);
132+
set => SetValue(IsScrollAnimationEnabledProperty, value);
133+
}
134+
135+
#endregion
136+
120137
private void OnLoaded(object sender, RoutedEventArgs e)
121138
{
122139
LastVerticalLocation = VerticalOffset;
@@ -179,9 +196,17 @@ protected override void OnMouseWheel(MouseWheelEventArgs e)
179196

180197
ScrollToVerticalOffset(LastVerticalLocation);
181198

182-
double scale = Math.Abs((LastVerticalLocation - newOffset) / WheelChange);
199+
if (IsScrollAnimationEnabled)
200+
{
201+
double scale = Math.Abs((LastVerticalLocation - newOffset) / WheelChange);
202+
203+
AnimateScroll(newOffset, Direction, scale);
204+
}
205+
else
206+
{
207+
Scroll(newOffset, Direction);
208+
}
183209

184-
AnimateScroll(newOffset, Direction, scale);
185210
LastVerticalLocation = newOffset;
186211
}
187212
else
@@ -214,12 +239,19 @@ protected override void OnMouseWheel(MouseWheelEventArgs e)
214239

215240
ScrollToHorizontalOffset(LastHorizontalLocation);
216241

217-
double scale = Math.Abs((LastHorizontalLocation - newOffset) / WheelChange);
242+
if (IsScrollAnimationEnabled)
243+
{
244+
double scale = Math.Abs((LastHorizontalLocation - newOffset) / WheelChange);
245+
246+
AnimateScroll(newOffset, Direction, scale);
247+
}
248+
else
249+
{
250+
Scroll(newOffset, Direction);
251+
}
218252

219-
AnimateScroll(newOffset, Direction, scale);
220253
LastHorizontalLocation = newOffset;
221254
}
222-
223255
}
224256

225257
/// <inheritdoc/>
@@ -286,14 +318,28 @@ public bool ChangeView(double? horizontalOffset, double? verticalOffset, float?
286318
if (horizontalOffset.HasValue)
287319
{
288320
ScrollToHorizontalOffset(LastHorizontalLocation);
289-
AnimateScroll(Math.Min(ScrollableWidth, horizontalOffset.Value), Orientation.Horizontal, 1);
321+
if (IsScrollAnimationEnabled)
322+
{
323+
AnimateScroll(Math.Min(ScrollableWidth, horizontalOffset.Value), Orientation.Horizontal, 1);
324+
}
325+
else
326+
{
327+
Scroll(Math.Min(ScrollableWidth, horizontalOffset.Value), Orientation.Horizontal);
328+
}
290329
LastHorizontalLocation = horizontalOffset.Value;
291330
}
292331

293332
if (verticalOffset.HasValue)
294333
{
295334
ScrollToVerticalOffset(LastVerticalLocation);
296-
AnimateScroll(Math.Min(ScrollableHeight, verticalOffset.Value), Orientation.Vertical, 1);
335+
if (IsScrollAnimationEnabled)
336+
{
337+
AnimateScroll(Math.Min(ScrollableHeight, verticalOffset.Value), Orientation.Vertical, 1);
338+
}
339+
else
340+
{
341+
Scroll(Math.Min(ScrollableHeight, verticalOffset.Value), Orientation.Vertical);
342+
}
297343
LastVerticalLocation = verticalOffset.Value;
298344
}
299345
}
@@ -333,6 +379,20 @@ private void AnimateScroll(double ToValue, Orientation Direction, double Scale)
333379
BeginAnimation(ScrollViewerBehavior.IsAnimatingProperty, keyFramesAnimation);
334380
}
335381

382+
private void Scroll(double ToValue, Orientation Direction)
383+
{
384+
if (Direction == Orientation.Vertical)
385+
{
386+
ScrollToVerticalOffset(ToValue);
387+
}
388+
else
389+
{
390+
ScrollToHorizontalOffset(ToValue);
391+
}
392+
393+
ScrollViewerBehavior.SetIsAnimating(this, false);
394+
}
395+
336396
private void UpdateVisualState(bool useTransitions = true)
337397
{
338398
string stateName = AutoHideScrollBars ? "NoIndicator" : "MouseIndicator";

0 commit comments

Comments
 (0)