mirror of
https://github.com/VitalickS/BrightSharp.Toolkit.git
synced 2026-03-21 10:21:16 +00:00
.net copy
This commit is contained in:
168
BrightSharp.NET/Extensions/MarkupExtensionProperties.cs
Normal file
168
BrightSharp.NET/Extensions/MarkupExtensionProperties.cs
Normal file
@@ -0,0 +1,168 @@
|
||||
using BrightSharp.Behaviors;
|
||||
using System;
|
||||
using System.Linq;
|
||||
using System.Windows;
|
||||
using System.Windows.Controls;
|
||||
using System.Windows.Media;
|
||||
|
||||
namespace BrightSharp.Extensions
|
||||
{
|
||||
public static class MarkupExtensionProperties
|
||||
{
|
||||
public static CornerRadius GetCornerRadius(DependencyObject obj)
|
||||
{
|
||||
return (CornerRadius)obj.GetValue(CornerRadiusProperty);
|
||||
}
|
||||
public static void SetCornerRadius(DependencyObject obj, CornerRadius value)
|
||||
{
|
||||
obj.SetValue(CornerRadiusProperty, value);
|
||||
}
|
||||
public static object GetHeader(DependencyObject obj)
|
||||
{
|
||||
return obj.GetValue(HeaderProperty);
|
||||
}
|
||||
public static void SetHeader(DependencyObject obj, object value)
|
||||
{
|
||||
obj.SetValue(HeaderProperty, value);
|
||||
}
|
||||
public static object GetLeadingElement(DependencyObject obj)
|
||||
{
|
||||
return obj.GetValue(CornerRadiusProperty);
|
||||
}
|
||||
public static void SetLeadingElement(DependencyObject obj, object value)
|
||||
{
|
||||
obj.SetValue(CornerRadiusProperty, value);
|
||||
}
|
||||
public static object GetTrailingElement(DependencyObject obj)
|
||||
{
|
||||
return obj.GetValue(CornerRadiusProperty);
|
||||
}
|
||||
public static void SetTrailingElement(DependencyObject obj, object value)
|
||||
{
|
||||
obj.SetValue(CornerRadiusProperty, value);
|
||||
}
|
||||
public static object GetIcon(DependencyObject obj)
|
||||
{
|
||||
return obj.GetValue(IconProperty);
|
||||
}
|
||||
public static void SetIcon(DependencyObject obj, object value)
|
||||
{
|
||||
obj.SetValue(IconProperty, value);
|
||||
}
|
||||
public static void SetHeaderHeight(DependencyObject obj, double value)
|
||||
{
|
||||
obj.SetValue(HeaderHeightProperty, value);
|
||||
}
|
||||
public static double GetHeaderHeight(DependencyObject obj)
|
||||
{
|
||||
return (double)obj.GetValue(HeaderHeightProperty);
|
||||
}
|
||||
|
||||
public static void SetHeaderVerticalAlignment(DependencyObject obj, VerticalAlignment value)
|
||||
{
|
||||
obj.SetValue(HeaderVerticalAlignmentProperty, value);
|
||||
}
|
||||
public static VerticalAlignment GetHeaderVerticalAlignment(DependencyObject obj)
|
||||
{
|
||||
return (VerticalAlignment)obj.GetValue(HeaderVerticalAlignmentProperty);
|
||||
}
|
||||
public static void SetHeaderHorizontalAlignment(DependencyObject obj, HorizontalAlignment value)
|
||||
{
|
||||
obj.SetValue(HeaderHorizontalAlignmentProperty, value);
|
||||
}
|
||||
public static HorizontalAlignment GetHeaderHorizontalAlignment(DependencyObject obj)
|
||||
{
|
||||
return (HorizontalAlignment)obj.GetValue(HeaderHorizontalAlignmentProperty);
|
||||
}
|
||||
public static void SetSpecialHeight(DependencyObject obj, double value)
|
||||
{
|
||||
obj.SetValue(SpecialHeightProperty, value);
|
||||
}
|
||||
public static double GetSpecialHeight(DependencyObject obj)
|
||||
{
|
||||
return (double)obj.GetValue(SpecialHeightProperty);
|
||||
}
|
||||
public static void SetSpecialWidth(DependencyObject obj, double value)
|
||||
{
|
||||
obj.SetValue(SpecialWidthProperty, value);
|
||||
}
|
||||
public static double GetSpecialWidth(DependencyObject obj)
|
||||
{
|
||||
return (double)obj.GetValue(SpecialWidthProperty);
|
||||
}
|
||||
public static Dock GetDocking(DependencyObject obj)
|
||||
{
|
||||
return (Dock)obj.GetValue(DockingProperty);
|
||||
}
|
||||
public static void SetDocking(DependencyObject obj, Dock value)
|
||||
{
|
||||
obj.SetValue(DockingProperty, value);
|
||||
}
|
||||
public static Thickness GetHeaderPadding(DependencyObject obj)
|
||||
{
|
||||
return (Thickness)obj.GetValue(HeaderPaddingProperty);
|
||||
}
|
||||
public static void SetHeaderPadding(DependencyObject obj, Thickness value)
|
||||
{
|
||||
obj.SetValue(HeaderPaddingProperty, value);
|
||||
}
|
||||
public static bool GetIsDragHelperVisible(DependencyObject obj)
|
||||
{
|
||||
return (bool)obj.GetValue(IsDragHelperVisibleProperty);
|
||||
}
|
||||
public static void SetIsDragHelperVisible(DependencyObject obj, bool value)
|
||||
{
|
||||
obj.SetValue(IsDragHelperVisibleProperty, value);
|
||||
}
|
||||
public static Brush GetSpecialBrush(DependencyObject obj)
|
||||
{
|
||||
return (Brush)obj.GetValue(SpecialBrushProperty);
|
||||
}
|
||||
public static void SetSpecialBrush(DependencyObject obj, Brush value)
|
||||
{
|
||||
obj.SetValue(SpecialBrushProperty, value);
|
||||
}
|
||||
|
||||
public static bool GetUseMinMaxSizeBehavior(Window obj)
|
||||
{
|
||||
return (bool)obj.GetValue(UseMinMaxSizeBehaviorProperty);
|
||||
}
|
||||
|
||||
public static void SetUseMinMaxSizeBehavior(Window obj, bool value)
|
||||
{
|
||||
obj.SetValue(UseMinMaxSizeBehaviorProperty, value);
|
||||
}
|
||||
|
||||
public static readonly DependencyProperty SpecialBrushProperty = DependencyProperty.RegisterAttached("SpecialBrush", typeof(Brush), typeof(MarkupExtensionProperties), new PropertyMetadata(null));
|
||||
public static readonly DependencyProperty IsDragHelperVisibleProperty = DependencyProperty.RegisterAttached("IsDragHelperVisible", typeof(bool), typeof(MarkupExtensionProperties), new FrameworkPropertyMetadata(true));
|
||||
public static readonly DependencyProperty HeaderPaddingProperty = DependencyProperty.RegisterAttached("HeaderPadding", typeof(Thickness), typeof(MarkupExtensionProperties), new PropertyMetadata(null));
|
||||
public static readonly DependencyProperty CornerRadiusProperty = DependencyProperty.RegisterAttached("CornerRadius", typeof(CornerRadius), typeof(MarkupExtensionProperties), new PropertyMetadata(null));
|
||||
public static readonly DependencyProperty LeadingElementProperty = DependencyProperty.RegisterAttached("LeadingElement", typeof(object), typeof(MarkupExtensionProperties), new PropertyMetadata(null));
|
||||
public static readonly DependencyProperty TrailingElementProperty = DependencyProperty.RegisterAttached("TrailingElement", typeof(object), typeof(MarkupExtensionProperties), new PropertyMetadata(null));
|
||||
public static readonly DependencyProperty HeaderProperty = DependencyProperty.RegisterAttached("Header", typeof(object), typeof(MarkupExtensionProperties), new FrameworkPropertyMetadata(null));
|
||||
public static readonly DependencyProperty DockingProperty = DependencyProperty.RegisterAttached("Docking", typeof(Dock), typeof(MarkupExtensionProperties), new PropertyMetadata(null));
|
||||
public static readonly DependencyProperty IconProperty = DependencyProperty.RegisterAttached("Icon", typeof(object), typeof(MarkupExtensionProperties), new PropertyMetadata(null));
|
||||
public static readonly DependencyProperty HeaderHeightProperty = DependencyProperty.RegisterAttached("HeaderHeight", typeof(double), typeof(MarkupExtensionProperties), new PropertyMetadata(null));
|
||||
public static readonly DependencyProperty HeaderVerticalAlignmentProperty = DependencyProperty.RegisterAttached("HeaderVerticalAlignment", typeof(VerticalAlignment), typeof(MarkupExtensionProperties), new PropertyMetadata(VerticalAlignment.Center));
|
||||
public static readonly DependencyProperty HeaderHorizontalAlignmentProperty = DependencyProperty.RegisterAttached("HeaderHorizontalAlignment", typeof(HorizontalAlignment), typeof(MarkupExtensionProperties), new PropertyMetadata(HorizontalAlignment.Left));
|
||||
public static readonly DependencyProperty SpecialHeightProperty = DependencyProperty.RegisterAttached("SpecialHeight", typeof(double), typeof(MarkupExtensionProperties), new FrameworkPropertyMetadata(0.0, FrameworkPropertyMetadataOptions.Inherits | FrameworkPropertyMetadataOptions.AffectsRender | FrameworkPropertyMetadataOptions.AffectsMeasure));
|
||||
public static readonly DependencyProperty SpecialWidthProperty = DependencyProperty.RegisterAttached("SpecialWidth", typeof(double), typeof(MarkupExtensionProperties), new FrameworkPropertyMetadata(0.0, FrameworkPropertyMetadataOptions.Inherits | FrameworkPropertyMetadataOptions.AffectsRender | FrameworkPropertyMetadataOptions.AffectsMeasure));
|
||||
public static readonly DependencyProperty UseMinMaxSizeBehaviorProperty = DependencyProperty.RegisterAttached("UseMinMaxSizeBehavior", typeof(bool), typeof(MarkupExtensionProperties), new PropertyMetadata(false, UseMinMaxSizeBehaviorChanged));
|
||||
|
||||
|
||||
private static void UseMinMaxSizeBehaviorChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
|
||||
{
|
||||
var window = d as Window;
|
||||
if (window == null) return;
|
||||
if ((bool)e.NewValue)
|
||||
{
|
||||
new MinMaxSize_Logic(window).OnAttached();
|
||||
}
|
||||
else
|
||||
{
|
||||
// Not supported yet
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
142
BrightSharp.NET/Extensions/WpfExtensions.cs
Normal file
142
BrightSharp.NET/Extensions/WpfExtensions.cs
Normal file
@@ -0,0 +1,142 @@
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Windows;
|
||||
using System.Windows.Controls;
|
||||
using System.Windows.Media;
|
||||
using System.Windows.Media.Media3D;
|
||||
|
||||
namespace BrightSharp.Extensions
|
||||
{
|
||||
public static class WpfExtensions
|
||||
{
|
||||
/// <summary>
|
||||
/// Returns Parent item of DependencyObject
|
||||
/// </summary>
|
||||
/// <param name="obj">Child object</param>
|
||||
/// <param name="useLogicalTree">Prefer LogicalTree when need.</param>
|
||||
/// <returns>Found item</returns>
|
||||
public static DependencyObject GetParent(this DependencyObject obj, bool useLogicalTree = false)
|
||||
{
|
||||
if (!useLogicalTree && (obj is Visual || obj is Visual3D))
|
||||
return VisualTreeHelper.GetParent(obj) ?? LogicalTreeHelper.GetParent(obj);
|
||||
else
|
||||
return LogicalTreeHelper.GetParent(obj);
|
||||
}
|
||||
|
||||
public static IEnumerable<T> GetAncestors<T>(this DependencyObject obj, bool useLogicalTree = false) where T : DependencyObject
|
||||
{
|
||||
if (obj == null) yield break;
|
||||
|
||||
var x = GetParent(obj, useLogicalTree);
|
||||
|
||||
while (x != null && !(x is T))
|
||||
{
|
||||
x = GetParent(x, useLogicalTree);
|
||||
}
|
||||
if (x != null)
|
||||
yield return x as T;
|
||||
else
|
||||
yield break;
|
||||
foreach (var item in GetAncestors<T>(x, useLogicalTree))
|
||||
{
|
||||
yield return item;
|
||||
}
|
||||
}
|
||||
|
||||
public static T FindAncestor<T>(this DependencyObject obj) where T : DependencyObject
|
||||
{
|
||||
return GetAncestors<T>(obj).FirstOrDefault();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Try find ItemsPanel of ItemsControl
|
||||
/// </summary>
|
||||
/// <param name="itemsControl">Where to search</param>
|
||||
/// <returns>Panel of ItemsControl</returns>
|
||||
public static Panel GetItemsPanel(DependencyObject itemsControl)
|
||||
{
|
||||
if (itemsControl is Panel) return (Panel)itemsControl;
|
||||
ItemsPresenter itemsPresenter = FindVisualChildren<ItemsPresenter>(itemsControl).FirstOrDefault();
|
||||
if (itemsPresenter == null) return null;
|
||||
var itemsPanel = VisualTreeHelper.GetChild(itemsPresenter, 0) as Panel;
|
||||
return itemsPanel;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Check if object is valid (no errors found in logical children)
|
||||
/// </summary>
|
||||
/// <param name="obj">Object to search</param>
|
||||
/// <returns>True if no errors found</returns>
|
||||
public static bool IsValid(this DependencyObject obj) {
|
||||
if (obj == null) return true;
|
||||
if (Validation.GetHasError(obj)) return false;
|
||||
foreach (var child in LogicalTreeHelper.GetChildren(obj).OfType<DependencyObject>()) {
|
||||
if (child == null) continue;
|
||||
if (child is UIElement ui) {
|
||||
if (!ui.IsVisible || !ui.IsEnabled) continue;
|
||||
}
|
||||
if (!IsValid(child)) return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Search all textboxes to update invalid with UpdateSource(). For ex. when need update validation messages.
|
||||
/// </summary>
|
||||
/// <param name="obj">Object where to search TextBoxes</param>
|
||||
public static void UpdateSources(this DependencyObject obj) {
|
||||
if (obj == null) return;
|
||||
if (Validation.GetHasError(obj)) {
|
||||
//TODO Any types?
|
||||
if (obj is TextBox tb) tb.GetBindingExpression(TextBox.TextProperty)?.UpdateSource();
|
||||
}
|
||||
foreach (var item in FindLogicalChildren<DependencyObject>(obj)) {
|
||||
UpdateSources(item);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Find all visual children of type T
|
||||
/// </summary>
|
||||
/// <typeparam name="T">Type to search</typeparam>
|
||||
/// <param name="depObj">Object where to search</param>
|
||||
/// <returns>Enumerator for items</returns>
|
||||
public static IEnumerable<T> FindVisualChildren<T>(this DependencyObject depObj) where T : DependencyObject {
|
||||
if (depObj != null && (depObj is Visual || depObj is Visual3D)) {
|
||||
for (int i = 0; i < VisualTreeHelper.GetChildrenCount(depObj); i++) {
|
||||
DependencyObject child = VisualTreeHelper.GetChild(depObj, i);
|
||||
if (child != null && child is T) {
|
||||
yield return (T)child;
|
||||
}
|
||||
|
||||
foreach (T childOfChild in FindVisualChildren<T>(child)) {
|
||||
yield return childOfChild;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Find all logical children of type T
|
||||
/// </summary>
|
||||
/// <typeparam name="T">Type to search</typeparam>
|
||||
/// <param name="depObj">Object where to search</param>
|
||||
/// <returns>Enumerator for items</returns>
|
||||
public static IEnumerable<T> FindLogicalChildren<T>(this DependencyObject depObj) where T : DependencyObject {
|
||||
if (depObj != null) {
|
||||
foreach (object rawChild in LogicalTreeHelper.GetChildren(depObj)) {
|
||||
if (rawChild is DependencyObject child) {
|
||||
if (child is T) {
|
||||
yield return (T)child;
|
||||
}
|
||||
|
||||
foreach (T childOfChild in FindLogicalChildren<T>(child)) {
|
||||
yield return childOfChild;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user