更新
This commit is contained in:
313
AntdWpf/Helpers/Control.cs
Normal file
313
AntdWpf/Helpers/Control.cs
Normal file
@@ -0,0 +1,313 @@
|
||||
namespace AntdWpf.Helpers
|
||||
{
|
||||
using System.ComponentModel;
|
||||
using System.Windows;
|
||||
using System.Windows.Media;
|
||||
|
||||
using AntdWpf.Controls;
|
||||
|
||||
/// <summary>
|
||||
/// A helper class that provides various controls.
|
||||
/// </summary>
|
||||
public static class Control
|
||||
{
|
||||
#region Border
|
||||
|
||||
public static readonly DependencyProperty CornerRadiusProperty = DependencyProperty.RegisterAttached(
|
||||
"CornerRadius",
|
||||
typeof(CornerRadius),
|
||||
typeof(Control),
|
||||
new FrameworkPropertyMetadata(
|
||||
default(CornerRadius),
|
||||
FrameworkPropertyMetadataOptions.AffectsMeasure | FrameworkPropertyMetadataOptions.AffectsRender
|
||||
));
|
||||
|
||||
/// <summary>
|
||||
/// Gets a value that represents the degree to which the corners of a control border are rounded.
|
||||
/// </summary>
|
||||
[Category("AntDesign")]
|
||||
public static CornerRadius GetCornerRadius(DependencyObject obj)
|
||||
{
|
||||
return (CornerRadius)obj.GetValue(CornerRadiusProperty);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Sets a value that represents the degree to which the corners of a control border are rounded.
|
||||
/// </summary>
|
||||
public static void SetCornerRadius(DependencyObject obj, CornerRadius value)
|
||||
{
|
||||
obj.SetValue(CornerRadiusProperty, value);
|
||||
}
|
||||
|
||||
public static readonly DependencyProperty BorderStyleProperty = DependencyProperty.RegisterAttached(
|
||||
"BorderStyle",
|
||||
typeof(BorderStyle),
|
||||
typeof(Control),
|
||||
new PropertyMetadata(BorderStyle.Solid));
|
||||
|
||||
/// <summary>
|
||||
/// Gets the style of the control border.
|
||||
/// </summary>
|
||||
public static BorderStyle GetBorderStyle(DependencyObject obj)
|
||||
{
|
||||
return (BorderStyle)obj.GetValue(BorderStyleProperty);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Sets the style of the control border.
|
||||
/// </summary>
|
||||
public static void SetBorderStyle(DependencyObject obj, BorderStyle value)
|
||||
{
|
||||
obj.SetValue(BorderStyleProperty, value);
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Size
|
||||
|
||||
public static readonly DependencyProperty SizeProperty = DependencyProperty.RegisterAttached(
|
||||
"Size",
|
||||
typeof(Sizes?),
|
||||
typeof(Control),
|
||||
new PropertyMetadata(null));
|
||||
|
||||
/// <summary>
|
||||
/// Gets the size of the control.
|
||||
/// </summary>
|
||||
public static Sizes? GetSize(DependencyObject obj)
|
||||
{
|
||||
return (Sizes?)obj.GetValue(SizeProperty);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Sets the size of the control.
|
||||
/// </summary>
|
||||
public static void SetSize(DependencyObject obj, Sizes? value)
|
||||
{
|
||||
obj.SetValue(SizeProperty, value);
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Brushes
|
||||
|
||||
public static readonly DependencyProperty MouseOverForegroundProperty = DependencyProperty.RegisterAttached(
|
||||
"MouseOverForeground",
|
||||
typeof(Brush),
|
||||
typeof(Control),
|
||||
new FrameworkPropertyMetadata(
|
||||
Brushes.Transparent, FrameworkPropertyMetadataOptions.AffectsRender | FrameworkPropertyMetadataOptions.Inherits
|
||||
));
|
||||
|
||||
/// <summary>
|
||||
/// Gets the foreground for controls in mouse over.
|
||||
/// </summary>
|
||||
public static Brush GetMouseOverForeground(DependencyObject obj)
|
||||
{
|
||||
return (Brush)obj.GetValue(MouseOverForegroundProperty);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets the foreground for controls in mouse over.
|
||||
/// </summary>
|
||||
public static void SetMouseOverForeground(DependencyObject obj, Brush value)
|
||||
{
|
||||
obj.SetValue(MouseOverForegroundProperty, value);
|
||||
}
|
||||
|
||||
public static readonly DependencyProperty MouseOverBorderBrushProperty = DependencyProperty.RegisterAttached(
|
||||
"MouseOverBorderBrush",
|
||||
typeof(Brush),
|
||||
typeof(Control),
|
||||
new FrameworkPropertyMetadata(
|
||||
Brushes.Transparent, FrameworkPropertyMetadataOptions.AffectsRender | FrameworkPropertyMetadataOptions.Inherits
|
||||
));
|
||||
|
||||
/// <summary>
|
||||
/// Gets the border brush for controls in mouse over.
|
||||
/// </summary>
|
||||
public static Brush GetMouseOverBorderBrush(DependencyObject obj)
|
||||
{
|
||||
return (Brush)obj.GetValue(MouseOverBorderBrushProperty);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Sets the border brush for controls in mouse over.
|
||||
/// </summary>
|
||||
public static void SetMouseOverBorderBrush(DependencyObject obj, Brush value)
|
||||
{
|
||||
obj.SetValue(MouseOverBorderBrushProperty, value);
|
||||
}
|
||||
|
||||
public static readonly DependencyProperty MouseOverBackgroundProperty = DependencyProperty.RegisterAttached(
|
||||
"MouseOverBackground",
|
||||
typeof(Brush),
|
||||
typeof(Control),
|
||||
new FrameworkPropertyMetadata(
|
||||
Brushes.Transparent, FrameworkPropertyMetadataOptions.AffectsRender | FrameworkPropertyMetadataOptions.Inherits
|
||||
));
|
||||
|
||||
/// <summary>
|
||||
/// Gets the background brush for controls in mouse over.
|
||||
/// </summary>
|
||||
public static Brush GetMouseOverBackground(DependencyObject obj)
|
||||
{
|
||||
return (Brush)obj.GetValue(MouseOverBackgroundProperty);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Sets the background brush for controls in mouse over.
|
||||
/// </summary>
|
||||
public static void SetMouseOverBackground(DependencyObject obj, Brush value)
|
||||
{
|
||||
obj.SetValue(MouseOverBackgroundProperty, value);
|
||||
}
|
||||
|
||||
public static readonly DependencyProperty FocusedForegroundProperty = DependencyProperty.RegisterAttached(
|
||||
"FocusedForeground",
|
||||
typeof(Brush),
|
||||
typeof(Control),
|
||||
new FrameworkPropertyMetadata(
|
||||
Brushes.Transparent, FrameworkPropertyMetadataOptions.AffectsRender | FrameworkPropertyMetadataOptions.Inherits
|
||||
));
|
||||
|
||||
/// <summary>
|
||||
/// Gets the foreground for controls in focused.
|
||||
/// </summary>
|
||||
public static Brush GetFocusedForeground(DependencyObject obj)
|
||||
{
|
||||
return (Brush)obj.GetValue(FocusedForegroundProperty);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets the foreground for controls in focused.
|
||||
/// </summary>
|
||||
public static void SetFocusedForeground(DependencyObject obj, Brush value)
|
||||
{
|
||||
obj.SetValue(FocusedForegroundProperty, value);
|
||||
}
|
||||
|
||||
public static readonly DependencyProperty FocusedBorderBrushProperty = DependencyProperty.RegisterAttached(
|
||||
"FocusedBorderBrush",
|
||||
typeof(Brush),
|
||||
typeof(Control),
|
||||
new FrameworkPropertyMetadata(
|
||||
Brushes.Transparent, FrameworkPropertyMetadataOptions.AffectsRender | FrameworkPropertyMetadataOptions.Inherits
|
||||
));
|
||||
|
||||
/// <summary>
|
||||
/// Gets the border brush for controls in focused.
|
||||
/// </summary>
|
||||
public static Brush GetFocusedBorderBrush(DependencyObject obj)
|
||||
{
|
||||
return (Brush)obj.GetValue(FocusedBorderBrushProperty);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Sets the border brush for controls in focused.
|
||||
/// </summary>
|
||||
public static void SetFocusedBorderBrush(DependencyObject obj, Brush value)
|
||||
{
|
||||
obj.SetValue(FocusedBorderBrushProperty, value);
|
||||
}
|
||||
|
||||
public static readonly DependencyProperty FocusedBackgroundProperty = DependencyProperty.RegisterAttached(
|
||||
"FocusedBackground",
|
||||
typeof(Brush),
|
||||
typeof(Control),
|
||||
new FrameworkPropertyMetadata(
|
||||
Brushes.Transparent, FrameworkPropertyMetadataOptions.AffectsRender | FrameworkPropertyMetadataOptions.Inherits
|
||||
));
|
||||
|
||||
/// <summary>
|
||||
/// Gets the background brush for controls in focused.
|
||||
/// </summary>
|
||||
public static Brush GetFocusedBackground(DependencyObject obj)
|
||||
{
|
||||
return (Brush)obj.GetValue(FocusedBackgroundProperty);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Sets the background brush for controls in focused.
|
||||
/// </summary>
|
||||
public static void SetFocusedBackground(DependencyObject obj, Brush value)
|
||||
{
|
||||
obj.SetValue(FocusedBackgroundProperty, value);
|
||||
}
|
||||
|
||||
public static readonly DependencyProperty PressedForegroundProperty = DependencyProperty.RegisterAttached(
|
||||
"PressedForeground",
|
||||
typeof(Brush),
|
||||
typeof(Control),
|
||||
new FrameworkPropertyMetadata(
|
||||
Brushes.Transparent, FrameworkPropertyMetadataOptions.AffectsRender | FrameworkPropertyMetadataOptions.Inherits
|
||||
));
|
||||
|
||||
/// <summary>
|
||||
/// Gets the foreground for controls in pressed.
|
||||
/// </summary>
|
||||
public static Brush GetPressedForeground(DependencyObject obj)
|
||||
{
|
||||
return (Brush)obj.GetValue(PressedForegroundProperty);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets the foreground for controls in pressed.
|
||||
/// </summary>
|
||||
public static void SetPressedForeground(DependencyObject obj, Brush value)
|
||||
{
|
||||
obj.SetValue(PressedForegroundProperty, value);
|
||||
}
|
||||
|
||||
public static readonly DependencyProperty PressedBorderBrushProperty = DependencyProperty.RegisterAttached(
|
||||
"PressedBorderBrush",
|
||||
typeof(Brush),
|
||||
typeof(Control),
|
||||
new FrameworkPropertyMetadata(
|
||||
Brushes.Transparent, FrameworkPropertyMetadataOptions.AffectsRender | FrameworkPropertyMetadataOptions.Inherits
|
||||
));
|
||||
|
||||
/// <summary>
|
||||
/// Gets the border brush for controls in pressed.
|
||||
/// </summary>
|
||||
public static Brush GetPressedBorderBrush(DependencyObject obj)
|
||||
{
|
||||
return (Brush)obj.GetValue(PressedBorderBrushProperty);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Sets the border brush for controls in pressed.
|
||||
/// </summary>
|
||||
public static void SetPressedBorderBrush(DependencyObject obj, Brush value)
|
||||
{
|
||||
obj.SetValue(PressedBorderBrushProperty, value);
|
||||
}
|
||||
|
||||
public static readonly DependencyProperty PressedBackgroundProperty = DependencyProperty.RegisterAttached(
|
||||
"PressedBackground",
|
||||
typeof(Brush),
|
||||
typeof(Control),
|
||||
new FrameworkPropertyMetadata(
|
||||
Brushes.Transparent, FrameworkPropertyMetadataOptions.AffectsRender | FrameworkPropertyMetadataOptions.Inherits
|
||||
));
|
||||
|
||||
/// <summary>
|
||||
/// Gets the background brush for controls in pressed.
|
||||
/// </summary>
|
||||
public static Brush GetPressedBackground(DependencyObject obj)
|
||||
{
|
||||
return (Brush)obj.GetValue(PressedBackgroundProperty);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Sets the background brush for controls in pressed.
|
||||
/// </summary>
|
||||
public static void SetPressedBackground(DependencyObject obj, Brush value)
|
||||
{
|
||||
obj.SetValue(PressedBackgroundProperty, value);
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
}
|
||||
}
|
||||
258
AntdWpf/Helpers/Input.cs
Normal file
258
AntdWpf/Helpers/Input.cs
Normal file
@@ -0,0 +1,258 @@
|
||||
namespace AntdWpf.Helpers
|
||||
{
|
||||
using System;
|
||||
using System.Linq;
|
||||
using System.Windows;
|
||||
using System.Windows.Controls;
|
||||
using System.Windows.Controls.Primitives;
|
||||
using System.Windows.Input;
|
||||
using System.Windows.Media;
|
||||
|
||||
public static class Input
|
||||
{
|
||||
#region Attached Properties
|
||||
|
||||
public static readonly DependencyProperty PlaceholderProperty =
|
||||
DependencyProperty.RegisterAttached("Placeholder", typeof(string), typeof(Input), new PropertyMetadata(string.Empty));
|
||||
|
||||
/// <summary>
|
||||
/// Gets the placeholder for the input control.
|
||||
/// </summary>
|
||||
[AttachedPropertyBrowsableForType(typeof(TextBoxBase))]
|
||||
[AttachedPropertyBrowsableForType(typeof(PasswordBox))]
|
||||
[AttachedPropertyBrowsableForType(typeof(ComboBox))]
|
||||
public static string GetPlaceholder(DependencyObject obj)
|
||||
{
|
||||
return (string)obj.GetValue(PlaceholderProperty);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Sets the placeholder for the input control.
|
||||
/// </summary>
|
||||
public static void SetPlaceholder(DependencyObject obj, string value)
|
||||
{
|
||||
obj.SetValue(PlaceholderProperty, value);
|
||||
}
|
||||
|
||||
public static readonly DependencyProperty PlaceholderBrushProperty =
|
||||
DependencyProperty.RegisterAttached("PlaceholderBrush", typeof(Brush), typeof(Input),
|
||||
new FrameworkPropertyMetadata(
|
||||
Brushes.Silver,
|
||||
FrameworkPropertyMetadataOptions.AffectsRender |
|
||||
FrameworkPropertyMetadataOptions.SubPropertiesDoNotAffectRender |
|
||||
FrameworkPropertyMetadataOptions.Inherits));
|
||||
|
||||
/// <summary>
|
||||
/// Gets the placeholder foreground brush for the input control.
|
||||
/// </summary>
|
||||
[AttachedPropertyBrowsableForType(typeof(TextBoxBase))]
|
||||
[AttachedPropertyBrowsableForType(typeof(PasswordBox))]
|
||||
[AttachedPropertyBrowsableForType(typeof(ComboBox))]
|
||||
public static Brush GetPlaceholderBrush(DependencyObject obj)
|
||||
{
|
||||
return (Brush)obj.GetValue(PlaceholderBrushProperty);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Sets the placeholder foreground brush for the input control.
|
||||
/// </summary>
|
||||
public static void SetPlaceholderBrush(DependencyObject obj, Brush value)
|
||||
{
|
||||
obj.SetValue(PlaceholderBrushProperty, value);
|
||||
}
|
||||
|
||||
public static readonly DependencyProperty PrefixProperty =
|
||||
DependencyProperty.RegisterAttached("Prefix", typeof(object), typeof(Input), new PropertyMetadata(null));
|
||||
|
||||
/// <summary>
|
||||
/// Gets the prefix object of the input control.
|
||||
/// </summary>
|
||||
[AttachedPropertyBrowsableForType(typeof(TextBox))]
|
||||
[AttachedPropertyBrowsableForType(typeof(PasswordBox))]
|
||||
[AttachedPropertyBrowsableForType(typeof(ComboBox))]
|
||||
public static object GetPrefix(DependencyObject obj)
|
||||
{
|
||||
return obj.GetValue(PrefixProperty);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Sets the prefix object of the input control.
|
||||
/// </summary>
|
||||
public static void SetPrefix(DependencyObject obj, object value)
|
||||
{
|
||||
obj.SetValue(PrefixProperty, value);
|
||||
}
|
||||
|
||||
public static readonly DependencyProperty SuffixProperty =
|
||||
DependencyProperty.RegisterAttached("Suffix", typeof(object), typeof(Input), new PropertyMetadata(null));
|
||||
|
||||
/// <summary>
|
||||
/// Gets the suffix object of the input control.
|
||||
/// </summary>
|
||||
[AttachedPropertyBrowsableForType(typeof(TextBox))]
|
||||
[AttachedPropertyBrowsableForType(typeof(PasswordBox))]
|
||||
public static object GetSuffix(DependencyObject obj)
|
||||
{
|
||||
return obj.GetValue(SuffixProperty);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Sets the suffix object of the input control.
|
||||
/// </summary>
|
||||
public static void SetSuffix(DependencyObject obj, object value)
|
||||
{
|
||||
obj.SetValue(SuffixProperty, value);
|
||||
}
|
||||
|
||||
public static readonly DependencyProperty PasswordProperty =
|
||||
DependencyProperty.RegisterAttached("Password", typeof(string), typeof(Input),
|
||||
new FrameworkPropertyMetadata(string.Empty, FrameworkPropertyMetadataOptions.BindsTwoWayByDefault, new PropertyChangedCallback(OnPasswordChanged)));
|
||||
|
||||
private static void OnPasswordChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
|
||||
{
|
||||
if (d is PasswordBox passwordBox)
|
||||
{
|
||||
var newVal = (string)e.NewValue;
|
||||
|
||||
// Sync password
|
||||
if (newVal != passwordBox.Password)
|
||||
{
|
||||
passwordBox.Password = newVal;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets the password currently held by the PasswordBox.
|
||||
/// </summary>
|
||||
[AttachedPropertyBrowsableForType(typeof(PasswordBox))]
|
||||
public static string GetPassword(DependencyObject obj)
|
||||
{
|
||||
return (string)obj.GetValue(PasswordProperty);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Sets the password currently held by the PasswordBox.
|
||||
/// </summary>
|
||||
public static void SetPassword(DependencyObject obj, string value)
|
||||
{
|
||||
obj.SetValue(PasswordProperty, value);
|
||||
}
|
||||
|
||||
public static readonly DependencyProperty ClearableProperty =
|
||||
DependencyProperty.RegisterAttached("Clearable", typeof(bool), typeof(Input), new PropertyMetadata(false));
|
||||
|
||||
/// <summary>
|
||||
/// Gets whether the input control has a clear behavior.
|
||||
/// </summary>
|
||||
[AttachedPropertyBrowsableForType(typeof(TextBox))]
|
||||
[AttachedPropertyBrowsableForType(typeof(PasswordBox))]
|
||||
[AttachedPropertyBrowsableForType(typeof(ComboBox))]
|
||||
public static bool GetClearable(DependencyObject obj)
|
||||
{
|
||||
return (bool)obj.GetValue(ClearableProperty);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Sets whether the input control has a clear behavior.
|
||||
/// </summary>
|
||||
public static void SetClearable(DependencyObject obj, bool value)
|
||||
{
|
||||
obj.SetValue(ClearableProperty, value);
|
||||
}
|
||||
|
||||
public static readonly DependencyProperty EyeableProperty =
|
||||
DependencyProperty.RegisterAttached("Eyeable", typeof(bool), typeof(Input), new PropertyMetadata(false));
|
||||
|
||||
/// <summary>
|
||||
/// Gets whether the password currently held by PasswordBox is displayed in text.
|
||||
/// </summary>
|
||||
[AttachedPropertyBrowsableForType(typeof(PasswordBox))]
|
||||
public static bool GetEyeable(DependencyObject obj)
|
||||
{
|
||||
return (bool)obj.GetValue(EyeableProperty);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Sets whether the password currently held by PasswordBox is displayed in text.
|
||||
/// </summary>
|
||||
public static void SetEyeable(DependencyObject obj, bool value)
|
||||
{
|
||||
obj.SetValue(EyeableProperty, value);
|
||||
}
|
||||
|
||||
public static readonly DependencyProperty ClearEnabledProperty =
|
||||
DependencyProperty.RegisterAttached("ClearEnabled", typeof(bool), typeof(Input), new PropertyMetadata(false, OnClearEnabled));
|
||||
|
||||
/// <summary>
|
||||
/// Gets the clear enable state of the control.
|
||||
/// </summary>
|
||||
public static bool GetClearEnabled(DependencyObject obj)
|
||||
{
|
||||
return (bool)obj.GetValue(ClearEnabledProperty);
|
||||
}
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// Sets the clear enable state of the control.
|
||||
/// </summary>
|
||||
public static void SetClearEnabled(DependencyObject obj, bool value)
|
||||
{
|
||||
obj.SetValue(ClearEnabledProperty, value);
|
||||
}
|
||||
|
||||
private static void OnClearEnabled(DependencyObject d, DependencyPropertyChangedEventArgs e)
|
||||
{
|
||||
if (d is UIElement)
|
||||
{
|
||||
if ((bool)e.NewValue)
|
||||
{
|
||||
((UIElement)d).MouseLeftButtonUp += OnClear;
|
||||
}
|
||||
else
|
||||
{
|
||||
((UIElement)d).MouseLeftButtonUp -= OnClear;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Private Methods
|
||||
|
||||
private static void OnClear(object sender, RoutedEventArgs e)
|
||||
{
|
||||
if (sender is DependencyObject d)
|
||||
{
|
||||
var parent = d.GetAncestors().FirstOrDefault(a => a is TextBox || a is PasswordBox || a is ComboBox);
|
||||
|
||||
if (GetClearable(parent))
|
||||
{
|
||||
if (parent is TextBox)
|
||||
{
|
||||
((TextBox)parent).Clear();
|
||||
((TextBox)parent).GetBindingExpression(TextBox.TextProperty)?.UpdateSource();
|
||||
}
|
||||
else if (parent is PasswordBox)
|
||||
{
|
||||
((PasswordBox)parent).Clear();
|
||||
((PasswordBox)parent).GetBindingExpression(PasswordProperty)?.UpdateSource();
|
||||
}
|
||||
else if (parent is ComboBox)
|
||||
{
|
||||
if (((ComboBox)parent).IsEditable)
|
||||
{
|
||||
((ComboBox)parent).Text = string.Empty;
|
||||
((ComboBox)parent).GetBindingExpression(ComboBox.TextProperty)?.UpdateSource();
|
||||
}
|
||||
|
||||
((ComboBox)parent).SelectedItem = null;
|
||||
((ComboBox)parent).GetBindingExpression(Selector.SelectedItemProperty)?.UpdateSource();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#endregion
|
||||
}
|
||||
}
|
||||
33
AntdWpf/Helpers/ProgressAssist.cs
Normal file
33
AntdWpf/Helpers/ProgressAssist.cs
Normal file
@@ -0,0 +1,33 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using System.Windows.Controls;
|
||||
using System.Windows.Media.Animation;
|
||||
using System.Windows;
|
||||
|
||||
namespace AntdWpf.Helpers
|
||||
{
|
||||
public class ProgressAssist
|
||||
{
|
||||
public static double GetSmoothValue(DependencyObject obj)
|
||||
{
|
||||
return (double)obj.GetValue(SmoothValueProperty);
|
||||
}
|
||||
|
||||
public static void SetSmoothValue(DependencyObject obj, double value)
|
||||
{
|
||||
obj.SetValue(SmoothValueProperty, value);
|
||||
}
|
||||
|
||||
public static readonly DependencyProperty SmoothValueProperty =
|
||||
DependencyProperty.RegisterAttached("SmoothValue", typeof(double), typeof(ProgressAssist), new PropertyMetadata(0.0, changing));
|
||||
|
||||
private static void changing(DependencyObject d, DependencyPropertyChangedEventArgs e)
|
||||
{
|
||||
var anim = new DoubleAnimation((double)e.OldValue, (double)e.NewValue, new TimeSpan(0, 0, 0, 0, 300));
|
||||
(d as ProgressBar).BeginAnimation(System.Windows.Controls.Primitives.RangeBase.ValueProperty, anim, HandoffBehavior.Compose);
|
||||
}
|
||||
}
|
||||
}
|
||||
246
AntdWpf/Helpers/TreeHelper.cs
Normal file
246
AntdWpf/Helpers/TreeHelper.cs
Normal file
@@ -0,0 +1,246 @@
|
||||
namespace AntdWpf.Helpers
|
||||
{
|
||||
using System.Collections.Generic;
|
||||
using System.Windows;
|
||||
using System.Windows.Media;
|
||||
using System.Windows.Media.Media3D;
|
||||
|
||||
/// <summary>
|
||||
/// Helper methods for UI-related tasks.
|
||||
/// This class was obtained from Philip Sumi (a fellow WPF Disciples blog)
|
||||
/// http://www.hardcodet.net/uploads/2009/06/UIHelper.cs
|
||||
/// </summary>
|
||||
public static class TreeHelper
|
||||
{
|
||||
#region Find Parent
|
||||
|
||||
/// <summary>
|
||||
/// Finds a parent of a given item on the visual tree.
|
||||
/// </summary>
|
||||
/// <typeparam name="T">The type of the queried item.</typeparam>
|
||||
/// <param name="child">A direct or indirect child of the
|
||||
/// queried item.</param>
|
||||
/// <returns>The first parent item that matches the submitted
|
||||
/// type parameter. If not matching item can be found, a null
|
||||
/// reference is being returned.</returns>
|
||||
public static T TryFindParent<T>(this DependencyObject child)
|
||||
where T : DependencyObject
|
||||
{
|
||||
//get parent item
|
||||
var parentObject = child.GetParentObject();
|
||||
|
||||
//we've reached the end of the tree
|
||||
if (parentObject == null) return null;
|
||||
|
||||
//check if the parent matches the type we're looking for
|
||||
if (parentObject is T parent)
|
||||
{
|
||||
return parent;
|
||||
}
|
||||
else
|
||||
{
|
||||
//use recursion to proceed with next level
|
||||
return parentObject.TryFindParent<T>();
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// This method is an alternative to WPF's
|
||||
/// <see cref="VisualTreeHelper.GetParent"/> method, which also
|
||||
/// supports content elements. Keep in mind that for content element,
|
||||
/// this method falls back to the logical tree of the element!
|
||||
/// </summary>
|
||||
/// <param name="child">The item to be processed.</param>
|
||||
/// <returns>The submitted item's parent, if available. Otherwise
|
||||
/// null.</returns>
|
||||
public static DependencyObject GetParentObject(this DependencyObject child)
|
||||
{
|
||||
if (child == null) return null;
|
||||
|
||||
//handle content elements separately
|
||||
if (child is ContentElement contentElement)
|
||||
{
|
||||
var parent = ContentOperations.GetParent(contentElement);
|
||||
if (parent != null) return parent;
|
||||
|
||||
return contentElement is FrameworkContentElement fce ? fce.Parent : null;
|
||||
}
|
||||
|
||||
//also try searching for parent in framework elements (such as DockPanel, etc)
|
||||
if (child is FrameworkElement frameworkElement)
|
||||
{
|
||||
var parent = frameworkElement.Parent;
|
||||
if (parent != null) return parent;
|
||||
}
|
||||
|
||||
//if it's not a ContentElement/FrameworkElement, rely on VisualTreeHelper
|
||||
return VisualTreeHelper.GetParent(child);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Finds all Ancestors of a given item on the visual tree.
|
||||
/// </summary>
|
||||
/// <param name="child">A node in a visual tree</param>
|
||||
/// <returns>All ancestors in visual tree of <paramref name="child"/> element</returns>
|
||||
public static IEnumerable<DependencyObject> GetAncestors(this DependencyObject child)
|
||||
{
|
||||
var parent = VisualTreeHelper.GetParent(child);
|
||||
while (parent != null)
|
||||
{
|
||||
yield return parent;
|
||||
parent = VisualTreeHelper.GetParent(parent);
|
||||
}
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Find children
|
||||
|
||||
/// <summary>
|
||||
/// Finds a Child of a given item in the visual tree.
|
||||
/// </summary>
|
||||
/// <param name="parent">A direct parent of the queried item.</param>
|
||||
/// <typeparam name="T">The type of the queried item.</typeparam>
|
||||
/// <param name="childName">x:Name or Name of child. </param>
|
||||
/// <returns>The first parent item that matches the submitted type parameter.
|
||||
/// If not matching item can be found,
|
||||
/// a null parent is being returned.</returns>
|
||||
public static T FindChild<T>(this DependencyObject parent, string childName)
|
||||
where T : DependencyObject
|
||||
{
|
||||
// Confirm parent and childName are valid.
|
||||
if (parent == null) return null;
|
||||
|
||||
T foundChild = null;
|
||||
|
||||
var childrenCount = VisualTreeHelper.GetChildrenCount(parent);
|
||||
for (var i = 0; i < childrenCount; i++)
|
||||
{
|
||||
var child = VisualTreeHelper.GetChild(parent, i);
|
||||
// If the child is not of the request child type child
|
||||
var childType = child as T;
|
||||
if (childType == null)
|
||||
{
|
||||
// recursively drill down the tree
|
||||
foundChild = child.FindChild<T>(childName);
|
||||
// If the child is found, break so we do not overwrite the found child.
|
||||
if (foundChild != null) break;
|
||||
}
|
||||
else if (!string.IsNullOrEmpty(childName))
|
||||
{
|
||||
var frameworkInputElement = child as IFrameworkInputElement;
|
||||
// If the child's name is set for search
|
||||
if (frameworkInputElement != null && frameworkInputElement.Name == childName)
|
||||
{
|
||||
// if the child's name is of the request name
|
||||
foundChild = (T)child;
|
||||
break;
|
||||
}
|
||||
else
|
||||
{
|
||||
// recursively drill down the tree
|
||||
foundChild = child.FindChild<T>(childName);
|
||||
// If the child is found, break so we do not overwrite the found child.
|
||||
if (foundChild != null) break;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
// child element found.
|
||||
foundChild = (T)child;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
return foundChild;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Analyzes both visual and logical tree in order to find all elements of a given
|
||||
/// type that are descendants of the <paramref name="source"/> item.
|
||||
/// </summary>
|
||||
/// <typeparam name="T">The type of the queried items.</typeparam>
|
||||
/// <param name="source">The root element that marks the source of the search. If the
|
||||
/// source is already of the requested type, it will not be included in the result.</param>
|
||||
/// <returns>All descendants of <paramref name="source"/> that match the requested type.</returns>
|
||||
public static IEnumerable<T> FindChildren<T>(this DependencyObject source) where T : DependencyObject
|
||||
{
|
||||
if (source != null)
|
||||
{
|
||||
var childs = source.GetChildObjects();
|
||||
foreach (var child in childs)
|
||||
{
|
||||
//analyze if children match the requested type
|
||||
if (child != null && child is T)
|
||||
{
|
||||
yield return (T)child;
|
||||
}
|
||||
|
||||
//recurse tree
|
||||
foreach (var descendant in child.FindChildren<T>())
|
||||
{
|
||||
yield return descendant;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// This method is an alternative to WPF's
|
||||
/// <see cref="VisualTreeHelper.GetChild"/> method, which also
|
||||
/// supports content elements. Keep in mind that for content elements,
|
||||
/// this method falls back to the logical tree of the element.
|
||||
/// </summary>
|
||||
/// <param name="parent">The item to be processed.</param>
|
||||
/// <param name="forceUsingTheVisualTreeHelper">Sometimes it's better to search in the VisualTree (e.g. in tests)</param>
|
||||
/// <returns>The submitted item's child elements, if available.</returns>
|
||||
public static IEnumerable<DependencyObject> GetChildObjects(this DependencyObject parent, bool forceUsingTheVisualTreeHelper = false)
|
||||
{
|
||||
if (parent == null) yield break;
|
||||
|
||||
if (!forceUsingTheVisualTreeHelper && (parent is ContentElement || parent is FrameworkElement))
|
||||
{
|
||||
//use the logical tree for content / framework elements
|
||||
foreach (var obj in LogicalTreeHelper.GetChildren(parent))
|
||||
{
|
||||
var depObj = obj as DependencyObject;
|
||||
if (depObj != null) yield return (DependencyObject)obj;
|
||||
}
|
||||
}
|
||||
else if (parent is Visual || parent is Visual3D)
|
||||
{
|
||||
//use the visual tree per default
|
||||
var count = VisualTreeHelper.GetChildrenCount(parent);
|
||||
for (var i = 0; i < count; i++)
|
||||
{
|
||||
yield return VisualTreeHelper.GetChild(parent, i);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Find From Point
|
||||
|
||||
/// <summary>
|
||||
/// Tries to locate a given item within the visual tree,
|
||||
/// starting with the dependency object at a given position.
|
||||
/// </summary>
|
||||
/// <typeparam name="T">The type of the element to be found
|
||||
/// on the visual tree of the element at the given location.</typeparam>
|
||||
/// <param name="reference">The main element which is used to perform
|
||||
/// hit testing.</param>
|
||||
/// <param name="point">The position to be evaluated on the origin.</param>
|
||||
public static T TryFindFromPoint<T>(UIElement reference, Point point)
|
||||
where T : DependencyObject
|
||||
{
|
||||
|
||||
if (!(reference.InputHitTest(point) is DependencyObject element)) return null;
|
||||
else if (element is T) return (T)element;
|
||||
else return element.TryFindParent<T>();
|
||||
}
|
||||
|
||||
#endregion
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user