add GotoTop

This commit is contained in:
NaBian committed 2019-02-23 20:06:03 +08:00
1 parent b8692d080f
commit b8c264d135
16 files changed
+238 -7

No files matched your search

+100
View File
@@ -0,0 +1,100 @@
using System;
using System.Windows;
using System.Windows.Controls;
using HandyControl.Data;
using HandyControl.Tools;
using HandyControl.Tools.Extension;
namespace HandyControl.Controls
{
public class GotoTop : Button
{
private Action _gotoTopAction;
private System.Windows.Controls.ScrollViewer _scrollViewer;
public static readonly DependencyProperty TargetProperty = DependencyProperty.Register(
"Target", typeof(DependencyObject), typeof(GotoTop), new PropertyMetadata(default(DependencyObject)));
public DependencyObject Target
{
get => (DependencyObject)GetValue(TargetProperty);
set => SetValue(TargetProperty, value);
}
public GotoTop() => Loaded += (s, e) => CreateGotoAction(Target);
public virtual void CreateGotoAction(DependencyObject obj)
{
if (_scrollViewer != null)
{
_scrollViewer.ScrollChanged -= ScrollViewer_ScrollChanged;
}
_scrollViewer = VisualHelper.GetGetChild<System.Windows.Controls.ScrollViewer>(obj);
if (_scrollViewer != null)
{
_scrollViewer.ScrollChanged += ScrollViewer_ScrollChanged;
if (_scrollViewer is ScrollViewer scrollViewerHandy && Animated && scrollViewerHandy.IsEnableInertia)
{
_gotoTopAction = () => scrollViewerHandy.ScrollToVerticalOffsetInternal(0, AnimationTime);
}
else
{
_gotoTopAction = () => _scrollViewer.ScrollToTop();
}
}
}
private void ScrollViewer_ScrollChanged(object sender, ScrollChangedEventArgs e)
{
if (AutoHiding)
{
this.Show(e.VerticalOffset >= HidingHeight);
}
}
public static readonly DependencyProperty AnimatedProperty = DependencyProperty.Register(
"Animated", typeof(bool), typeof(GotoTop), new PropertyMetadata(ValueBoxes.TrueBox));
public bool Animated
{
get => (bool) GetValue(AnimatedProperty);
set => SetValue(AnimatedProperty, value);
}
public static readonly DependencyProperty AnimationTimeProperty = DependencyProperty.Register(
"AnimationTime", typeof(double), typeof(GotoTop), new PropertyMetadata(ValueBoxes.Double200Box));
public double AnimationTime
{
get => (double) GetValue(AnimationTimeProperty);
set => SetValue(AnimationTimeProperty, value);
}
public static readonly DependencyProperty HidingHeightProperty = DependencyProperty.Register(
"HidingHeight", typeof(double), typeof(GotoTop), new PropertyMetadata(default(double)));
public double HidingHeight
{
get => (double) GetValue(HidingHeightProperty);
set => SetValue(HidingHeightProperty, value);
}
public static readonly DependencyProperty AutoHidingProperty = DependencyProperty.Register(
"AutoHiding", typeof(bool), typeof(GotoTop), new PropertyMetadata(ValueBoxes.TrueBox));
public bool AutoHiding
{
get => (bool) GetValue(AutoHidingProperty);
set => SetValue(AutoHidingProperty, value);
}
protected override void OnClick()
{
base.OnClick();
_gotoTopAction?.Invoke();
}
}
}
+9
View File
@@ -111,6 +111,7 @@
<Compile Include="Controls\Other\Carousel.cs" />
<Compile Include="Controls\Cover\CoverView\CoverView.cs" />
<Compile Include="Controls\Other\ChatBubble.cs" />
<Compile Include="Controls\Other\GotoTop.cs" />
<Compile Include="Controls\Transfer\Transfer.cs" />
<Compile Include="Controls\Transfer\TransferGroup.cs" />
<Compile Include="Controls\Transfer\TransferItem.cs" />
@@ -379,6 +380,10 @@
<SubType>Designer</SubType>
<Generator>MSBuild:Compile</Generator>
</Page>
<Page Include="Themes\Styles\Base\GotoTopBaseStyle.xaml">
<SubType>Designer</SubType>
<Generator>MSBuild:Compile</Generator>
</Page>
<Page Include="Themes\Styles\Base\ProgressButtonBaseStyle.xaml">
<SubType>Designer</SubType>
<Generator>MSBuild:Compile</Generator>
@@ -427,6 +432,10 @@
<SubType>Designer</SubType>
<Generator>MSBuild:Compile</Generator>
</Page>
<Page Include="Themes\Styles\GotoTop.xaml">
<SubType>Designer</SubType>
<Generator>MSBuild:Compile</Generator>
</Page>
<Page Include="Themes\Styles\GroupBox.xaml">
<SubType>Designer</SubType>
<Generator>MSBuild:Compile</Generator>
@@ -0,0 +1,28 @@
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:controls="clr-namespace:HandyControl.Controls">
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="../../Basic/Geometries.xaml"/>
<ResourceDictionary Source="../Button.xaml"/>
</ResourceDictionary.MergedDictionaries>
<Style x:Key="GotoTopBaseStyle" BasedOn="{StaticResource ButtonBaseStyle}" TargetType="controls:GotoTop">
<Setter Property="Width" Value="50"/>
<Setter Property="Height" Value="50"/>
<Setter Property="controls:BorderElement.CornerRadius" Value="25"/>
<Setter Property="controls:IconElement.Geometry" Value="{StaticResource UpGeometry}"/>
<Setter Property="Foreground" Value="{DynamicResource PrimaryTextBrush}"/>
<Setter Property="Padding" Value="16"/>
<Setter Property="BorderThickness" Value="1"/>
<Style.Triggers>
<Trigger Property="IsMouseOver" Value="True">
<Setter Property="Background" Value="{DynamicResource DarkDefaultBrush}"/>
</Trigger>
<Trigger Property="IsPressed" Value="True">
<Setter Property="Background" Value="{DynamicResource BorderBrush}"/>
</Trigger>
</Style.Triggers>
</Style>
</ResourceDictionary>
+10
View File
@@ -0,0 +1,10 @@
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:controls="clr-namespace:HandyControl.Controls">
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="Base/GotoTopBaseStyle.xaml"/>
</ResourceDictionary.MergedDictionaries>
<Style BasedOn="{StaticResource GotoTopBaseStyle}" TargetType="controls:GotoTop"/>
</ResourceDictionary>
+1
View File
@@ -57,6 +57,7 @@
<ResourceDictionary Source="ProgressButton.xaml"/>
<ResourceDictionary Source="Transfer.xaml"/>
<ResourceDictionary Source="ChatBubble.xaml"/>
<ResourceDictionary Source="GotoTop.xaml"/>
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
+28 -5
View File
@@ -1,14 +1,15 @@
using System.Linq;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;
namespace HandyControl.Tools
{
public class VisualHelper
{
internal static VisualStateGroup TryGetVisualStateGroup(DependencyObject dependencyObject, string groupName)
internal static VisualStateGroup TryGetVisualStateGroup(DependencyObject d, string groupName)
{
var root = GetImplementationRoot(dependencyObject);
var root = GetImplementationRoot(d);
if (root == null)
{
return null;
@@ -20,13 +21,35 @@ namespace HandyControl.Tools
.FirstOrDefault(group => string.CompareOrdinal(groupName, group.Name) == 0);
}
internal static FrameworkElement GetImplementationRoot(DependencyObject dependencyObject)
internal static FrameworkElement GetImplementationRoot(DependencyObject d)
{
return 1 == VisualTreeHelper.GetChildrenCount(dependencyObject)
? VisualTreeHelper.GetChild(dependencyObject, 0) as FrameworkElement
return 1 == VisualTreeHelper.GetChildrenCount(d)
? VisualTreeHelper.GetChild(d, 0) as FrameworkElement
: null;
}
internal static T GetGetChild<T>(DependencyObject d) where T : DependencyObject
{
if (d is T scrollViewer)
{
return scrollViewer;
}
for (int i = 0; i < VisualTreeHelper.GetChildrenCount(d); i++)
{
var child = VisualTreeHelper.GetChild(d, i);
var result = GetGetChild<T>(child);
if (result != null) return result;
}
return default(T);
}
/// <summary>
/// 获取当前应用中处于激活的一个窗口
/// </summary>
/// <returns></returns>
public static Window GetActiveWindow() => Application.Current.Windows.OfType<Window>().SingleOrDefault(x => x.IsActive);
}
}