Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Windows;
- using System.Windows.Controls;
- using Lion.Utils.Common.Extensions;
- namespace Lion.Utils.Wpf
- {
- /// <summary>
- /// Busy indicator helper for automatically on/off when async operation in process.
- /// Supported any ContentControls with property IsBusy and list controls with property ItemsSource
- /// <para></para>
- /// Usage:
- /// <code>
- /// <BusyIndicator AsyncIndicator.Attached="True">
- /// <DataGrid ItemsSource="{Binding WorkItems, IsAsync=True}">
- /// ...
- /// </DataGrid>
- /// </BusyIndicator>
- /// </code>
- /// </summary>
- public static class AsyncIndicator
- {
- static AsyncIndicator() { }
- private const string ListPropertyName = "ItemsSource";
- private const string IsBusyPropertyName = "IsBusy";
- private static readonly DependencyProperty _busyIndicatorProperty = DependencyProperty.RegisterAttached("%BusyIndicatorProperty%",
- typeof (ContentControl), typeof (DependencyObject));
- public static readonly DependencyProperty AttachedProperty = DependencyProperty.RegisterAttached("Attached",
- typeof (bool), typeof (ContentControl), new FrameworkPropertyMetadata(false, AttachedChanged));
- public static Boolean GetAttached(UIElement element)
- {
- return (Boolean) element.GetValue(AttachedProperty);
- }
- public static void SetAttached(UIElement element, Boolean value)
- {
- element.SetValue(AttachedProperty, value);
- }
- private static void SetPropertyChangedCallback(this DependencyProperty dp, DependencyObject d,
- PropertyChangedCallback callback, bool reset = false)
- {
- if (dp == null || d == null) return;
- var typ = d.GetType();
- var x = dp.GetMetadata(typ);
- var oldValue = x.SetPropValue("Sealed", false, false, true);
- x.PropertyChangedCallback -= callback;
- if (!reset)
- x.PropertyChangedCallback += callback;
- x.SetPropValue("Sealed", oldValue, false, true);
- }
- private static void AttachedChanged(DependencyObject busyIndicator, DependencyPropertyChangedEventArgs e)
- {
- ContentControl.ContentProperty.SetPropertyChangedCallback(busyIndicator, ContentChangedCallback);
- }
- private static DependencyProperty GetItemsSourceProperty(this DependencyObject contentObject)
- {
- return contentObject == null
- ? null
- : (contentObject.GetFieldValue(ListPropertyName + "Property") ??
- contentObject.GetPropValue(ListPropertyName + "Property")) as DependencyProperty;
- }
- private static object GetItemsSourceValue(this DependencyObject contentObject)
- {
- return contentObject == null ? null : contentObject.GetValue(contentObject.GetItemsSourceProperty());
- }
- private static DependencyObject GetBusyIndicator(this DependencyObject contentObject)
- {
- return contentObject == null ? null : contentObject.GetValue(_busyIndicatorProperty) as DependencyObject;
- }
- private static void SetBusyIndicator(this DependencyObject contentObject, DependencyObject busyIndicator)
- {
- if (contentObject != null)
- {
- contentObject.GetItemsSourceProperty().SetPropertyChangedCallback(contentObject, ItemsSourceChangedCallback,
- busyIndicator == null);
- contentObject.SetValue(_busyIndicatorProperty, busyIndicator);
- }
- busyIndicator.UpdateBusyIndicator(contentObject);
- }
- private static void UpdateBusyIndicator(this DependencyObject busyIndicator, DependencyObject contentObject)
- {
- if (busyIndicator == null) return;
- if (contentObject == null)
- busyIndicator.SetPropValue(IsBusyPropertyName, false);
- else
- busyIndicator.SetPropValue(IsBusyPropertyName, contentObject.GetItemsSourceValue() == null);
- }
- private static bool IsAttached(this DependencyObject busyIndicator)
- {
- return (bool) busyIndicator.GetValue(AttachedProperty);
- }
- private static void ContentChangedCallback(DependencyObject busyIndicator, DependencyPropertyChangedEventArgs e)
- {
- if (!busyIndicator.IsAttached()) return;
- (e.OldValue as DependencyObject).SetBusyIndicator(null);
- (e.NewValue as DependencyObject).SetBusyIndicator(busyIndicator);
- }
- private static void ItemsSourceChangedCallback(DependencyObject contentObject, DependencyPropertyChangedEventArgs e)
- {
- contentObject.GetBusyIndicator().UpdateBusyIndicator(contentObject);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement