Dreamine.UI.Wpf 1.0.1
Dreamine.UI.Wpf 사용자 인터페이스 기능과 구성 요소를 제공합니다.
로딩중...
검색중...
일치하는것 없음
AutoScrollListBoxBehavior.cs
이 파일의 문서화 페이지로 가기
1using System.Collections.Specialized;
2using System.Windows;
3using System.Windows.Controls;
4
6
23public static class AutoScrollListBoxBehavior
24{
33 public static readonly DependencyProperty IsEnabledProperty =
34 DependencyProperty.RegisterAttached(
35 "IsEnabled",
36 typeof(bool),
38 new PropertyMetadata(false, OnIsEnabledChanged));
39
72 public static void SetIsEnabled(DependencyObject element, bool value)
73 => element.SetValue(IsEnabledProperty, value);
74
107 public static bool GetIsEnabled(DependencyObject element)
108 => (bool)element.GetValue(IsEnabledProperty);
109
118 private static readonly DependencyProperty SubscriptionProperty =
119 DependencyProperty.RegisterAttached(
120 "Subscription",
121 typeof(NotifyCollectionChangedEventHandler),
123 new PropertyMetadata(null));
124
149 private static void OnIsEnabledChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
150 {
151 if (d is not ListBox listBox)
152 return;
153
154 if ((bool)e.NewValue)
155 {
156 listBox.Loaded += OnLoaded;
157 listBox.Unloaded += OnUnloaded;
158 }
159 else
160 {
161 listBox.Loaded -= OnLoaded;
162 listBox.Unloaded -= OnUnloaded;
163 Detach(listBox);
164 }
165 }
166
191 private static void OnLoaded(object sender, RoutedEventArgs e)
192 {
193 if (sender is not ListBox listBox)
194 return;
195
196 Attach(listBox);
197 ScrollToLast(listBox);
198 }
199
224 private static void OnUnloaded(object sender, RoutedEventArgs e)
225 {
226 if (sender is not ListBox listBox)
227 return;
228
229 Detach(listBox);
230 }
231
248 private static void Attach(ListBox listBox)
249 {
250 Detach(listBox);
251
252 if (listBox.ItemsSource is not INotifyCollectionChanged incc)
253 return;
254
255 NotifyCollectionChangedEventHandler handler = (_, __) =>
256 {
257 listBox.Dispatcher.InvokeAsync(() => ScrollToLast(listBox));
258 };
259
260 incc.CollectionChanged += handler;
261 listBox.SetValue(SubscriptionProperty, handler);
262 }
263
280 private static void Detach(ListBox listBox)
281 {
282 if (listBox.ItemsSource is INotifyCollectionChanged incc &&
283 listBox.GetValue(SubscriptionProperty) is NotifyCollectionChangedEventHandler handler)
284 {
285 incc.CollectionChanged -= handler;
286 }
287
288 listBox.ClearValue(SubscriptionProperty);
289 }
290
307 private static void ScrollToLast(ListBox listBox)
308 {
309 if (listBox.Items.Count <= 0)
310 return;
311
312 var last = listBox.Items[listBox.Items.Count - 1];
313 listBox.ScrollIntoView(last);
314 }
315}
static void OnLoaded(object sender, RoutedEventArgs e)
static void OnIsEnabledChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
static void SetIsEnabled(DependencyObject element, bool value)
static void OnUnloaded(object sender, RoutedEventArgs e)