From bc941e5c5c266cb7da55be0d2dd99b49696efc7b Mon Sep 17 00:00:00 2001
From: Kaniu Tayou <1056879522@qq.com>
Date: Thu, 20 Dec 2018 00:18:49 +0800
Subject: [PATCH] =?UTF-8?q?=E6=BB=9A=E5=8A=A8=E6=9D=A1?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
StormChatWPF/MainWindow.xaml | 6 ++-
StormChatWPF/ScrollViewer.cs | 64 ++++++++++++++++++++++++++++++++
StormChatWPF/StormChatWPF.csproj | 1 +
3 files changed, 70 insertions(+), 1 deletion(-)
create mode 100644 StormChatWPF/ScrollViewer.cs
diff --git a/StormChatWPF/MainWindow.xaml b/StormChatWPF/MainWindow.xaml
index 7c4c99a..74d527b 100644
--- a/StormChatWPF/MainWindow.xaml
+++ b/StormChatWPF/MainWindow.xaml
@@ -23,6 +23,10 @@
-
+
+
+
+
+
diff --git a/StormChatWPF/ScrollViewer.cs b/StormChatWPF/ScrollViewer.cs
new file mode 100644
index 0000000..3ff0a3d
--- /dev/null
+++ b/StormChatWPF/ScrollViewer.cs
@@ -0,0 +1,64 @@
+using System;
+using System.Windows;
+using System.Windows.Controls;
+
+namespace StormChatWPF
+{
+ public class ScrollViewerExtensions
+ {
+ public static readonly DependencyProperty AlwaysScrollToEndProperty = DependencyProperty.RegisterAttached("AlwaysScrollToEnd", typeof(bool), typeof(ScrollViewerExtensions), new PropertyMetadata(false, AlwaysScrollToEndChanged));
+ private static bool _autoScroll;
+
+
+ private static void AlwaysScrollToEndChanged(object sender, DependencyPropertyChangedEventArgs e)
+ {
+ ScrollViewer scroll = sender as ScrollViewer;
+ if (scroll != null)
+ {
+ bool alwaysScrollToEnd = (e.NewValue != null) && (bool)e.NewValue;
+ if (alwaysScrollToEnd)
+ {
+ scroll.ScrollToEnd();
+ scroll.ScrollChanged += ScrollChanged;
+ // scroll.SizeChanged += Scroll_SizeChanged;
+ }
+ else { scroll.ScrollChanged -= ScrollChanged; /*scroll.ScrollChanged -= ScrollChanged; */}
+ }
+ else { throw new InvalidOperationException("The attached AlwaysScrollToEnd property can only be applied to ScrollViewer instances."); }
+ }
+
+
+ //private static void Scroll_SizeChanged(object sender, SizeChangedEventArgs e)
+ //{
+ // ScrollViewer scroll = sender as ScrollViewer;
+ // if (scroll == null) { throw new InvalidOperationException("The attached AlwaysScrollToEnd property can only be applied to ScrollViewer instances."); }
+ // double d = scroll.ActualHeight + scroll.ViewportHeight + scroll.ExtentHeight;
+ // scroll.ScrollToVerticalOffset(d);
+ //}
+
+
+ public static bool GetAlwaysScrollToEnd(ScrollViewer scroll)
+ {
+ if (scroll == null) { throw new ArgumentNullException("scroll"); }
+ return (bool)scroll.GetValue(AlwaysScrollToEndProperty);
+ }
+
+
+ public static void SetAlwaysScrollToEnd(ScrollViewer scroll, bool alwaysScrollToEnd)
+ {
+ if (scroll == null) { throw new ArgumentNullException("scroll"); }
+ scroll.SetValue(AlwaysScrollToEndProperty, alwaysScrollToEnd);
+ }
+
+
+ private static void ScrollChanged(object sender, ScrollChangedEventArgs e)
+ {
+ ScrollViewer scroll = sender as ScrollViewer;
+ if (scroll == null) { throw new InvalidOperationException("The attached AlwaysScrollToEnd property can only be applied to ScrollViewer instances."); }
+
+
+ if (e.ExtentHeightChange == 0) { _autoScroll = scroll.VerticalOffset == scroll.ScrollableHeight; }
+ if (_autoScroll && e.ExtentHeightChange != 0) { scroll.ScrollToVerticalOffset(scroll.ExtentHeight); }
+ }
+ }
+}
diff --git a/StormChatWPF/StormChatWPF.csproj b/StormChatWPF/StormChatWPF.csproj
index 08cf33d..ca29bee 100644
--- a/StormChatWPF/StormChatWPF.csproj
+++ b/StormChatWPF/StormChatWPF.csproj
@@ -63,6 +63,7 @@
MainWindow.xaml
+
MSBuild:Compile
Designer