Merge branch 'master' into MenuItemAttach

This commit is contained in:
NaBian authored and GitHub committed 2023-09-28 21:14:31 +08:00
commit 60b359ec20
1325 files changed
+85761 -74012

No files matched your search

@@ -1,22 +1,21 @@
using System.Windows;
using System.Windows.Media;
namespace HandyControl.Controls
namespace HandyControl.Controls;
public class BackgroundSwitchElement
{
public class BackgroundSwitchElement
{
public static readonly DependencyProperty MouseHoverBackgroundProperty = DependencyProperty.RegisterAttached(
"MouseHoverBackground", typeof(Brush), typeof(BackgroundSwitchElement), new FrameworkPropertyMetadata(Brushes.Transparent, FrameworkPropertyMetadataOptions.Inherits));
public static readonly DependencyProperty MouseHoverBackgroundProperty = DependencyProperty.RegisterAttached(
"MouseHoverBackground", typeof(Brush), typeof(BackgroundSwitchElement), new FrameworkPropertyMetadata(Brushes.Transparent, FrameworkPropertyMetadataOptions.Inherits));
public static void SetMouseHoverBackground(DependencyObject element, Brush value) => element.SetValue(MouseHoverBackgroundProperty, value);
public static void SetMouseHoverBackground(DependencyObject element, Brush value) => element.SetValue(MouseHoverBackgroundProperty, value);
public static Brush GetMouseHoverBackground(DependencyObject element) => (Brush) element.GetValue(MouseHoverBackgroundProperty);
public static Brush GetMouseHoverBackground(DependencyObject element) => (Brush) element.GetValue(MouseHoverBackgroundProperty);
public static readonly DependencyProperty MouseDownBackgroundProperty = DependencyProperty.RegisterAttached(
"MouseDownBackground", typeof(Brush), typeof(BackgroundSwitchElement), new FrameworkPropertyMetadata(Brushes.Transparent, FrameworkPropertyMetadataOptions.Inherits));
public static readonly DependencyProperty MouseDownBackgroundProperty = DependencyProperty.RegisterAttached(
"MouseDownBackground", typeof(Brush), typeof(BackgroundSwitchElement), new FrameworkPropertyMetadata(Brushes.Transparent, FrameworkPropertyMetadataOptions.Inherits));
public static void SetMouseDownBackground(DependencyObject element, Brush value) => element.SetValue(MouseDownBackgroundProperty, value);
public static void SetMouseDownBackground(DependencyObject element, Brush value) => element.SetValue(MouseDownBackgroundProperty, value);
public static Brush GetMouseDownBackground(DependencyObject element) => (Brush) element.GetValue(MouseDownBackgroundProperty);
}
public static Brush GetMouseDownBackground(DependencyObject element) => (Brush) element.GetValue(MouseDownBackgroundProperty);
}
@@ -4,47 +4,46 @@ using System.Windows.Data;
using HandyControl.Data;
using HandyControl.Tools.Converter;
namespace HandyControl.Controls
namespace HandyControl.Controls;
public class BorderElement
{
public class BorderElement
public static readonly DependencyProperty CornerRadiusProperty = DependencyProperty.RegisterAttached(
"CornerRadius", typeof(CornerRadius), typeof(BorderElement), new FrameworkPropertyMetadata(default(CornerRadius), FrameworkPropertyMetadataOptions.Inherits));
public static void SetCornerRadius(DependencyObject element, CornerRadius value) => element.SetValue(CornerRadiusProperty, value);
public static CornerRadius GetCornerRadius(DependencyObject element) => (CornerRadius) element.GetValue(CornerRadiusProperty);
public static readonly DependencyProperty CircularProperty = DependencyProperty.RegisterAttached(
"Circular", typeof(bool), typeof(BorderElement), new PropertyMetadata(ValueBoxes.FalseBox, OnCircularChanged));
private static void OnCircularChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
public static readonly DependencyProperty CornerRadiusProperty = DependencyProperty.RegisterAttached(
"CornerRadius", typeof(CornerRadius), typeof(BorderElement), new FrameworkPropertyMetadata(default(CornerRadius), FrameworkPropertyMetadataOptions.Inherits));
public static void SetCornerRadius(DependencyObject element, CornerRadius value) => element.SetValue(CornerRadiusProperty, value);
public static CornerRadius GetCornerRadius(DependencyObject element) => (CornerRadius) element.GetValue(CornerRadiusProperty);
public static readonly DependencyProperty CircularProperty = DependencyProperty.RegisterAttached(
"Circular", typeof(bool), typeof(BorderElement), new PropertyMetadata(ValueBoxes.FalseBox, OnCircularChanged));
private static void OnCircularChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
if (d is Border border)
{
if (d is Border border)
if ((bool) e.NewValue)
{
if ((bool) e.NewValue)
var binding = new MultiBinding
{
var binding = new MultiBinding
{
Converter = new BorderCircularConverter()
};
binding.Bindings.Add(new Binding(FrameworkElement.ActualWidthProperty.Name) { Source = border });
binding.Bindings.Add(new Binding(FrameworkElement.ActualHeightProperty.Name) { Source = border });
border.SetBinding(Border.CornerRadiusProperty, binding);
}
else
{
BindingOperations.ClearBinding(border, FrameworkElement.ActualWidthProperty);
BindingOperations.ClearBinding(border, FrameworkElement.ActualHeightProperty);
BindingOperations.ClearBinding(border, Border.CornerRadiusProperty);
}
Converter = new BorderCircularConverter()
};
binding.Bindings.Add(new Binding(FrameworkElement.ActualWidthProperty.Name) { Source = border });
binding.Bindings.Add(new Binding(FrameworkElement.ActualHeightProperty.Name) { Source = border });
border.SetBinding(Border.CornerRadiusProperty, binding);
}
else
{
BindingOperations.ClearBinding(border, FrameworkElement.ActualWidthProperty);
BindingOperations.ClearBinding(border, FrameworkElement.ActualHeightProperty);
BindingOperations.ClearBinding(border, Border.CornerRadiusProperty);
}
}
public static void SetCircular(DependencyObject element, bool value)
=> element.SetValue(CircularProperty, ValueBoxes.BooleanBox(value));
public static bool GetCircular(DependencyObject element)
=> (bool) element.GetValue(CircularProperty);
}
public static void SetCircular(DependencyObject element, bool value)
=> element.SetValue(CircularProperty, ValueBoxes.BooleanBox(value));
public static bool GetCircular(DependencyObject element)
=> (bool) element.GetValue(CircularProperty);
}
@@ -1,288 +1,279 @@
using System.Linq;
using System.Collections.Specialized;
using System.Linq;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Controls.Primitives;
using System.Windows.Input;
using HandyControl.Data;
namespace HandyControl.Controls
namespace HandyControl.Controls;
public class DataGridAttach
{
public class DataGridAttach
public static readonly DependencyProperty ApplyDefaultStyleProperty = DependencyProperty.RegisterAttached(
"ApplyDefaultStyle", typeof(bool), typeof(DataGridAttach), new PropertyMetadata(ValueBoxes.FalseBox, OnApplyDefaultStyleChanged));
private static void OnApplyDefaultStyleChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
public static readonly DependencyProperty ApplyDefaultStyleProperty = DependencyProperty.RegisterAttached(
"ApplyDefaultStyle", typeof(bool), typeof(DataGridAttach), new PropertyMetadata(ValueBoxes.FalseBox, OnApplyDefaultStyleChanged));
private static void OnApplyDefaultStyleChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
var grid = (DataGrid) d;
if ((bool) e.NewValue)
{
var grid = (DataGrid) d;
if ((bool) e.NewValue)
{
if (grid.AutoGenerateColumns)
{
grid.AutoGeneratedColumns += Grid_AutoGeneratedColumns;
return;
}
UpdateTextColumnStyles(grid);
UpdateComboBoxColumnStyles(grid);
UpdateCheckBoxColumnStyles(grid);
}
else
{
grid.AutoGeneratedColumns -= Grid_AutoGeneratedColumns;
}
grid.Columns.CollectionChanged += OnDataGridColumnsCollectionChanged;
}
else
{
grid.Columns.CollectionChanged -= OnDataGridColumnsCollectionChanged;
}
private static void Grid_AutoGeneratedColumns(object sender, System.EventArgs e)
void OnDataGridColumnsCollectionChanged(object sender, NotifyCollectionChangedEventArgs e)
{
var grid = (DataGrid) sender;
if (e.Action is NotifyCollectionChangedAction.Remove or NotifyCollectionChangedAction.Move)
{
return;
}
UpdateTextColumnStyles(grid);
UpdateComboBoxColumnStyles(grid);
UpdateCheckBoxColumnStyles(grid);
}
}
public static void SetApplyDefaultStyle(DependencyObject element, bool value)
=> element.SetValue(ApplyDefaultStyleProperty, ValueBoxes.BooleanBox(value));
public static void SetApplyDefaultStyle(DependencyObject element, bool value)
=> element.SetValue(ApplyDefaultStyleProperty, ValueBoxes.BooleanBox(value));
public static bool GetApplyDefaultStyle(DependencyObject element)
=> (bool) element.GetValue(ApplyDefaultStyleProperty);
public static bool GetApplyDefaultStyle(DependencyObject element)
=> (bool) element.GetValue(ApplyDefaultStyleProperty);
public static readonly DependencyProperty TextColumnStyleProperty = DependencyProperty.RegisterAttached(
"TextColumnStyle", typeof(Style), typeof(DataGridAttach), new PropertyMetadata(default(Style), OnTextColumnStyleChanged));
public static readonly DependencyProperty TextColumnStyleProperty = DependencyProperty.RegisterAttached(
"TextColumnStyle", typeof(Style), typeof(DataGridAttach), new PropertyMetadata(default(Style), OnTextColumnStyleChanged));
private static void OnTextColumnStyleChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
private static void OnTextColumnStyleChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
var grid = (DataGrid) d;
if (e.OldValue == null && e.NewValue != null)
{
var grid = (DataGrid) d;
if (e.OldValue == null && e.NewValue != null)
{
UpdateTextColumnStyles(grid);
}
UpdateTextColumnStyles(grid);
}
}
public static void SetTextColumnStyle(DependencyObject element, Style value) => element.SetValue(TextColumnStyleProperty, value);
public static void SetTextColumnStyle(DependencyObject element, Style value) => element.SetValue(TextColumnStyleProperty, value);
public static Style GetTextColumnStyle(DependencyObject element) => (Style) element.GetValue(TextColumnStyleProperty);
public static Style GetTextColumnStyle(DependencyObject element) => (Style) element.GetValue(TextColumnStyleProperty);
public static readonly DependencyProperty EditingTextColumnStyleProperty = DependencyProperty.RegisterAttached(
"EditingTextColumnStyle", typeof(Style), typeof(DataGridAttach), new PropertyMetadata(default(Style), OnTextColumnStyleChanged));
public static readonly DependencyProperty EditingTextColumnStyleProperty = DependencyProperty.RegisterAttached(
"EditingTextColumnStyle", typeof(Style), typeof(DataGridAttach), new PropertyMetadata(default(Style), OnTextColumnStyleChanged));
public static void SetEditingTextColumnStyle(DependencyObject element, Style value) => element.SetValue(EditingTextColumnStyleProperty, value);
public static void SetEditingTextColumnStyle(DependencyObject element, Style value) => element.SetValue(EditingTextColumnStyleProperty, value);
public static Style GetEditingTextColumnStyle(DependencyObject element) => (Style) element.GetValue(EditingTextColumnStyleProperty);
public static Style GetEditingTextColumnStyle(DependencyObject element) => (Style) element.GetValue(EditingTextColumnStyleProperty);
private static void UpdateTextColumnStyles(DataGrid grid)
private static void UpdateTextColumnStyles(DataGrid grid)
{
var textColumnStyle = GetTextColumnStyle(grid);
var editingTextColumnStyle = GetEditingTextColumnStyle(grid);
if (textColumnStyle != null)
{
var textColumnStyle = GetTextColumnStyle(grid);
var editingTextColumnStyle = GetEditingTextColumnStyle(grid);
if (textColumnStyle != null)
foreach (var column in grid.Columns.OfType<DataGridTextColumn>())
{
foreach (var column in grid.Columns.OfType<DataGridTextColumn>())
var elementStyle = new Style
{
var elementStyle = new Style
{
BasedOn = column.ElementStyle,
TargetType = textColumnStyle.TargetType
};
BasedOn = column.ElementStyle,
TargetType = textColumnStyle.TargetType
};
foreach (var setter in textColumnStyle.Setters.OfType<Setter>())
{
elementStyle.Setters.Add(setter);
}
column.ElementStyle = elementStyle;
}
}
if (editingTextColumnStyle != null)
{
foreach (var column in grid.Columns.OfType<DataGridTextColumn>())
foreach (var setter in textColumnStyle.Setters.OfType<Setter>())
{
var editingElementStyle = new Style
{
BasedOn = column.EditingElementStyle,
TargetType = editingTextColumnStyle.TargetType
};
foreach (var setter in editingTextColumnStyle.Setters.OfType<Setter>())
{
editingElementStyle.Setters.Add(setter);
}
column.EditingElementStyle = editingElementStyle;
elementStyle.Setters.Add(setter);
}
column.ElementStyle = elementStyle;
}
}
public static readonly DependencyProperty ComboBoxColumnStyleProperty = DependencyProperty.RegisterAttached(
"ComboBoxColumnStyle", typeof(Style), typeof(DataGridAttach), new PropertyMetadata(default(Style), OnComboBoxColumnStyleChanged));
private static void OnComboBoxColumnStyleChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
if (editingTextColumnStyle != null)
{
var grid = (DataGrid) d;
if (e.OldValue == null && e.NewValue != null)
foreach (var column in grid.Columns.OfType<DataGridTextColumn>())
{
UpdateComboBoxColumnStyles(grid);
}
}
public static void SetComboBoxColumnStyle(DependencyObject element, Style value) => element.SetValue(ComboBoxColumnStyleProperty, value);
public static Style GetComboBoxColumnStyle(DependencyObject element) => (Style) element.GetValue(ComboBoxColumnStyleProperty);
public static readonly DependencyProperty EditingComboBoxColumnStyleProperty = DependencyProperty.RegisterAttached(
"EditingComboBoxColumnStyle", typeof(Style), typeof(DataGridAttach), new PropertyMetadata(default(Style), OnComboBoxColumnStyleChanged));
public static void SetEditingComboBoxColumnStyle(DependencyObject element, Style value) => element.SetValue(EditingComboBoxColumnStyleProperty, value);
public static Style GetEditingComboBoxColumnStyle(DependencyObject element) => (Style) element.GetValue(EditingComboBoxColumnStyleProperty);
private static void UpdateComboBoxColumnStyles(DataGrid grid)
{
var comboBoxColumnStyle = GetComboBoxColumnStyle(grid);
var editingComboBoxColumnStyle = GetEditingComboBoxColumnStyle(grid);
if (comboBoxColumnStyle != null)
{
foreach (var column in grid.Columns.OfType<DataGridComboBoxColumn>())
var editingElementStyle = new Style
{
var elementStyle = new Style
{
BasedOn = column.ElementStyle,
TargetType = comboBoxColumnStyle.TargetType
};
BasedOn = column.EditingElementStyle,
TargetType = editingTextColumnStyle.TargetType
};
foreach (var setter in comboBoxColumnStyle.Setters.OfType<Setter>())
{
elementStyle.Setters.Add(setter);
}
column.ElementStyle = elementStyle;
}
}
if (editingComboBoxColumnStyle != null)
{
foreach (var column in grid.Columns.OfType<DataGridComboBoxColumn>())
foreach (var setter in editingTextColumnStyle.Setters.OfType<Setter>())
{
var editingElementStyle = new Style
{
BasedOn = column.EditingElementStyle,
TargetType = editingComboBoxColumnStyle.TargetType
};
foreach (var setter in editingComboBoxColumnStyle.Setters.OfType<Setter>())
{
editingElementStyle.Setters.Add(setter);
}
column.EditingElementStyle = editingElementStyle;
editingElementStyle.Setters.Add(setter);
}
column.EditingElementStyle = editingElementStyle;
}
}
}
public static readonly DependencyProperty CheckBoxColumnStyleProperty = DependencyProperty.RegisterAttached(
"CheckBoxColumnStyle", typeof(Style), typeof(DataGridAttach), new PropertyMetadata(default(Style), OnCheckBoxColumnStyleChanged));
public static readonly DependencyProperty ComboBoxColumnStyleProperty = DependencyProperty.RegisterAttached(
"ComboBoxColumnStyle", typeof(Style), typeof(DataGridAttach), new PropertyMetadata(default(Style), OnComboBoxColumnStyleChanged));
private static void OnCheckBoxColumnStyleChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
private static void OnComboBoxColumnStyleChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
var grid = (DataGrid) d;
if (e.OldValue == null && e.NewValue != null)
{
var grid = (DataGrid) d;
if (e.OldValue == null && e.NewValue != null)
{
UpdateCheckBoxColumnStyles(grid);
}
UpdateComboBoxColumnStyles(grid);
}
}
public static void SetCheckBoxColumnStyle(DependencyObject element, Style value) => element.SetValue(CheckBoxColumnStyleProperty, value);
public static void SetComboBoxColumnStyle(DependencyObject element, Style value) => element.SetValue(ComboBoxColumnStyleProperty, value);
public static Style GetCheckBoxColumnStyle(DependencyObject element) => (Style) element.GetValue(CheckBoxColumnStyleProperty);
public static Style GetComboBoxColumnStyle(DependencyObject element) => (Style) element.GetValue(ComboBoxColumnStyleProperty);
public static readonly DependencyProperty EditingCheckBoxColumnStyleProperty = DependencyProperty.RegisterAttached(
"EditingCheckBoxColumnStyle", typeof(Style), typeof(DataGridAttach), new PropertyMetadata(default(Style), OnCheckBoxColumnStyleChanged));
public static readonly DependencyProperty EditingComboBoxColumnStyleProperty = DependencyProperty.RegisterAttached(
"EditingComboBoxColumnStyle", typeof(Style), typeof(DataGridAttach), new PropertyMetadata(default(Style), OnComboBoxColumnStyleChanged));
public static void SetEditingCheckBoxColumnStyle(DependencyObject element, Style value) => element.SetValue(EditingCheckBoxColumnStyleProperty, value);
public static void SetEditingComboBoxColumnStyle(DependencyObject element, Style value) => element.SetValue(EditingComboBoxColumnStyleProperty, value);
public static Style GetEditingCheckBoxColumnStyle(DependencyObject element) => (Style) element.GetValue(EditingCheckBoxColumnStyleProperty);
public static Style GetEditingComboBoxColumnStyle(DependencyObject element) => (Style) element.GetValue(EditingComboBoxColumnStyleProperty);
private static void UpdateCheckBoxColumnStyles(DataGrid grid)
private static void UpdateComboBoxColumnStyles(DataGrid grid)
{
var comboBoxColumnStyle = GetComboBoxColumnStyle(grid);
var editingComboBoxColumnStyle = GetEditingComboBoxColumnStyle(grid);
if (comboBoxColumnStyle != null)
{
var checkBoxColumnStyle = GetCheckBoxColumnStyle(grid);
var editingCheckBoxColumnStyle = GetEditingCheckBoxColumnStyle(grid);
if (checkBoxColumnStyle != null)
foreach (var column in grid.Columns.OfType<DataGridComboBoxColumn>())
{
foreach (var column in grid.Columns.OfType<DataGridCheckBoxColumn>())
var elementStyle = new Style
{
var elementStyle = new Style
{
BasedOn = column.ElementStyle,
TargetType = checkBoxColumnStyle.TargetType
};
BasedOn = column.ElementStyle,
TargetType = comboBoxColumnStyle.TargetType
};
foreach (var setter in checkBoxColumnStyle.Setters.OfType<Setter>())
{
elementStyle.Setters.Add(setter);
}
column.ElementStyle = elementStyle;
}
}
if (editingCheckBoxColumnStyle != null)
{
foreach (var column in grid.Columns.OfType<DataGridCheckBoxColumn>())
foreach (var setter in comboBoxColumnStyle.Setters.OfType<Setter>())
{
var editingElementStyle = new Style
{
BasedOn = column.EditingElementStyle,
TargetType = editingCheckBoxColumnStyle.TargetType
};
foreach (var setter in editingCheckBoxColumnStyle.Setters.OfType<Setter>())
{
editingElementStyle.Setters.Add(setter);
}
column.EditingElementStyle = editingElementStyle;
elementStyle.Setters.Add(setter);
}
column.ElementStyle = elementStyle;
}
}
public static DependencyProperty ShowRowNumberProperty = DependencyProperty.RegisterAttached("ShowRowNumber",
typeof(bool), typeof(DataGridAttach),
new FrameworkPropertyMetadata(ValueBoxes.FalseBox, FrameworkPropertyMetadataOptions.Inherits, OnShowRowNumberChanged));
public static bool GetShowRowNumber(DependencyObject target) => (bool) target.GetValue(ShowRowNumberProperty);
public static void SetShowRowNumber(DependencyObject target, bool value) => target.SetValue(ShowRowNumberProperty, ValueBoxes.BooleanBox(value));
private static void OnShowRowNumberChanged(DependencyObject target, DependencyPropertyChangedEventArgs e)
if (editingComboBoxColumnStyle != null)
{
if (!(target is DataGrid dataGrid)) return;
var show = (bool) e.NewValue;
if (show)
foreach (var column in grid.Columns.OfType<DataGridComboBoxColumn>())
{
dataGrid.LoadingRow += DataGrid_LoadingRow;
dataGrid.ItemContainerGenerator.ItemsChanged += ItemContainerGenerator_ItemsChanged;
var editingElementStyle = new Style
{
BasedOn = column.EditingElementStyle,
TargetType = editingComboBoxColumnStyle.TargetType
};
foreach (var setter in editingComboBoxColumnStyle.Setters.OfType<Setter>())
{
editingElementStyle.Setters.Add(setter);
}
column.EditingElementStyle = editingElementStyle;
}
else
}
}
public static readonly DependencyProperty CheckBoxColumnStyleProperty = DependencyProperty.RegisterAttached(
"CheckBoxColumnStyle", typeof(Style), typeof(DataGridAttach), new PropertyMetadata(default(Style), OnCheckBoxColumnStyleChanged));
private static void OnCheckBoxColumnStyleChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
var grid = (DataGrid) d;
if (e.OldValue == null && e.NewValue != null)
{
UpdateCheckBoxColumnStyles(grid);
}
}
public static void SetCheckBoxColumnStyle(DependencyObject element, Style value) => element.SetValue(CheckBoxColumnStyleProperty, value);
public static Style GetCheckBoxColumnStyle(DependencyObject element) => (Style) element.GetValue(CheckBoxColumnStyleProperty);
public static readonly DependencyProperty EditingCheckBoxColumnStyleProperty = DependencyProperty.RegisterAttached(
"EditingCheckBoxColumnStyle", typeof(Style), typeof(DataGridAttach), new PropertyMetadata(default(Style), OnCheckBoxColumnStyleChanged));
public static void SetEditingCheckBoxColumnStyle(DependencyObject element, Style value) => element.SetValue(EditingCheckBoxColumnStyleProperty, value);
public static Style GetEditingCheckBoxColumnStyle(DependencyObject element) => (Style) element.GetValue(EditingCheckBoxColumnStyleProperty);
private static void UpdateCheckBoxColumnStyles(DataGrid grid)
{
var checkBoxColumnStyle = GetCheckBoxColumnStyle(grid);
var editingCheckBoxColumnStyle = GetEditingCheckBoxColumnStyle(grid);
if (checkBoxColumnStyle != null)
{
foreach (var column in grid.Columns.OfType<DataGridCheckBoxColumn>())
{
dataGrid.LoadingRow -= DataGrid_LoadingRow;
dataGrid.ItemContainerGenerator.ItemsChanged -= ItemContainerGenerator_ItemsChanged;
var elementStyle = new Style
{
BasedOn = column.ElementStyle,
TargetType = checkBoxColumnStyle.TargetType
};
foreach (var setter in checkBoxColumnStyle.Setters.OfType<Setter>())
{
elementStyle.Setters.Add(setter);
}
column.ElementStyle = elementStyle;
}
UpdateItems(dataGrid.ItemContainerGenerator, dataGrid.Items.Count, show);
}
private static void ItemContainerGenerator_ItemsChanged(object sender, ItemsChangedEventArgs e)
if (editingCheckBoxColumnStyle != null)
{
if (!(sender is ItemContainerGenerator generator)) return;
UpdateItems(generator, e.ItemCount);
foreach (var column in grid.Columns.OfType<DataGridCheckBoxColumn>())
{
var editingElementStyle = new Style
{
BasedOn = column.EditingElementStyle,
TargetType = editingCheckBoxColumnStyle.TargetType
};
foreach (var setter in editingCheckBoxColumnStyle.Setters.OfType<Setter>())
{
editingElementStyle.Setters.Add(setter);
}
column.EditingElementStyle = editingElementStyle;
}
}
}
public static DependencyProperty ShowRowNumberProperty = DependencyProperty.RegisterAttached("ShowRowNumber",
typeof(bool), typeof(DataGridAttach),
new FrameworkPropertyMetadata(ValueBoxes.FalseBox, FrameworkPropertyMetadataOptions.Inherits, OnShowRowNumberChanged));
public static bool GetShowRowNumber(DependencyObject target) => (bool) target.GetValue(ShowRowNumberProperty);
public static void SetShowRowNumber(DependencyObject target, bool value) => target.SetValue(ShowRowNumberProperty, ValueBoxes.BooleanBox(value));
private static void OnShowRowNumberChanged(DependencyObject target, DependencyPropertyChangedEventArgs e)
{
if (target is not DataGrid dataGrid) return;
var show = (bool) e.NewValue;
if (show)
{
dataGrid.LoadingRow += DataGrid_LoadingRow;
dataGrid.ItemContainerGenerator.ItemsChanged += ItemContainerGeneratorItemsChanged;
}
else
{
dataGrid.LoadingRow -= DataGrid_LoadingRow;
dataGrid.ItemContainerGenerator.ItemsChanged -= ItemContainerGeneratorItemsChanged;
}
private static void UpdateItems(ItemContainerGenerator generator, int itemsCount, bool show = true)
void ItemContainerGeneratorItemsChanged(object sender, ItemsChangedEventArgs e)
{
var generator = dataGrid.ItemContainerGenerator;
var itemsCount = dataGrid.Items.Count;
if (show)
{
for (var i = 0; i < itemsCount; i++)
@@ -306,40 +297,49 @@ namespace HandyControl.Controls
}
}
}
private static void DataGrid_LoadingRow(object sender, DataGridRowEventArgs e) => e.Row.Header = (e.Row.GetIndex() + 1).ToString();
public static readonly DependencyProperty CanUnselectAllWithBlankAreaProperty = DependencyProperty.RegisterAttached(
"CanUnselectAllWithBlankArea", typeof(bool), typeof(DataGridAttach), new PropertyMetadata(ValueBoxes.FalseBox, OnCanUnselectAllWithBlankAreaChanged));
private static void OnCanUnselectAllWithBlankAreaChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
if (d is DataGrid dataGrid)
{
if ((bool) e.NewValue)
{
dataGrid.PreviewMouseDown += DataGrid_PreviewMouseDown;
}
else
{
dataGrid.PreviewMouseDown -= DataGrid_PreviewMouseDown;
}
}
}
private static void DataGrid_PreviewMouseDown(object sender, MouseButtonEventArgs e)
{
if (sender is DataGrid dataGrid && e.OriginalSource is System.Windows.Controls.ScrollViewer)
{
dataGrid.CommitEdit();
dataGrid.UnselectAll();
}
}
public static void SetCanUnselectAllWithBlankArea(DependencyObject element, bool value)
=> element.SetValue(CanUnselectAllWithBlankAreaProperty, value);
public static bool GetCanUnselectAllWithBlankArea(DependencyObject element)
=> (bool) element.GetValue(CanUnselectAllWithBlankAreaProperty);
}
private static void DataGrid_LoadingRow(object sender, DataGridRowEventArgs e) => e.Row.Header = (e.Row.GetIndex() + 1).ToString();
public static readonly DependencyProperty CanUnselectAllWithBlankAreaProperty = DependencyProperty.RegisterAttached(
"CanUnselectAllWithBlankArea", typeof(bool), typeof(DataGridAttach), new PropertyMetadata(ValueBoxes.FalseBox, OnCanUnselectAllWithBlankAreaChanged));
private static void OnCanUnselectAllWithBlankAreaChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
if (d is DataGrid dataGrid)
{
if ((bool) e.NewValue)
{
dataGrid.PreviewMouseDown += DataGrid_PreviewMouseDown;
}
else
{
dataGrid.PreviewMouseDown -= DataGrid_PreviewMouseDown;
}
}
}
private static void DataGrid_PreviewMouseDown(object sender, MouseButtonEventArgs e)
{
if (sender is DataGrid dataGrid && e.OriginalSource is System.Windows.Controls.ScrollViewer)
{
dataGrid.CommitEdit();
dataGrid.UnselectAll();
}
}
public static void SetCanUnselectAllWithBlankArea(DependencyObject element, bool value)
=> element.SetValue(CanUnselectAllWithBlankAreaProperty, ValueBoxes.BooleanBox(value));
public static bool GetCanUnselectAllWithBlankArea(DependencyObject element)
=> (bool) element.GetValue(CanUnselectAllWithBlankAreaProperty);
public static readonly DependencyProperty ShowSelectAllButtonProperty = DependencyProperty.RegisterAttached(
"ShowSelectAllButton", typeof(bool), typeof(DataGridAttach), new PropertyMetadata(ValueBoxes.TrueBox));
public static void SetShowSelectAllButton(DependencyObject element, bool value)
=> element.SetValue(ShowSelectAllButtonProperty, ValueBoxes.BooleanBox(value));
public static bool GetShowSelectAllButton(DependencyObject element)
=> (bool) element.GetValue(ShowSelectAllButtonProperty);
}
@@ -1,28 +1,27 @@
using System.Windows;
using HandyControl.Data;
namespace HandyControl.Controls
namespace HandyControl.Controls;
public class DropDownElement
{
public class DropDownElement
{
public static readonly DependencyProperty ConsistentWidthProperty = DependencyProperty.RegisterAttached(
"ConsistentWidth", typeof(bool), typeof(DropDownElement),
new FrameworkPropertyMetadata(ValueBoxes.TrueBox, FrameworkPropertyMetadataOptions.Inherits));
public static readonly DependencyProperty ConsistentWidthProperty = DependencyProperty.RegisterAttached(
"ConsistentWidth", typeof(bool), typeof(DropDownElement),
new FrameworkPropertyMetadata(ValueBoxes.FalseBox, FrameworkPropertyMetadataOptions.Inherits));
public static void SetConsistentWidth(DependencyObject element, bool value)
=> element.SetValue(ConsistentWidthProperty, ValueBoxes.BooleanBox(value));
public static void SetConsistentWidth(DependencyObject element, bool value)
=> element.SetValue(ConsistentWidthProperty, ValueBoxes.BooleanBox(value));
public static bool GetConsistentWidth(DependencyObject element)
=> (bool) element.GetValue(ConsistentWidthProperty);
public static bool GetConsistentWidth(DependencyObject element)
=> (bool) element.GetValue(ConsistentWidthProperty);
public static readonly DependencyProperty AutoWidthProperty = DependencyProperty.RegisterAttached(
"AutoWidth", typeof(bool), typeof(DropDownElement),
new FrameworkPropertyMetadata(ValueBoxes.FalseBox, FrameworkPropertyMetadataOptions.Inherits));
public static readonly DependencyProperty AutoWidthProperty = DependencyProperty.RegisterAttached(
"AutoWidth", typeof(bool), typeof(DropDownElement),
new FrameworkPropertyMetadata(ValueBoxes.FalseBox, FrameworkPropertyMetadataOptions.Inherits));
public static void SetAutoWidth(DependencyObject element, bool value)
=> element.SetValue(AutoWidthProperty, value);
public static void SetAutoWidth(DependencyObject element, bool value)
=> element.SetValue(AutoWidthProperty, ValueBoxes.BooleanBox(value));
public static bool GetAutoWidth(DependencyObject element)
=> (bool) element.GetValue(AutoWidthProperty);
}
public static bool GetAutoWidth(DependencyObject element)
=> (bool) element.GetValue(AutoWidthProperty);
}
@@ -1,47 +1,46 @@
using System.Windows;
using HandyControl.Data;
namespace HandyControl.Controls
namespace HandyControl.Controls;
public class EdgeElement
{
public class EdgeElement
{
public static readonly DependencyProperty LeftContentProperty = DependencyProperty.RegisterAttached(
"LeftContent", typeof(object), typeof(EdgeElement), new PropertyMetadata(default(object), OnEdgeContentChanged));
public static readonly DependencyProperty LeftContentProperty = DependencyProperty.RegisterAttached(
"LeftContent", typeof(object), typeof(EdgeElement), new PropertyMetadata(default(object), OnEdgeContentChanged));
public static void SetLeftContent(DependencyObject element, object value) => element.SetValue(LeftContentProperty, value);
public static void SetLeftContent(DependencyObject element, object value) => element.SetValue(LeftContentProperty, value);
public static object GetLeftContent(DependencyObject element) => element.GetValue(LeftContentProperty);
public static object GetLeftContent(DependencyObject element) => element.GetValue(LeftContentProperty);
public static readonly DependencyProperty TopContentProperty = DependencyProperty.RegisterAttached(
"TopContent", typeof(object), typeof(EdgeElement), new PropertyMetadata(default(object), OnEdgeContentChanged));
public static readonly DependencyProperty TopContentProperty = DependencyProperty.RegisterAttached(
"TopContent", typeof(object), typeof(EdgeElement), new PropertyMetadata(default(object), OnEdgeContentChanged));
public static void SetTopContent(DependencyObject element, object value) => element.SetValue(TopContentProperty, value);
public static void SetTopContent(DependencyObject element, object value) => element.SetValue(TopContentProperty, value);
public static object GetTopContent(DependencyObject element) => element.GetValue(TopContentProperty);
public static object GetTopContent(DependencyObject element) => element.GetValue(TopContentProperty);
public static readonly DependencyProperty RightContentProperty = DependencyProperty.RegisterAttached(
"RightContent", typeof(object), typeof(EdgeElement), new PropertyMetadata(default(object), OnEdgeContentChanged));
public static readonly DependencyProperty RightContentProperty = DependencyProperty.RegisterAttached(
"RightContent", typeof(object), typeof(EdgeElement), new PropertyMetadata(default(object), OnEdgeContentChanged));
public static void SetRightContent(DependencyObject element, object value) => element.SetValue(RightContentProperty, value);
public static void SetRightContent(DependencyObject element, object value) => element.SetValue(RightContentProperty, value);
public static object GetRightContent(DependencyObject element) => element.GetValue(RightContentProperty);
public static object GetRightContent(DependencyObject element) => element.GetValue(RightContentProperty);
public static readonly DependencyProperty BottomContentProperty = DependencyProperty.RegisterAttached(
"BottomContent", typeof(object), typeof(EdgeElement), new PropertyMetadata(default(object), OnEdgeContentChanged));
public static readonly DependencyProperty BottomContentProperty = DependencyProperty.RegisterAttached(
"BottomContent", typeof(object), typeof(EdgeElement), new PropertyMetadata(default(object), OnEdgeContentChanged));
public static void SetBottomContent(DependencyObject element, object value) => element.SetValue(BottomContentProperty, value);
public static void SetBottomContent(DependencyObject element, object value) => element.SetValue(BottomContentProperty, value);
public static object GetBottomContent(DependencyObject element) => element.GetValue(BottomContentProperty);
public static object GetBottomContent(DependencyObject element) => element.GetValue(BottomContentProperty);
public static readonly DependencyProperty ShowEdgeContentProperty = DependencyProperty.RegisterAttached(
"ShowEdgeContent", typeof(bool), typeof(EdgeElement), new PropertyMetadata(ValueBoxes.FalseBox));
public static readonly DependencyProperty ShowEdgeContentProperty = DependencyProperty.RegisterAttached(
"ShowEdgeContent", typeof(bool), typeof(EdgeElement), new PropertyMetadata(ValueBoxes.FalseBox));
public static void SetShowEdgeContent(DependencyObject element, bool value) => element.SetValue(ShowEdgeContentProperty, ValueBoxes.BooleanBox(value));
public static void SetShowEdgeContent(DependencyObject element, bool value) => element.SetValue(ShowEdgeContentProperty, ValueBoxes.BooleanBox(value));
public static bool GetShowEdgeContent(DependencyObject element) => (bool) element.GetValue(ShowEdgeContentProperty);
public static bool GetShowEdgeContent(DependencyObject element) => (bool) element.GetValue(ShowEdgeContentProperty);
private static void OnEdgeContentChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) =>
SetShowEdgeContent(d, !(GetLeftContent(d) == null && GetTopContent(d) == null &&
GetRightContent(d) == null && GetBottomContent(d) == null));
}
private static void OnEdgeContentChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) =>
SetShowEdgeContent(d, !(GetLeftContent(d) == null && GetTopContent(d) == null &&
GetRightContent(d) == null && GetBottomContent(d) == null));
}
@@ -0,0 +1,95 @@
using System;
using System.Windows;
using System.Windows.Controls;
namespace HandyControl.Controls;
public class GridAttach
{
public static readonly DependencyProperty NameProperty = DependencyProperty.RegisterAttached(
"Name", typeof(string), typeof(GridAttach), new PropertyMetadata(default(string)));
public static void SetName(DependencyObject element, string value)
=> element.SetValue(NameProperty, value);
public static string GetName(DependencyObject element)
=> (string) element.GetValue(NameProperty);
public static readonly DependencyProperty RowNameProperty = DependencyProperty.RegisterAttached(
"RowName", typeof(string), typeof(GridAttach), new PropertyMetadata(default(string), OnRowNameChanged));
private static void OnRowNameChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
if (d is FrameworkElement frameworkElement)
{
if (e.NewValue is string rowName)
{
if (string.IsNullOrEmpty(rowName))
{
return;
}
if (frameworkElement.Parent is Grid grid)
{
for (var i = 0; i < grid.RowDefinitions.Count; i++)
{
var gridRowDefinition = grid.RowDefinitions[i];
var gridRowName = GetName(gridRowDefinition);
if (!string.IsNullOrEmpty(gridRowName) &&
gridRowName.Equals(rowName, StringComparison.Ordinal))
{
Grid.SetRow(frameworkElement, i);
return;
}
}
}
}
}
}
public static void SetRowName(DependencyObject element, string value)
=> element.SetValue(RowNameProperty, value);
public static string GetRowName(DependencyObject element)
=> (string) element.GetValue(RowNameProperty);
public static readonly DependencyProperty ColumnNameProperty = DependencyProperty.RegisterAttached(
"ColumnName", typeof(string), typeof(GridAttach), new PropertyMetadata(default(string), OnColumnNameChanged));
private static void OnColumnNameChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
if (d is FrameworkElement frameworkElement)
{
if (e.NewValue is string columnName)
{
if (string.IsNullOrEmpty(columnName))
{
return;
}
if (frameworkElement.Parent is Grid grid)
{
for (var i = 0; i < grid.ColumnDefinitions.Count; i++)
{
var gridColumnDefinition = grid.ColumnDefinitions[i];
var gridColumnName = GetName(gridColumnDefinition);
if (!string.IsNullOrEmpty(gridColumnName) &&
gridColumnName.Equals(columnName, StringComparison.Ordinal))
{
Grid.SetColumn(frameworkElement, i);
return;
}
}
}
}
}
}
public static void SetColumnName(DependencyObject element, string value)
=> element.SetValue(ColumnNameProperty, value);
public static string GetColumnName(DependencyObject element)
=> (string) element.GetValue(ColumnNameProperty);
}
@@ -0,0 +1,16 @@
using System.Windows;
using HandyControl.Data;
namespace HandyControl.Controls;
public class GridViewAttach
{
public static readonly DependencyProperty ColumnHeaderHeightProperty = DependencyProperty.RegisterAttached(
"ColumnHeaderHeight", typeof(double), typeof(GridViewAttach), new PropertyMetadata(ValueBoxes.Double0Box));
public static void SetColumnHeaderHeight(DependencyObject element, double value)
=> element.SetValue(ColumnHeaderHeightProperty, value);
public static double GetColumnHeaderHeight(DependencyObject element)
=> (double) element.GetValue(ColumnHeaderHeightProperty);
}
@@ -1,35 +1,34 @@
using System.Windows;
using System.Windows.Media;
namespace HandyControl.Controls
namespace HandyControl.Controls;
public class IconElement
{
public class IconElement
{
public static readonly DependencyProperty GeometryProperty = DependencyProperty.RegisterAttached(
"Geometry", typeof(Geometry), typeof(IconElement), new PropertyMetadata(default(Geometry)));
public static readonly DependencyProperty GeometryProperty = DependencyProperty.RegisterAttached(
"Geometry", typeof(Geometry), typeof(IconElement), new PropertyMetadata(default(Geometry)));
public static void SetGeometry(DependencyObject element, Geometry value)
=> element.SetValue(GeometryProperty, value);
public static void SetGeometry(DependencyObject element, Geometry value)
=> element.SetValue(GeometryProperty, value);
public static Geometry GetGeometry(DependencyObject element)
=> (Geometry) element.GetValue(GeometryProperty);
public static Geometry GetGeometry(DependencyObject element)
=> (Geometry) element.GetValue(GeometryProperty);
public static readonly DependencyProperty WidthProperty = DependencyProperty.RegisterAttached(
"Width", typeof(double), typeof(IconElement), new PropertyMetadata(double.NaN));
public static readonly DependencyProperty WidthProperty = DependencyProperty.RegisterAttached(
"Width", typeof(double), typeof(IconElement), new PropertyMetadata(double.NaN));
public static void SetWidth(DependencyObject element, double value)
=> element.SetValue(WidthProperty, value);
public static void SetWidth(DependencyObject element, double value)
=> element.SetValue(WidthProperty, value);
public static double GetWidth(DependencyObject element)
=> (double) element.GetValue(WidthProperty);
public static double GetWidth(DependencyObject element)
=> (double) element.GetValue(WidthProperty);
public static readonly DependencyProperty HeightProperty = DependencyProperty.RegisterAttached(
"Height", typeof(double), typeof(IconElement), new PropertyMetadata(double.NaN));
public static readonly DependencyProperty HeightProperty = DependencyProperty.RegisterAttached(
"Height", typeof(double), typeof(IconElement), new PropertyMetadata(double.NaN));
public static void SetHeight(DependencyObject element, double value)
=> element.SetValue(HeightProperty, value);
public static void SetHeight(DependencyObject element, double value)
=> element.SetValue(HeightProperty, value);
public static double GetHeight(DependencyObject element)
=> (double) element.GetValue(HeightProperty);
}
public static double GetHeight(DependencyObject element)
=> (double) element.GetValue(HeightProperty);
}
@@ -1,21 +1,20 @@
using System.Windows;
using System.Windows.Media;
namespace HandyControl.Controls
namespace HandyControl.Controls;
public class IconSwitchElement : IconElement
{
public class IconSwitchElement : IconElement
public static readonly DependencyProperty GeometrySelectedProperty = DependencyProperty.RegisterAttached(
"GeometrySelected", typeof(Geometry), typeof(IconSwitchElement), new PropertyMetadata(default(Geometry)));
public static void SetGeometrySelected(DependencyObject element, Geometry value)
{
public static readonly DependencyProperty GeometrySelectedProperty = DependencyProperty.RegisterAttached(
"GeometrySelected", typeof(Geometry), typeof(IconSwitchElement), new PropertyMetadata(default(Geometry)));
element.SetValue(GeometrySelectedProperty, value);
}
public static void SetGeometrySelected(DependencyObject element, Geometry value)
{
element.SetValue(GeometrySelectedProperty, value);
}
public static Geometry GetGeometrySelected(DependencyObject element)
{
return (Geometry) element.GetValue(GeometrySelectedProperty);
}
public static Geometry GetGeometrySelected(DependencyObject element)
{
return (Geometry) element.GetValue(GeometrySelectedProperty);
}
}
@@ -2,41 +2,40 @@
using System.Windows.Controls;
using System.Windows.Media;
namespace HandyControl.Controls
namespace HandyControl.Controls;
public class ImageAttach
{
public class ImageAttach
public static readonly DependencyProperty SourceFailedProperty = DependencyProperty.RegisterAttached(
"SourceFailed", typeof(ImageSource), typeof(ImageAttach), new FrameworkPropertyMetadata(default(ImageSource), FrameworkPropertyMetadataOptions.Inherits, OnSourceFailedChanged));
private static void OnSourceFailedChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
public static readonly DependencyProperty SourceFailedProperty = DependencyProperty.RegisterAttached(
"SourceFailed", typeof(ImageSource), typeof(ImageAttach), new FrameworkPropertyMetadata(default(ImageSource), FrameworkPropertyMetadataOptions.Inherits, OnSourceFailedChanged));
private static void OnSourceFailedChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
if (d is Image image)
{
if (d is Image image)
if (e.NewValue is ImageSource)
{
if (e.NewValue is ImageSource)
{
image.ImageFailed += Image_ImageFailed;
}
else
{
image.ImageFailed -= Image_ImageFailed;
}
image.ImageFailed += Image_ImageFailed;
}
}
private static void Image_ImageFailed(object sender, ExceptionRoutedEventArgs e)
{
if (sender is Image image)
else
{
image.SetCurrentValue(Image.SourceProperty, GetSourceFailed(image));
image.ImageFailed -= Image_ImageFailed;
}
}
public static void SetSourceFailed(DependencyObject element, ImageSource value)
=> element.SetValue(SourceFailedProperty, value);
public static ImageSource GetSourceFailed(DependencyObject element)
=> (ImageSource) element.GetValue(SourceFailedProperty);
}
private static void Image_ImageFailed(object sender, ExceptionRoutedEventArgs e)
{
if (sender is Image image)
{
image.SetCurrentValue(Image.SourceProperty, GetSourceFailed(image));
image.ImageFailed -= Image_ImageFailed;
}
}
public static void SetSourceFailed(DependencyObject element, ImageSource value)
=> element.SetValue(SourceFailedProperty, value);
public static ImageSource GetSourceFailed(DependencyObject element)
=> (ImageSource) element.GetValue(SourceFailedProperty);
}
@@ -1,84 +1,92 @@
using System.Windows;
using HandyControl.Data;
namespace HandyControl.Controls
namespace HandyControl.Controls;
public class InfoElement : TitleElement
{
public class InfoElement : TitleElement
{
/// <summary>
/// 占位符
/// </summary>
public static readonly DependencyProperty PlaceholderProperty = DependencyProperty.RegisterAttached(
"Placeholder", typeof(string), typeof(InfoElement), new FrameworkPropertyMetadata(default(string), FrameworkPropertyMetadataOptions.Inherits));
/// <summary>
/// 占位符
/// </summary>
public static readonly DependencyProperty PlaceholderProperty = DependencyProperty.RegisterAttached(
"Placeholder", typeof(string), typeof(InfoElement), new FrameworkPropertyMetadata(default(string), FrameworkPropertyMetadataOptions.Inherits));
public static void SetPlaceholder(DependencyObject element, string value) => element.SetValue(PlaceholderProperty, value);
public static void SetPlaceholder(DependencyObject element, string value) => element.SetValue(PlaceholderProperty, value);
public static string GetPlaceholder(DependencyObject element) => (string) element.GetValue(PlaceholderProperty);
public static string GetPlaceholder(DependencyObject element) => (string) element.GetValue(PlaceholderProperty);
/// <summary>
/// 是否必填
/// </summary>
public static readonly DependencyProperty NecessaryProperty = DependencyProperty.RegisterAttached(
"Necessary", typeof(bool), typeof(InfoElement), new FrameworkPropertyMetadata(ValueBoxes.FalseBox, FrameworkPropertyMetadataOptions.Inherits));
/// <summary>
/// 是否必填
/// </summary>
public static readonly DependencyProperty NecessaryProperty = DependencyProperty.RegisterAttached(
"Necessary", typeof(bool), typeof(InfoElement), new FrameworkPropertyMetadata(ValueBoxes.FalseBox, FrameworkPropertyMetadataOptions.Inherits));
public static void SetNecessary(DependencyObject element, bool value) => element.SetValue(NecessaryProperty, ValueBoxes.BooleanBox(value));
public static void SetNecessary(DependencyObject element, bool value) => element.SetValue(NecessaryProperty, ValueBoxes.BooleanBox(value));
public static bool GetNecessary(DependencyObject element) => (bool) element.GetValue(NecessaryProperty);
public static bool GetNecessary(DependencyObject element) => (bool) element.GetValue(NecessaryProperty);
/// <summary>
/// 标记
/// </summary>
public static readonly DependencyProperty SymbolProperty = DependencyProperty.RegisterAttached(
"Symbol", typeof(string), typeof(InfoElement), new FrameworkPropertyMetadata(default(string), FrameworkPropertyMetadataOptions.Inherits));
/// <summary>
/// 标记
/// </summary>
public static readonly DependencyProperty SymbolProperty = DependencyProperty.RegisterAttached(
"Symbol", typeof(string), typeof(InfoElement), new FrameworkPropertyMetadata(default(string), FrameworkPropertyMetadataOptions.Inherits));
public static void SetSymbol(DependencyObject element, string value) => element.SetValue(SymbolProperty, value);
public static void SetSymbol(DependencyObject element, string value) => element.SetValue(SymbolProperty, value);
public static string GetSymbol(DependencyObject element) => (string) element.GetValue(SymbolProperty);
public static string GetSymbol(DependencyObject element) => (string) element.GetValue(SymbolProperty);
/// <summary>
/// 内容高度
/// </summary>
public static readonly DependencyProperty ContentHeightProperty = DependencyProperty.RegisterAttached(
"ContentHeight", typeof(double), typeof(InfoElement), new FrameworkPropertyMetadata(30.0, FrameworkPropertyMetadataOptions.Inherits));
/// <summary>
/// 内容高度
/// </summary>
public static readonly DependencyProperty ContentHeightProperty = DependencyProperty.RegisterAttached(
"ContentHeight", typeof(double), typeof(InfoElement), new FrameworkPropertyMetadata(28.0, FrameworkPropertyMetadataOptions.Inherits));
public static void SetContentHeight(DependencyObject element, double value) => element.SetValue(ContentHeightProperty, value);
public static void SetContentHeight(DependencyObject element, double value) => element.SetValue(ContentHeightProperty, value);
public static double GetContentHeight(DependencyObject element) => (double) element.GetValue(ContentHeightProperty);
public static double GetContentHeight(DependencyObject element) => (double) element.GetValue(ContentHeightProperty);
/// <summary>
/// 最小内容高度
/// </summary>
public static readonly DependencyProperty MinContentHeightProperty = DependencyProperty.RegisterAttached(
"MinContentHeight", typeof(double), typeof(InfoElement), new PropertyMetadata(30.0));
/// <summary>
/// 最小内容高度
/// </summary>
public static readonly DependencyProperty MinContentHeightProperty = DependencyProperty.RegisterAttached(
"MinContentHeight", typeof(double), typeof(InfoElement), new PropertyMetadata(28.0));
public static void SetMinContentHeight(DependencyObject element, double value)
=> element.SetValue(MinContentHeightProperty, value);
public static void SetMinContentHeight(DependencyObject element, double value)
=> element.SetValue(MinContentHeightProperty, value);
public static double GetMinContentHeight(DependencyObject element)
=> (double) element.GetValue(MinContentHeightProperty);
public static double GetMinContentHeight(DependencyObject element)
=> (double) element.GetValue(MinContentHeightProperty);
/// <summary>
/// 最大内容高度
/// </summary>
public static readonly DependencyProperty MaxContentHeightProperty = DependencyProperty.RegisterAttached(
"MaxContentHeight", typeof(double), typeof(InfoElement), new PropertyMetadata(double.PositiveInfinity));
/// <summary>
/// 最大内容高度
/// </summary>
public static readonly DependencyProperty MaxContentHeightProperty = DependencyProperty.RegisterAttached(
"MaxContentHeight", typeof(double), typeof(InfoElement), new PropertyMetadata(double.PositiveInfinity));
public static void SetMaxContentHeight(DependencyObject element, double value)
=> element.SetValue(MaxContentHeightProperty, value);
public static void SetMaxContentHeight(DependencyObject element, double value)
=> element.SetValue(MaxContentHeightProperty, value);
public static double GetMaxContentHeight(DependencyObject element)
=> (double) element.GetValue(MaxContentHeightProperty);
public static double GetMaxContentHeight(DependencyObject element)
=> (double) element.GetValue(MaxContentHeightProperty);
/// <summary>
/// 正则表达式
/// </summary>
public static readonly DependencyProperty RegexPatternProperty = DependencyProperty.RegisterAttached(
"RegexPattern", typeof(string), typeof(InfoElement), new PropertyMetadata(default(string)));
/// <summary>
/// 正则表达式
/// </summary>
public static readonly DependencyProperty RegexPatternProperty = DependencyProperty.RegisterAttached(
"RegexPattern", typeof(string), typeof(InfoElement), new PropertyMetadata(default(string)));
public static void SetRegexPattern(DependencyObject element, string value)
=> element.SetValue(RegexPatternProperty, value);
public static void SetRegexPattern(DependencyObject element, string value)
=> element.SetValue(RegexPatternProperty, value);
public static string GetRegexPattern(DependencyObject element)
=> (string) element.GetValue(RegexPatternProperty);
}
public static string GetRegexPattern(DependencyObject element)
=> (string) element.GetValue(RegexPatternProperty);
public static readonly DependencyProperty ShowClearButtonProperty = DependencyProperty.RegisterAttached(
"ShowClearButton", typeof(bool), typeof(InfoElement), new PropertyMetadata(ValueBoxes.FalseBox));
public static void SetShowClearButton(DependencyObject element, bool value)
=> element.SetValue(ShowClearButtonProperty, ValueBoxes.BooleanBox(value));
public static bool GetShowClearButton(DependencyObject element)
=> (bool) element.GetValue(ShowClearButtonProperty);
}
@@ -0,0 +1,66 @@
using System.Collections;
using System.Linq;
using System.Windows;
using System.Windows.Controls;
using HandyControl.Data;
namespace HandyControl.Controls;
public class ListBoxAttach
{
public static readonly DependencyProperty SelectedItemsProperty = DependencyProperty.RegisterAttached(
"SelectedItems", typeof(IList), typeof(ListBoxAttach),
new FrameworkPropertyMetadata(default(IList), FrameworkPropertyMetadataOptions.BindsTwoWayByDefault,
OnSelectedItemsChanged));
public static void SetSelectedItems(DependencyObject element, IList value)
=> element.SetValue(SelectedItemsProperty, value);
public static IList GetSelectedItems(DependencyObject element)
=> (IList) element.GetValue(SelectedItemsProperty);
internal static readonly DependencyProperty InternalActionProperty = DependencyProperty.RegisterAttached(
"InternalAction", typeof(bool), typeof(ListBoxAttach), new PropertyMetadata(ValueBoxes.FalseBox));
internal static void SetInternalAction(DependencyObject element, bool value)
=> element.SetValue(InternalActionProperty, ValueBoxes.BooleanBox(value));
internal static bool GetInternalAction(DependencyObject element)
=> (bool) element.GetValue(InternalActionProperty);
private static void OnSelectedItemsChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
if (d is not ListBox listBox)
{
return;
}
if (GetInternalAction(listBox))
{
return;
}
listBox.SelectionChanged -= OnListBoxSelectionChanged;
listBox.SelectedItems.Clear();
if (e.NewValue is IList selectedItems)
{
foreach (object selectedItem in selectedItems)
{
listBox.SelectedItems.Add(selectedItem);
}
}
listBox.SelectionChanged += OnListBoxSelectionChanged;
}
private static void OnListBoxSelectionChanged(object sender, SelectionChangedEventArgs e)
{
if (sender is ListBox listBox)
{
SetInternalAction(listBox, true);
SetSelectedItems(listBox, listBox.SelectedItems.Cast<object>().ToArray());
SetInternalAction(listBox, false);
}
}
}
@@ -0,0 +1,34 @@
using System.Windows;
using HandyControl.Data;
namespace HandyControl.Controls;
public class MenuAttach
{
public static readonly DependencyProperty PopupVerticalOffsetProperty = DependencyProperty.RegisterAttached(
"PopupVerticalOffset", typeof(double), typeof(MenuAttach), new PropertyMetadata(ValueBoxes.Double0Box));
public static void SetPopupVerticalOffset(DependencyObject element, double value)
=> element.SetValue(PopupVerticalOffsetProperty, value);
public static double GetPopupVerticalOffset(DependencyObject element)
=> (double) element.GetValue(PopupVerticalOffsetProperty);
public static readonly DependencyProperty PopupHorizontalOffsetProperty = DependencyProperty.RegisterAttached(
"PopupHorizontalOffset", typeof(double), typeof(MenuAttach), new PropertyMetadata(ValueBoxes.Double0Box));
public static void SetPopupHorizontalOffset(DependencyObject element, double value)
=> element.SetValue(PopupHorizontalOffsetProperty, value);
public static double GetPopupHorizontalOffset(DependencyObject element)
=> (double) element.GetValue(PopupHorizontalOffsetProperty);
public static readonly DependencyProperty ItemPaddingProperty = DependencyProperty.RegisterAttached(
"ItemPadding", typeof(Thickness), typeof(MenuAttach), new PropertyMetadata(default(Thickness)));
public static void SetItemPadding(DependencyObject element, Thickness value)
=> element.SetValue(ItemPaddingProperty, value);
public static Thickness GetItemPadding(DependencyObject element)
=> (Thickness) element.GetValue(ItemPaddingProperty);
}
@@ -3,108 +3,102 @@ using System.Windows;
using System.Windows.Controls;
using System.Windows.Controls.Primitives;
using HandyControl.Tools;
using HandyControl.Tools.Interop;
namespace HandyControl.Controls
namespace HandyControl.Controls;
public class MenuTopLineAttach
{
public class MenuTopLineAttach
public static readonly DependencyProperty PopupProperty = DependencyProperty.RegisterAttached(
"Popup", typeof(Popup), typeof(MenuTopLineAttach), new PropertyMetadata(default(Popup), OnPopupChanged));
private static void OnPopupChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
public static readonly DependencyProperty PopupProperty = DependencyProperty.RegisterAttached(
"Popup", typeof(Popup), typeof(MenuTopLineAttach), new PropertyMetadata(default(Popup), OnPopupChanged));
var topLine = (FrameworkElement) d;
private static void OnPopupChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
if (e.NewValue is Popup { TemplatedParent: MenuItem menuItem })
{
var topLine = (FrameworkElement) d;
if (e.NewValue is Popup popup)
{
if (popup.TemplatedParent is MenuItem menuItem)
{
SetTopLine(menuItem, topLine);
menuItem.Loaded += MenuItem_Loaded;
}
}
SetTopLine(menuItem, topLine);
menuItem.Loaded += MenuItem_Loaded;
}
private static void MenuItem_Loaded(object sender, RoutedEventArgs e)
{
var menuItem = (FrameworkElement) sender;
menuItem.Unloaded += MenuItem_Unloaded;
var topLine = GetTopLine(menuItem);
var popup = GetPopup(topLine);
if (popup != null)
{
popup.Opened += Popup_Opened;
}
}
private static void MenuItem_Unloaded(object sender, RoutedEventArgs e)
{
var menuItem = (FrameworkElement) sender;
menuItem.Unloaded -= MenuItem_Unloaded;
var topLine = GetTopLine(menuItem);
var popup = GetPopup(topLine);
if (popup != null)
{
popup.Opened -= Popup_Opened;
}
}
private static void Popup_Opened(object sender, EventArgs e)
{
var popup = (Popup) sender;
if (popup.TemplatedParent is MenuItem menuItem)
{
var topLine = GetTopLine(menuItem);
if (topLine == null) return;
topLine.HorizontalAlignment = HorizontalAlignment.Left;
topLine.Width = menuItem.ActualWidth;
topLine.Margin = new Thickness();
var positionLeftTop = menuItem.PointToScreen(new Point());
var positionRightBottom = menuItem.PointToScreen(new Point(menuItem.ActualWidth, menuItem.ActualHeight));
ScreenHelper.FindMonitorRectsFromPoint(InteropMethods.GetCursorPos(), out _, out var workAreaRect);
var panel = VisualHelper.GetParent<Panel>(topLine);
if (positionLeftTop.X < 0)
{
topLine.Margin = new Thickness(positionLeftTop.X - panel.Margin.Left, 0, 0, 0);
}
else if (positionLeftTop.X + panel.ActualWidth > workAreaRect.Right)
{
var overflowWidth = positionRightBottom.X - workAreaRect.Right;
if (overflowWidth > 0)
{
topLine.Width -= overflowWidth + panel.Margin.Right;
}
topLine.HorizontalAlignment = HorizontalAlignment.Left;
topLine.Margin = new Thickness(positionLeftTop.X + panel.ActualWidth - workAreaRect.Right + panel.Margin.Right, 0, 0, 0);
}
if (positionRightBottom.Y > workAreaRect.Bottom)
{
topLine.Width = 0;
topLine.HorizontalAlignment = HorizontalAlignment.Stretch;
topLine.Margin = new Thickness();
}
}
}
public static void SetPopup(DependencyObject element, Popup value)
=> element.SetValue(PopupProperty, value);
public static Popup GetPopup(DependencyObject element)
=> (Popup) element.GetValue(PopupProperty);
internal static readonly DependencyProperty TopLineProperty = DependencyProperty.RegisterAttached(
"TopLine", typeof(FrameworkElement), typeof(MenuTopLineAttach), new PropertyMetadata(default(FrameworkElement)));
internal static void SetTopLine(DependencyObject element, FrameworkElement value)
=> element.SetValue(TopLineProperty, value);
internal static FrameworkElement GetTopLine(DependencyObject element)
=> (FrameworkElement) element.GetValue(TopLineProperty);
}
private static void MenuItem_Loaded(object sender, RoutedEventArgs e)
{
var menuItem = (FrameworkElement) sender;
menuItem.Unloaded += MenuItem_Unloaded;
var topLine = GetTopLine(menuItem);
var popup = GetPopup(topLine);
if (popup != null)
{
popup.Opened += Popup_Opened;
}
}
private static void MenuItem_Unloaded(object sender, RoutedEventArgs e)
{
var menuItem = (FrameworkElement) sender;
menuItem.Unloaded -= MenuItem_Unloaded;
var topLine = GetTopLine(menuItem);
var popup = GetPopup(topLine);
if (popup != null)
{
popup.Opened -= Popup_Opened;
}
}
private static void Popup_Opened(object sender, EventArgs e)
{
var popup = (Popup) sender;
if (popup.TemplatedParent is MenuItem menuItem)
{
var topLine = GetTopLine(menuItem);
if (topLine == null) return;
topLine.HorizontalAlignment = HorizontalAlignment.Left;
topLine.Width = menuItem.ActualWidth;
topLine.Margin = new Thickness();
var positionLeftTop = menuItem.PointToScreen(new Point());
var positionRightBottom = menuItem.PointToScreen(new Point(menuItem.ActualWidth, menuItem.ActualHeight));
ScreenHelper.FindMonitorRectsFromPoint(positionLeftTop, out _, out var workAreaRect);
var panel = VisualHelper.GetParent<Panel>(topLine);
if (positionLeftTop.X < 0)
{
topLine.Margin = new Thickness(positionLeftTop.X - panel.Margin.Left, 0, 0, 0);
}
else if (positionLeftTop.X + panel.ActualWidth > workAreaRect.Right)
{
var overflowWidth = positionRightBottom.X - workAreaRect.Right;
if (overflowWidth > 0)
{
topLine.Width -= overflowWidth + panel.Margin.Right;
}
topLine.HorizontalAlignment = HorizontalAlignment.Left;
topLine.Margin = new Thickness(positionLeftTop.X + panel.ActualWidth - workAreaRect.Right + panel.Margin.Right, 0, 0, 0);
}
if (positionRightBottom.Y > workAreaRect.Bottom)
{
topLine.Width = 0;
topLine.HorizontalAlignment = HorizontalAlignment.Stretch;
topLine.Margin = new Thickness();
}
}
}
public static void SetPopup(DependencyObject element, Popup value)
=> element.SetValue(PopupProperty, value);
public static Popup GetPopup(DependencyObject element)
=> (Popup) element.GetValue(PopupProperty);
internal static readonly DependencyProperty TopLineProperty = DependencyProperty.RegisterAttached(
"TopLine", typeof(FrameworkElement), typeof(MenuTopLineAttach), new PropertyMetadata(default(FrameworkElement)));
internal static void SetTopLine(DependencyObject element, FrameworkElement value)
=> element.SetValue(TopLineProperty, value);
internal static FrameworkElement GetTopLine(DependencyObject element)
=> (FrameworkElement) element.GetValue(TopLineProperty);
}
@@ -2,43 +2,42 @@
using System.Windows.Controls;
using HandyControl.Interactivity;
namespace HandyControl.Controls
namespace HandyControl.Controls;
public class PanelElement
{
public class PanelElement
public static readonly DependencyProperty FluidMoveBehaviorProperty = DependencyProperty.RegisterAttached(
"FluidMoveBehavior", typeof(FluidMoveBehavior), typeof(PanelElement), new PropertyMetadata(default(FluidMoveBehavior), OnFluidMoveBehaviorChanged));
private static void OnFluidMoveBehaviorChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
public static readonly DependencyProperty FluidMoveBehaviorProperty = DependencyProperty.RegisterAttached(
"FluidMoveBehavior", typeof(FluidMoveBehavior), typeof(PanelElement), new PropertyMetadata(default(FluidMoveBehavior), OnFluidMoveBehaviorChanged));
private static void OnFluidMoveBehaviorChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
if (d is Panel panel)
{
if (d is Panel panel)
{
var collection = Interaction.GetBehaviors(panel);
var oldBehavior = GetTempFluidMoveBehavior(panel);
collection.ItemRemoved(oldBehavior);
panel.SetCurrentValue(TempFluidMoveBehaviorProperty, DependencyProperty.UnsetValue);
var collection = Interaction.GetBehaviors(panel);
var oldBehavior = GetTempFluidMoveBehavior(panel);
collection.ItemRemoved(oldBehavior);
panel.SetCurrentValue(TempFluidMoveBehaviorProperty, DependencyProperty.UnsetValue);
if (e.NewValue is FluidMoveBehavior behavior)
{
collection.ItemAdded(behavior);
SetTempFluidMoveBehavior(panel, behavior);
}
if (e.NewValue is FluidMoveBehavior behavior)
{
collection.ItemAdded(behavior);
SetTempFluidMoveBehavior(panel, behavior);
}
}
public static void SetFluidMoveBehavior(DependencyObject element, FluidMoveBehavior value)
=> element.SetValue(FluidMoveBehaviorProperty, value);
public static FluidMoveBehavior GetFluidMoveBehavior(DependencyObject element)
=> (FluidMoveBehavior) element.GetValue(FluidMoveBehaviorProperty);
private static readonly DependencyProperty TempFluidMoveBehaviorProperty = DependencyProperty.RegisterAttached(
"TempFluidMoveBehavior", typeof(FluidMoveBehavior), typeof(PanelElement), new PropertyMetadata(default(FluidMoveBehavior)));
private static void SetTempFluidMoveBehavior(DependencyObject element, FluidMoveBehavior value)
=> element.SetValue(TempFluidMoveBehaviorProperty, value);
private static FluidMoveBehavior GetTempFluidMoveBehavior(DependencyObject element)
=> (FluidMoveBehavior) element.GetValue(TempFluidMoveBehaviorProperty);
}
public static void SetFluidMoveBehavior(DependencyObject element, FluidMoveBehavior value)
=> element.SetValue(FluidMoveBehaviorProperty, value);
public static FluidMoveBehavior GetFluidMoveBehavior(DependencyObject element)
=> (FluidMoveBehavior) element.GetValue(FluidMoveBehaviorProperty);
private static readonly DependencyProperty TempFluidMoveBehaviorProperty = DependencyProperty.RegisterAttached(
"TempFluidMoveBehavior", typeof(FluidMoveBehavior), typeof(PanelElement), new PropertyMetadata(default(FluidMoveBehavior)));
private static void SetTempFluidMoveBehavior(DependencyObject element, FluidMoveBehavior value)
=> element.SetValue(TempFluidMoveBehaviorProperty, value);
private static FluidMoveBehavior GetTempFluidMoveBehavior(DependencyObject element)
=> (FluidMoveBehavior) element.GetValue(TempFluidMoveBehaviorProperty);
}
@@ -1,54 +1,53 @@
using System.Windows;
using HandyControl.Data;
namespace HandyControl.Controls
namespace HandyControl.Controls;
public class PasswordBoxAttach
{
public class PasswordBoxAttach
/// <summary>
/// 密码长度
/// </summary>
public static readonly DependencyProperty PasswordLengthProperty = DependencyProperty.RegisterAttached(
"PasswordLength", typeof(int), typeof(PasswordBoxAttach), new PropertyMetadata(ValueBoxes.Int0Box));
public static void SetPasswordLength(DependencyObject element, int value) => element.SetValue(PasswordLengthProperty, value);
public static int GetPasswordLength(DependencyObject element) => (int) element.GetValue(PasswordLengthProperty);
/// <summary>
/// 是否监测
/// </summary>
public static readonly DependencyProperty IsMonitoringProperty = DependencyProperty.RegisterAttached(
"IsMonitoring", typeof(bool), typeof(PasswordBoxAttach), new FrameworkPropertyMetadata(ValueBoxes.FalseBox, FrameworkPropertyMetadataOptions.Inherits, OnIsMonitoringChanged));
private static void OnIsMonitoringChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
/// <summary>
/// 密码长度
/// </summary>
public static readonly DependencyProperty PasswordLengthProperty = DependencyProperty.RegisterAttached(
"PasswordLength", typeof(int), typeof(PasswordBoxAttach), new PropertyMetadata(ValueBoxes.Int0Box));
public static void SetPasswordLength(DependencyObject element, int value) => element.SetValue(PasswordLengthProperty, value);
public static int GetPasswordLength(DependencyObject element) => (int) element.GetValue(PasswordLengthProperty);
/// <summary>
/// 是否监测
/// </summary>
public static readonly DependencyProperty IsMonitoringProperty = DependencyProperty.RegisterAttached(
"IsMonitoring", typeof(bool), typeof(PasswordBoxAttach), new FrameworkPropertyMetadata(ValueBoxes.FalseBox, FrameworkPropertyMetadataOptions.Inherits, OnIsMonitoringChanged));
private static void OnIsMonitoringChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
if (d is System.Windows.Controls.PasswordBox passwordBox)
{
if (d is System.Windows.Controls.PasswordBox passwordBox)
if (e.NewValue is bool boolValue)
{
if (e.NewValue is bool boolValue)
if (boolValue)
{
if (boolValue)
{
passwordBox.PasswordChanged += PasswordChanged;
}
else
{
passwordBox.PasswordChanged -= PasswordChanged;
}
passwordBox.PasswordChanged += PasswordChanged;
}
else
{
passwordBox.PasswordChanged -= PasswordChanged;
}
}
}
}
public static void SetIsMonitoring(DependencyObject element, bool value) => element.SetValue(IsMonitoringProperty, ValueBoxes.BooleanBox(value));
public static void SetIsMonitoring(DependencyObject element, bool value) => element.SetValue(IsMonitoringProperty, ValueBoxes.BooleanBox(value));
public static bool GetIsMonitoring(DependencyObject element) => (bool) element.GetValue(IsMonitoringProperty);
public static bool GetIsMonitoring(DependencyObject element) => (bool) element.GetValue(IsMonitoringProperty);
private static void PasswordChanged(object sender, RoutedEventArgs e)
private static void PasswordChanged(object sender, RoutedEventArgs e)
{
if (sender is System.Windows.Controls.PasswordBox passwordBox)
{
if (sender is System.Windows.Controls.PasswordBox passwordBox)
{
SetPasswordLength(passwordBox, passwordBox.Password.Length);
}
SetPasswordLength(passwordBox, passwordBox.Password.Length);
}
}
}
@@ -4,42 +4,41 @@ using System.Windows.Shapes;
using HandyControl.Data;
using HandyControl.Tools.Converter;
namespace HandyControl.Controls
{
public class RectangleAttach
{
public static readonly DependencyProperty CircularProperty = DependencyProperty.RegisterAttached(
"Circular", typeof(bool), typeof(RectangleAttach), new PropertyMetadata(ValueBoxes.FalseBox, OnCircularChanged));
namespace HandyControl.Controls;
private static void OnCircularChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
public class RectangleAttach
{
public static readonly DependencyProperty CircularProperty = DependencyProperty.RegisterAttached(
"Circular", typeof(bool), typeof(RectangleAttach), new PropertyMetadata(ValueBoxes.FalseBox, OnCircularChanged));
private static void OnCircularChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
if (d is Rectangle rectangle)
{
if (d is Rectangle rectangle)
if ((bool) e.NewValue)
{
if ((bool) e.NewValue)
var binding = new MultiBinding
{
var binding = new MultiBinding
{
Converter = new RectangleCircularConverter()
};
binding.Bindings.Add(new Binding(FrameworkElement.ActualWidthProperty.Name) { Source = rectangle });
binding.Bindings.Add(new Binding(FrameworkElement.ActualHeightProperty.Name) { Source = rectangle });
rectangle.SetBinding(Rectangle.RadiusXProperty, binding);
rectangle.SetBinding(Rectangle.RadiusYProperty, binding);
}
else
{
BindingOperations.ClearBinding(rectangle, FrameworkElement.ActualWidthProperty);
BindingOperations.ClearBinding(rectangle, FrameworkElement.ActualHeightProperty);
BindingOperations.ClearBinding(rectangle, Rectangle.RadiusXProperty);
BindingOperations.ClearBinding(rectangle, Rectangle.RadiusYProperty);
}
Converter = new RectangleCircularConverter()
};
binding.Bindings.Add(new Binding(FrameworkElement.ActualWidthProperty.Name) { Source = rectangle });
binding.Bindings.Add(new Binding(FrameworkElement.ActualHeightProperty.Name) { Source = rectangle });
rectangle.SetBinding(Rectangle.RadiusXProperty, binding);
rectangle.SetBinding(Rectangle.RadiusYProperty, binding);
}
else
{
BindingOperations.ClearBinding(rectangle, FrameworkElement.ActualWidthProperty);
BindingOperations.ClearBinding(rectangle, FrameworkElement.ActualHeightProperty);
BindingOperations.ClearBinding(rectangle, Rectangle.RadiusXProperty);
BindingOperations.ClearBinding(rectangle, Rectangle.RadiusYProperty);
}
}
public static void SetCircular(DependencyObject element, bool value)
=> element.SetValue(CircularProperty, ValueBoxes.BooleanBox(value));
public static bool GetCircular(DependencyObject element)
=> (bool) element.GetValue(CircularProperty);
}
public static void SetCircular(DependencyObject element, bool value)
=> element.SetValue(CircularProperty, ValueBoxes.BooleanBox(value));
public static bool GetCircular(DependencyObject element)
=> (bool) element.GetValue(CircularProperty);
}
@@ -0,0 +1,100 @@
using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using HandyControl.Data;
using HandyControl.Tools;
namespace HandyControl.Controls;
public class ScrollViewerAttach
{
public static readonly DependencyProperty AutoHideProperty = DependencyProperty.RegisterAttached(
"AutoHide", typeof(bool), typeof(ScrollViewerAttach), new FrameworkPropertyMetadata(ValueBoxes.TrueBox, FrameworkPropertyMetadataOptions.Inherits));
public static void SetAutoHide(DependencyObject element, bool value)
=> element.SetValue(AutoHideProperty, ValueBoxes.BooleanBox(value));
public static bool GetAutoHide(DependencyObject element)
=> (bool) element.GetValue(AutoHideProperty);
public static readonly DependencyProperty OrientationProperty = DependencyProperty.RegisterAttached(
"Orientation", typeof(Orientation), typeof(ScrollViewerAttach), new FrameworkPropertyMetadata(ValueBoxes.VerticalBox, FrameworkPropertyMetadataOptions.Inherits, OnOrientationChanged));
private static void OnOrientationChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
if (d is ScrollViewer)
{
return;
}
if (d is System.Windows.Controls.ScrollViewer scrollViewer)
{
if ((Orientation) e.NewValue == Orientation.Horizontal)
{
scrollViewer.PreviewMouseWheel += ScrollViewerPreviewMouseWheel;
}
else
{
scrollViewer.PreviewMouseWheel -= ScrollViewerPreviewMouseWheel;
}
}
void ScrollViewerPreviewMouseWheel(object sender, MouseWheelEventArgs args)
{
var scrollViewerNative = (System.Windows.Controls.ScrollViewer) sender;
scrollViewerNative.ScrollToHorizontalOffset(Math.Min(Math.Max(0, scrollViewerNative.HorizontalOffset - args.Delta), scrollViewerNative.ScrollableWidth));
args.Handled = true;
}
}
public static void SetOrientation(DependencyObject element, Orientation value)
=> element.SetValue(OrientationProperty, ValueBoxes.OrientationBox(value));
public static Orientation GetOrientation(DependencyObject element)
=> (Orientation) element.GetValue(OrientationProperty);
public static readonly DependencyProperty IsDisabledProperty = DependencyProperty.RegisterAttached(
"IsDisabled", typeof(bool), typeof(ScrollViewerAttach), new PropertyMetadata(ValueBoxes.FalseBox, OnIsDisabledChanged));
private static void OnIsDisabledChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
if (d is UIElement element)
{
if ((bool) e.NewValue)
{
element.PreviewMouseWheel += ScrollViewerPreviewMouseWheel;
}
else
{
element.PreviewMouseWheel -= ScrollViewerPreviewMouseWheel;
}
}
void ScrollViewerPreviewMouseWheel(object sender, MouseWheelEventArgs args)
{
if (args.Handled)
{
return;
}
args.Handled = true;
if (VisualHelper.GetParent<System.Windows.Controls.ScrollViewer>((UIElement) sender) is { } scrollViewer)
{
scrollViewer.RaiseEvent(new MouseWheelEventArgs(args.MouseDevice, args.Timestamp, args.Delta)
{
RoutedEvent = UIElement.MouseWheelEvent,
Source = sender
});
}
}
}
public static void SetIsDisabled(DependencyObject element, bool value)
=> element.SetValue(IsDisabledProperty, ValueBoxes.BooleanBox(value));
public static bool GetIsDisabled(DependencyObject element)
=> (bool) element.GetValue(IsDisabledProperty);
}
@@ -1,28 +1,27 @@
using System.Windows;
using HandyControl.Data;
namespace HandyControl.Controls
namespace HandyControl.Controls;
public class StatusSwitchElement
{
public class StatusSwitchElement
{
/// <summary>
/// 选中时展示的元素
/// </summary>
public static readonly DependencyProperty CheckedElementProperty = DependencyProperty.RegisterAttached(
"CheckedElement", typeof(object), typeof(StatusSwitchElement), new PropertyMetadata(default(object)));
/// <summary>
/// 选中时展示的元素
/// </summary>
public static readonly DependencyProperty CheckedElementProperty = DependencyProperty.RegisterAttached(
"CheckedElement", typeof(object), typeof(StatusSwitchElement), new PropertyMetadata(default(object)));
public static void SetCheckedElement(DependencyObject element, object value) => element.SetValue(CheckedElementProperty, value);
public static void SetCheckedElement(DependencyObject element, object value) => element.SetValue(CheckedElementProperty, value);
public static object GetCheckedElement(DependencyObject element) => element.GetValue(CheckedElementProperty);
public static object GetCheckedElement(DependencyObject element) => element.GetValue(CheckedElementProperty);
/// <summary>
/// 是否隐藏元素
/// </summary>
public static readonly DependencyProperty HideUncheckedElementProperty = DependencyProperty.RegisterAttached(
"HideUncheckedElement", typeof(bool), typeof(StatusSwitchElement), new PropertyMetadata(ValueBoxes.FalseBox));
/// <summary>
/// 是否隐藏元素
/// </summary>
public static readonly DependencyProperty HideUncheckedElementProperty = DependencyProperty.RegisterAttached(
"HideUncheckedElement", typeof(bool), typeof(StatusSwitchElement), new PropertyMetadata(ValueBoxes.FalseBox));
public static void SetHideUncheckedElement(DependencyObject element, bool value) => element.SetValue(HideUncheckedElementProperty, ValueBoxes.BooleanBox(value));
public static void SetHideUncheckedElement(DependencyObject element, bool value) => element.SetValue(HideUncheckedElementProperty, ValueBoxes.BooleanBox(value));
public static bool GetHideUncheckedElement(DependencyObject element) => (bool) element.GetValue(HideUncheckedElementProperty);
}
public static bool GetHideUncheckedElement(DependencyObject element) => (bool) element.GetValue(HideUncheckedElementProperty);
}
@@ -1,53 +1,70 @@
using System.Windows;
using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;
using HandyControl.Data;
using HandyControl.Tools.Helper;
namespace HandyControl.Controls
namespace HandyControl.Controls;
public class TextBlockAttach
{
public class TextBlockAttach
public static readonly DependencyProperty AutoTooltipProperty = DependencyProperty.RegisterAttached(
"AutoTooltip", typeof(bool), typeof(TextBlockAttach), new PropertyMetadata(ValueBoxes.FalseBox, OnAutoTooltipChanged));
private static void OnAutoTooltipChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
public static readonly DependencyProperty AutoTooltipProperty = DependencyProperty.RegisterAttached(
"AutoTooltip", typeof(bool), typeof(TextBlockAttach), new PropertyMetadata(ValueBoxes.FalseBox, OnAutoTooltipChanged));
private static void OnAutoTooltipChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
if (d is TextBlock ctl)
{
if (d is TextBlock ctl)
if ((bool) e.NewValue)
{
if ((bool) e.NewValue)
{
ctl.TextTrimming = TextTrimming.WordEllipsis;
UpdateTooltip(ctl);
ctl.SizeChanged += TextBlock_SizeChanged;
}
else
{
ctl.ClearValue(TextBlock.TextTrimmingProperty);
ctl.SizeChanged -= TextBlock_SizeChanged;
}
UpdateTooltip(ctl);
ctl.SizeChanged += TextBlock_SizeChanged;
}
}
public static void SetAutoTooltip(DependencyObject element, bool value)
=> element.SetValue(AutoTooltipProperty, ValueBoxes.BooleanBox(value));
public static bool GetAutoTooltip(DependencyObject element)
=> (bool) element.GetValue(AutoTooltipProperty);
private static void TextBlock_SizeChanged(object sender, SizeChangedEventArgs e)
{
if (sender is TextBlock textBlock)
else
{
UpdateTooltip(textBlock);
ctl.SizeChanged -= TextBlock_SizeChanged;
}
}
}
private static void UpdateTooltip(TextBlock textBlock)
public static void SetAutoTooltip(DependencyObject element, bool value)
=> element.SetValue(AutoTooltipProperty, ValueBoxes.BooleanBox(value));
public static bool GetAutoTooltip(DependencyObject element)
=> (bool) element.GetValue(AutoTooltipProperty);
private static void TextBlock_SizeChanged(object sender, SizeChangedEventArgs e)
{
if (sender is TextBlock textBlock)
{
textBlock.Measure(new Size(double.PositiveInfinity, double.PositiveInfinity));
var width = textBlock.DesiredSize.Width - textBlock.Margin.Left - textBlock.Margin.Right;
UpdateTooltip(textBlock);
}
}
ToolTipService.SetToolTip(textBlock,
textBlock.RenderSize.Width > width || textBlock.ActualWidth < width ? textBlock.Text : null);
private static void UpdateTooltip(TextBlock textBlock)
{
textBlock.Measure(new Size(double.PositiveInfinity, double.PositiveInfinity));
var width = textBlock.DesiredSize.Width - textBlock.Margin.Left - textBlock.Margin.Right;
// the code Math.Abs(CalcTextWidth(textBlock) - width) > 1 is not elegant end even bugly,
// while it does solve the problem of Tooltip failure in some cases
if (textBlock.RenderSize.Width > width || textBlock.ActualWidth < width || Math.Abs(CalcTextWidth(textBlock) - width) > 1)
{
ToolTipService.SetToolTip(textBlock, textBlock.Text);
}
else
{
ToolTipService.SetToolTip(textBlock, null);
}
}
private static double CalcTextWidth(TextBlock textBlock)
{
var formattedText = TextHelper.CreateFormattedText(textBlock.Text, textBlock.FlowDirection,
new Typeface(textBlock.FontFamily, textBlock.FontStyle, textBlock.FontWeight, textBlock.FontStretch),
textBlock.FontSize);
return formattedText.WidthIncludingTrailingWhitespace;
}
}
@@ -1,31 +1,30 @@
using System.Windows;
using HandyControl.Data;
namespace HandyControl.Controls
namespace HandyControl.Controls;
public class TipElement
{
public class TipElement
{
public static readonly DependencyProperty VisibilityProperty = DependencyProperty.RegisterAttached(
"Visibility", typeof(Visibility), typeof(TipElement), new PropertyMetadata(Visibility.Collapsed));
public static readonly DependencyProperty VisibilityProperty = DependencyProperty.RegisterAttached(
"Visibility", typeof(Visibility), typeof(TipElement), new PropertyMetadata(Visibility.Collapsed));
public static void SetVisibility(DependencyObject element, Visibility value) => element.SetValue(VisibilityProperty, value);
public static void SetVisibility(DependencyObject element, Visibility value) => element.SetValue(VisibilityProperty, value);
public static Visibility GetVisibility(DependencyObject element) => (Visibility) element.GetValue(VisibilityProperty);
public static Visibility GetVisibility(DependencyObject element) => (Visibility) element.GetValue(VisibilityProperty);
public static readonly DependencyProperty PlacementProperty = DependencyProperty.RegisterAttached(
"Placement", typeof(PlacementType), typeof(TipElement), new PropertyMetadata(default(PlacementType)));
public static readonly DependencyProperty PlacementProperty = DependencyProperty.RegisterAttached(
"Placement", typeof(PlacementType), typeof(TipElement), new PropertyMetadata(default(PlacementType)));
public static void SetPlacement(DependencyObject element, PlacementType value) => element.SetValue(PlacementProperty, value);
public static void SetPlacement(DependencyObject element, PlacementType value) => element.SetValue(PlacementProperty, value);
public static PlacementType GetPlacement(DependencyObject element) => (PlacementType) element.GetValue(PlacementProperty);
public static PlacementType GetPlacement(DependencyObject element) => (PlacementType) element.GetValue(PlacementProperty);
public static readonly DependencyProperty StringFormatProperty = DependencyProperty.RegisterAttached(
"StringFormat", typeof(string), typeof(TipElement), new PropertyMetadata("#0.0"));
public static readonly DependencyProperty StringFormatProperty = DependencyProperty.RegisterAttached(
"StringFormat", typeof(string), typeof(TipElement), new PropertyMetadata("#0.0"));
public static void SetStringFormat(DependencyObject element, string value)
=> element.SetValue(StringFormatProperty, value);
public static void SetStringFormat(DependencyObject element, string value)
=> element.SetValue(StringFormatProperty, value);
public static string GetStringFormat(DependencyObject element)
=> (string) element.GetValue(StringFormatProperty);
}
public static string GetStringFormat(DependencyObject element)
=> (string) element.GetValue(StringFormatProperty);
}
@@ -2,78 +2,116 @@
using System.Windows.Media;
using HandyControl.Data;
namespace HandyControl.Controls
namespace HandyControl.Controls;
public class TitleElement
{
public class TitleElement
{
public static readonly DependencyProperty TitleProperty = DependencyProperty.RegisterAttached(
"Title", typeof(string), typeof(TitleElement), new PropertyMetadata(default(string)));
public static readonly DependencyProperty TitleProperty = DependencyProperty.RegisterAttached(
"Title", typeof(string), typeof(TitleElement), new PropertyMetadata(default(string)));
public static void SetTitle(DependencyObject element, string value)
=> element.SetValue(TitleProperty, value);
public static void SetTitle(DependencyObject element, string value)
=> element.SetValue(TitleProperty, value);
public static string GetTitle(DependencyObject element)
=> (string) element.GetValue(TitleProperty);
public static string GetTitle(DependencyObject element)
=> (string) element.GetValue(TitleProperty);
public static readonly DependencyProperty BackgroundProperty = DependencyProperty.RegisterAttached(
"Background", typeof(Brush), typeof(TitleElement), new FrameworkPropertyMetadata(default(Brush), FrameworkPropertyMetadataOptions.Inherits));
public static readonly DependencyProperty BackgroundProperty = DependencyProperty.RegisterAttached(
"Background", typeof(Brush), typeof(TitleElement), new FrameworkPropertyMetadata(default(Brush), FrameworkPropertyMetadataOptions.Inherits));
public static void SetBackground(DependencyObject element, Brush value)
=> element.SetValue(BackgroundProperty, value);
public static void SetBackground(DependencyObject element, Brush value)
=> element.SetValue(BackgroundProperty, value);
public static Brush GetBackground(DependencyObject element)
=> (Brush) element.GetValue(BackgroundProperty);
public static Brush GetBackground(DependencyObject element)
=> (Brush) element.GetValue(BackgroundProperty);
public static readonly DependencyProperty ForegroundProperty = DependencyProperty.RegisterAttached(
"Foreground", typeof(Brush), typeof(TitleElement), new FrameworkPropertyMetadata(default(Brush), FrameworkPropertyMetadataOptions.Inherits));
public static readonly DependencyProperty ForegroundProperty = DependencyProperty.RegisterAttached(
"Foreground", typeof(Brush), typeof(TitleElement), new FrameworkPropertyMetadata(default(Brush), FrameworkPropertyMetadataOptions.Inherits));
public static void SetForeground(DependencyObject element, Brush value)
=> element.SetValue(ForegroundProperty, value);
public static void SetForeground(DependencyObject element, Brush value)
=> element.SetValue(ForegroundProperty, value);
public static Brush GetForeground(DependencyObject element)
=> (Brush) element.GetValue(ForegroundProperty);
public static Brush GetForeground(DependencyObject element)
=> (Brush) element.GetValue(ForegroundProperty);
public static readonly DependencyProperty BorderBrushProperty = DependencyProperty.RegisterAttached(
"BorderBrush", typeof(Brush), typeof(TitleElement), new FrameworkPropertyMetadata(default(Brush), FrameworkPropertyMetadataOptions.Inherits));
public static readonly DependencyProperty BorderBrushProperty = DependencyProperty.RegisterAttached(
"BorderBrush", typeof(Brush), typeof(TitleElement), new FrameworkPropertyMetadata(default(Brush), FrameworkPropertyMetadataOptions.Inherits));
public static void SetBorderBrush(DependencyObject element, Brush value)
=> element.SetValue(BorderBrushProperty, value);
public static void SetBorderBrush(DependencyObject element, Brush value)
=> element.SetValue(BorderBrushProperty, value);
public static Brush GetBorderBrush(DependencyObject element)
=> (Brush) element.GetValue(BorderBrushProperty);
public static Brush GetBorderBrush(DependencyObject element)
=> (Brush) element.GetValue(BorderBrushProperty);
public static readonly DependencyProperty TitlePlacementProperty = DependencyProperty.RegisterAttached(
"TitlePlacement", typeof(TitlePlacementType), typeof(TitleElement), new FrameworkPropertyMetadata(TitlePlacementType.Top, FrameworkPropertyMetadataOptions.Inherits));
public static readonly DependencyProperty TitlePlacementProperty = DependencyProperty.RegisterAttached(
"TitlePlacement", typeof(TitlePlacementType), typeof(TitleElement), new FrameworkPropertyMetadata(TitlePlacementType.Top, FrameworkPropertyMetadataOptions.Inherits));
public static void SetTitlePlacement(DependencyObject element, TitlePlacementType value)
=> element.SetValue(TitlePlacementProperty, value);
public static void SetTitlePlacement(DependencyObject element, TitlePlacementType value)
=> element.SetValue(TitlePlacementProperty, value);
public static TitlePlacementType GetTitlePlacement(DependencyObject element)
=> (TitlePlacementType) element.GetValue(TitlePlacementProperty);
public static TitlePlacementType GetTitlePlacement(DependencyObject element)
=> (TitlePlacementType) element.GetValue(TitlePlacementProperty);
public static readonly DependencyProperty TitleWidthProperty = DependencyProperty.RegisterAttached(
"TitleWidth", typeof(GridLength), typeof(TitleElement), new FrameworkPropertyMetadata(GridLength.Auto, FrameworkPropertyMetadataOptions.Inherits));
public static readonly DependencyProperty TitleWidthProperty = DependencyProperty.RegisterAttached(
"TitleWidth", typeof(GridLength), typeof(TitleElement), new FrameworkPropertyMetadata(GridLength.Auto, FrameworkPropertyMetadataOptions.Inherits));
public static void SetTitleWidth(DependencyObject element, GridLength value) => element.SetValue(TitleWidthProperty, value);
public static void SetTitleWidth(DependencyObject element, GridLength value) => element.SetValue(TitleWidthProperty, value);
public static GridLength GetTitleWidth(DependencyObject element) => (GridLength) element.GetValue(TitleWidthProperty);
public static GridLength GetTitleWidth(DependencyObject element) => (GridLength) element.GetValue(TitleWidthProperty);
public static readonly DependencyProperty HorizontalAlignmentProperty = DependencyProperty.RegisterAttached(
"HorizontalAlignment", typeof(HorizontalAlignment), typeof(TitleElement), new FrameworkPropertyMetadata(default(HorizontalAlignment), FrameworkPropertyMetadataOptions.Inherits));
public static readonly DependencyProperty HorizontalAlignmentProperty = DependencyProperty.RegisterAttached(
"HorizontalAlignment", typeof(HorizontalAlignment), typeof(TitleElement), new FrameworkPropertyMetadata(default(HorizontalAlignment), FrameworkPropertyMetadataOptions.Inherits));
public static void SetHorizontalAlignment(DependencyObject element, HorizontalAlignment value)
=> element.SetValue(HorizontalAlignmentProperty, value);
public static void SetHorizontalAlignment(DependencyObject element, HorizontalAlignment value)
=> element.SetValue(HorizontalAlignmentProperty, value);
public static HorizontalAlignment GetHorizontalAlignment(DependencyObject element)
=> (HorizontalAlignment) element.GetValue(HorizontalAlignmentProperty);
public static HorizontalAlignment GetHorizontalAlignment(DependencyObject element)
=> (HorizontalAlignment) element.GetValue(HorizontalAlignmentProperty);
public static readonly DependencyProperty VerticalAlignmentProperty = DependencyProperty.RegisterAttached(
"VerticalAlignment", typeof(VerticalAlignment), typeof(TitleElement), new FrameworkPropertyMetadata(default(VerticalAlignment), FrameworkPropertyMetadataOptions.Inherits));
public static readonly DependencyProperty VerticalAlignmentProperty = DependencyProperty.RegisterAttached(
"VerticalAlignment", typeof(VerticalAlignment), typeof(TitleElement), new FrameworkPropertyMetadata(default(VerticalAlignment), FrameworkPropertyMetadataOptions.Inherits));
public static void SetVerticalAlignment(DependencyObject element, VerticalAlignment value)
=> element.SetValue(VerticalAlignmentProperty, value);
public static void SetVerticalAlignment(DependencyObject element, VerticalAlignment value)
=> element.SetValue(VerticalAlignmentProperty, value);
public static VerticalAlignment GetVerticalAlignment(DependencyObject element)
=> (VerticalAlignment) element.GetValue(VerticalAlignmentProperty);
}
public static VerticalAlignment GetVerticalAlignment(DependencyObject element)
=> (VerticalAlignment) element.GetValue(VerticalAlignmentProperty);
public static readonly DependencyProperty MarginOnTheLeftProperty = DependencyProperty.RegisterAttached(
"MarginOnTheLeft", typeof(Thickness), typeof(TitleElement), new FrameworkPropertyMetadata(default(Thickness), FrameworkPropertyMetadataOptions.Inherits));
public static void SetMarginOnTheLeft(DependencyObject element, Thickness value)
=> element.SetValue(MarginOnTheLeftProperty, value);
public static Thickness GetMarginOnTheLeft(DependencyObject element)
=> (Thickness) element.GetValue(MarginOnTheLeftProperty);
public static readonly DependencyProperty MarginOnTheTopProperty = DependencyProperty.RegisterAttached(
"MarginOnTheTop", typeof(Thickness), typeof(TitleElement), new FrameworkPropertyMetadata(default(Thickness), FrameworkPropertyMetadataOptions.Inherits));
public static void SetMarginOnTheTop(DependencyObject element, Thickness value)
=> element.SetValue(MarginOnTheTopProperty, value);
public static Thickness GetMarginOnTheTop(DependencyObject element)
=> (Thickness) element.GetValue(MarginOnTheTopProperty);
public static readonly DependencyProperty PaddingProperty = DependencyProperty.RegisterAttached(
"Padding", typeof(Thickness), typeof(TitleElement), new FrameworkPropertyMetadata(default(Thickness), FrameworkPropertyMetadataOptions.Inherits));
public static void SetPadding(DependencyObject element, Thickness value) => element.SetValue(PaddingProperty, value);
public static Thickness GetPadding(DependencyObject element) => (Thickness) element.GetValue(PaddingProperty);
public static readonly DependencyProperty MinHeightProperty =
DependencyProperty.RegisterAttached("MinHeight", typeof(double), typeof(TitleElement), new PropertyMetadata(ValueBoxes.Double0Box));
public static double GetMinHeight(DependencyObject obj) => (double) obj.GetValue(MinHeightProperty);
public static void SetMinHeight(DependencyObject obj, double value) => obj.SetValue(MinHeightProperty, value);
public static readonly DependencyProperty MinWidthProperty =
DependencyProperty.RegisterAttached("MinWidth", typeof(double), typeof(TitleElement), new PropertyMetadata(ValueBoxes.Double0Box));
public static double GetMinWidth(DependencyObject obj) => (double) obj.GetValue(MinWidthProperty);
public static void SetMinWidth(DependencyObject obj, double value) => obj.SetValue(MinWidthProperty, value);
}
@@ -0,0 +1,14 @@
using System.Windows;
using HandyControl.Data;
namespace HandyControl.Controls;
public class ToggleButtonAttach
{
public static readonly DependencyProperty ShowLabelProperty = DependencyProperty.RegisterAttached(
"ShowLabel", typeof(bool), typeof(ToggleButtonAttach), new FrameworkPropertyMetadata(ValueBoxes.FalseBox, FrameworkPropertyMetadataOptions.Inherits));
public static void SetShowLabel(DependencyObject element, bool value) => element.SetValue(ShowLabelProperty, ValueBoxes.BooleanBox(value));
public static bool GetShowLabel(DependencyObject element) => (bool) element.GetValue(ShowLabelProperty);
}
@@ -1,53 +1,52 @@
using System.Windows;
using System.Windows.Media;
namespace HandyControl.Controls
namespace HandyControl.Controls;
public class VisualElement
{
public class VisualElement
{
public static readonly DependencyProperty HighlightBrushProperty = DependencyProperty.RegisterAttached(
"HighlightBrush", typeof(Brush), typeof(VisualElement), new FrameworkPropertyMetadata(default(Brush), FrameworkPropertyMetadataOptions.Inherits));
public static readonly DependencyProperty HighlightBrushProperty = DependencyProperty.RegisterAttached(
"HighlightBrush", typeof(Brush), typeof(VisualElement), new FrameworkPropertyMetadata(default(Brush), FrameworkPropertyMetadataOptions.Inherits));
public static void SetHighlightBrush(DependencyObject element, Brush value)
=> element.SetValue(HighlightBrushProperty, value);
public static void SetHighlightBrush(DependencyObject element, Brush value)
=> element.SetValue(HighlightBrushProperty, value);
public static Brush GetHighlightBrush(DependencyObject element)
=> (Brush) element.GetValue(HighlightBrushProperty);
public static Brush GetHighlightBrush(DependencyObject element)
=> (Brush) element.GetValue(HighlightBrushProperty);
public static readonly DependencyProperty HighlightBackgroundProperty = DependencyProperty.RegisterAttached(
"HighlightBackground", typeof(Brush), typeof(VisualElement), new FrameworkPropertyMetadata(default(Brush), FrameworkPropertyMetadataOptions.Inherits));
public static readonly DependencyProperty HighlightBackgroundProperty = DependencyProperty.RegisterAttached(
"HighlightBackground", typeof(Brush), typeof(VisualElement), new FrameworkPropertyMetadata(default(Brush), FrameworkPropertyMetadataOptions.Inherits));
public static void SetHighlightBackground(DependencyObject element, Brush value)
=> element.SetValue(HighlightBackgroundProperty, value);
public static void SetHighlightBackground(DependencyObject element, Brush value)
=> element.SetValue(HighlightBackgroundProperty, value);
public static Brush GetHighlightBackground(DependencyObject element)
=> (Brush) element.GetValue(HighlightBackgroundProperty);
public static Brush GetHighlightBackground(DependencyObject element)
=> (Brush) element.GetValue(HighlightBackgroundProperty);
public static readonly DependencyProperty HighlightBorderBrushProperty = DependencyProperty.RegisterAttached(
"HighlightBorderBrush", typeof(Brush), typeof(VisualElement), new FrameworkPropertyMetadata(default(Brush), FrameworkPropertyMetadataOptions.Inherits));
public static readonly DependencyProperty HighlightBorderBrushProperty = DependencyProperty.RegisterAttached(
"HighlightBorderBrush", typeof(Brush), typeof(VisualElement), new FrameworkPropertyMetadata(default(Brush), FrameworkPropertyMetadataOptions.Inherits));
public static void SetHighlightBorderBrush(DependencyObject element, Brush value)
=> element.SetValue(HighlightBorderBrushProperty, value);
public static void SetHighlightBorderBrush(DependencyObject element, Brush value)
=> element.SetValue(HighlightBorderBrushProperty, value);
public static Brush GetHighlightBorderBrush(DependencyObject element)
=> (Brush) element.GetValue(HighlightBorderBrushProperty);
public static Brush GetHighlightBorderBrush(DependencyObject element)
=> (Brush) element.GetValue(HighlightBorderBrushProperty);
public static readonly DependencyProperty HighlightForegroundProperty = DependencyProperty.RegisterAttached(
"HighlightForeground", typeof(Brush), typeof(VisualElement), new FrameworkPropertyMetadata(default(Brush), FrameworkPropertyMetadataOptions.Inherits));
public static readonly DependencyProperty HighlightForegroundProperty = DependencyProperty.RegisterAttached(
"HighlightForeground", typeof(Brush), typeof(VisualElement), new FrameworkPropertyMetadata(default(Brush), FrameworkPropertyMetadataOptions.Inherits));
public static void SetHighlightForeground(DependencyObject element, Brush value)
=> element.SetValue(HighlightForegroundProperty, value);
public static void SetHighlightForeground(DependencyObject element, Brush value)
=> element.SetValue(HighlightForegroundProperty, value);
public static Brush GetHighlightForeground(DependencyObject element)
=> (Brush) element.GetValue(HighlightForegroundProperty);
public static Brush GetHighlightForeground(DependencyObject element)
=> (Brush) element.GetValue(HighlightForegroundProperty);
public static readonly DependencyProperty TextProperty = DependencyProperty.RegisterAttached(
"Text", typeof(string), typeof(VisualElement), new PropertyMetadata(default(string)));
public static readonly DependencyProperty TextProperty = DependencyProperty.RegisterAttached(
"Text", typeof(string), typeof(VisualElement), new PropertyMetadata(default(string)));
public static void SetText(DependencyObject element, string value)
=> element.SetValue(TextProperty, value);
public static void SetText(DependencyObject element, string value)
=> element.SetValue(TextProperty, value);
public static string GetText(DependencyObject element)
=> (string) element.GetValue(TextProperty);
}
public static string GetText(DependencyObject element)
=> (string) element.GetValue(TextProperty);
}
@@ -6,144 +6,152 @@ using System.Windows.Interop;
using HandyControl.Data;
using HandyControl.Tools.Interop;
namespace HandyControl.Controls
namespace HandyControl.Controls;
public static class WindowAttach
{
public static class WindowAttach
public static readonly DependencyProperty IsDragElementProperty = DependencyProperty.RegisterAttached(
"IsDragElement", typeof(bool), typeof(WindowAttach), new PropertyMetadata(ValueBoxes.FalseBox, OnIsDragElementChanged));
public static void SetIsDragElement(DependencyObject element, bool value)
=> element.SetValue(IsDragElementProperty, ValueBoxes.BooleanBox(value));
public static bool GetIsDragElement(DependencyObject element)
=> (bool) element.GetValue(IsDragElementProperty);
private static void OnIsDragElementChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
public static readonly DependencyProperty IsDragElementProperty = DependencyProperty.RegisterAttached(
"IsDragElement", typeof(bool), typeof(WindowAttach), new PropertyMetadata(ValueBoxes.FalseBox, OnIsDragElementChanged));
public static void SetIsDragElement(DependencyObject element, bool value)
=> element.SetValue(IsDragElementProperty, ValueBoxes.BooleanBox(value));
public static bool GetIsDragElement(DependencyObject element)
=> (bool) element.GetValue(IsDragElementProperty);
private static void OnIsDragElementChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
if (d is UIElement ctl)
{
if (d is UIElement ctl)
if ((bool) e.NewValue)
{
if ((bool) e.NewValue)
{
ctl.MouseLeftButtonDown += DragElement_MouseLeftButtonDown;
}
else
{
ctl.MouseLeftButtonDown -= DragElement_MouseLeftButtonDown;
}
ctl.MouseLeftButtonDown += DragElement_MouseLeftButtonDown;
}
else
{
ctl.MouseLeftButtonDown -= DragElement_MouseLeftButtonDown;
}
}
private static void DragElement_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
if (sender is DependencyObject obj && e.ButtonState == MouseButtonState.Pressed)
{
System.Windows.Window.GetWindow(obj)?.DragMove();
}
}
public static readonly DependencyProperty IgnoreAltF4Property = DependencyProperty.RegisterAttached(
"IgnoreAltF4", typeof(bool), typeof(WindowAttach), new PropertyMetadata(ValueBoxes.FalseBox, OnIgnoreAltF4Changed));
private static void OnIgnoreAltF4Changed(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
if (d is System.Windows.Window window)
{
if ((bool) e.NewValue)
{
window.PreviewKeyDown += Window_PreviewKeyDown;
}
else
{
window.PreviewKeyDown -= Window_PreviewKeyDown;
}
}
}
private static void Window_PreviewKeyDown(object sender, KeyEventArgs e)
{
if (e.Key == Key.System && e.SystemKey == Key.F4)
{
e.Handled = true;
}
}
public static void SetIgnoreAltF4(DependencyObject element, bool value)
=> element.SetValue(IgnoreAltF4Property, ValueBoxes.BooleanBox(value));
public static bool GetIgnoreAltF4(DependencyObject element)
=> (bool) element.GetValue(IgnoreAltF4Property);
public static readonly DependencyProperty ShowInTaskManagerProperty = DependencyProperty.RegisterAttached(
"ShowInTaskManager", typeof(bool), typeof(WindowAttach), new PropertyMetadata(ValueBoxes.TrueBox, OnShowInTaskManagerChanged));
private static void OnShowInTaskManagerChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
if (d is System.Windows.Window window)
{
var v = (bool) e.NewValue;
window.SetCurrentValue(System.Windows.Window.ShowInTaskbarProperty, v);
if (v)
{
window.SourceInitialized -= Window_SourceInitialized;
}
else
{
window.SourceInitialized += Window_SourceInitialized;
}
}
}
private static void Window_SourceInitialized(object sender, EventArgs e)
{
if (sender is System.Windows.Window window)
{
var _ = new WindowInteropHelper(window)
{
Owner = InteropMethods.GetDesktopWindow()
};
}
}
public static void SetShowInTaskManager(DependencyObject element, bool value)
=> element.SetValue(ShowInTaskManagerProperty, ValueBoxes.BooleanBox(value));
public static bool GetShowInTaskManager(DependencyObject element)
=> (bool) element.GetValue(ShowInTaskManagerProperty);
public static readonly DependencyProperty HideWhenClosingProperty = DependencyProperty.RegisterAttached(
"HideWhenClosing", typeof(bool), typeof(WindowAttach), new PropertyMetadata(ValueBoxes.FalseBox, OnHideWhenClosingChanged));
private static void OnHideWhenClosingChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
if (d is System.Windows.Window window)
{
var v = (bool) e.NewValue;
if (v)
{
window.Closing += Window_Closing;
}
else
{
window.Closing -= Window_Closing;
}
}
}
private static void Window_Closing(object sender, CancelEventArgs e)
{
if (sender is System.Windows.Window window)
{
window.Hide();
e.Cancel = true;
}
}
public static void SetHideWhenClosing(DependencyObject element, bool value)
=> element.SetValue(HideWhenClosingProperty, value);
public static bool GetHideWhenClosing(DependencyObject element)
=> (bool) element.GetValue(HideWhenClosingProperty);
}
private static void DragElement_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
if (sender is DependencyObject obj && e.ButtonState == MouseButtonState.Pressed)
{
System.Windows.Window.GetWindow(obj)?.DragMove();
}
}
public static readonly DependencyProperty IgnoreAltF4Property = DependencyProperty.RegisterAttached(
"IgnoreAltF4", typeof(bool), typeof(WindowAttach), new PropertyMetadata(ValueBoxes.FalseBox, OnIgnoreAltF4Changed));
private static void OnIgnoreAltF4Changed(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
if (d is System.Windows.Window window)
{
if ((bool) e.NewValue)
{
window.PreviewKeyDown += Window_PreviewKeyDown;
}
else
{
window.PreviewKeyDown -= Window_PreviewKeyDown;
}
}
}
private static void Window_PreviewKeyDown(object sender, KeyEventArgs e)
{
if (e.Key == Key.System && e.SystemKey == Key.F4)
{
e.Handled = true;
}
}
public static void SetIgnoreAltF4(DependencyObject element, bool value)
=> element.SetValue(IgnoreAltF4Property, ValueBoxes.BooleanBox(value));
public static bool GetIgnoreAltF4(DependencyObject element)
=> (bool) element.GetValue(IgnoreAltF4Property);
public static readonly DependencyProperty ShowInTaskManagerProperty = DependencyProperty.RegisterAttached(
"ShowInTaskManager", typeof(bool), typeof(WindowAttach), new PropertyMetadata(ValueBoxes.TrueBox, OnShowInTaskManagerChanged));
private static void OnShowInTaskManagerChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
if (d is System.Windows.Window window)
{
var v = (bool) e.NewValue;
window.SetCurrentValue(System.Windows.Window.ShowInTaskbarProperty, v);
if (v)
{
window.SourceInitialized -= Window_SourceInitialized;
}
else
{
window.SourceInitialized += Window_SourceInitialized;
}
}
}
private static void Window_SourceInitialized(object sender, EventArgs e)
{
if (sender is System.Windows.Window window)
{
var _ = new WindowInteropHelper(window)
{
Owner = InteropMethods.GetDesktopWindow()
};
}
}
public static void SetShowInTaskManager(DependencyObject element, bool value)
=> element.SetValue(ShowInTaskManagerProperty, ValueBoxes.BooleanBox(value));
public static bool GetShowInTaskManager(DependencyObject element)
=> (bool) element.GetValue(ShowInTaskManagerProperty);
public static readonly DependencyProperty HideWhenClosingProperty = DependencyProperty.RegisterAttached(
"HideWhenClosing", typeof(bool), typeof(WindowAttach), new PropertyMetadata(ValueBoxes.FalseBox, OnHideWhenClosingChanged));
private static void OnHideWhenClosingChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
if (d is System.Windows.Window window)
{
var v = (bool) e.NewValue;
if (v)
{
window.Closing += Window_Closing;
}
else
{
window.Closing -= Window_Closing;
}
}
}
private static void Window_Closing(object sender, CancelEventArgs e)
{
if (sender is System.Windows.Window window)
{
window.Hide();
e.Cancel = true;
}
}
public static void SetHideWhenClosing(DependencyObject element, bool value)
=> element.SetValue(HideWhenClosingProperty, ValueBoxes.BooleanBox(value));
public static bool GetHideWhenClosing(DependencyObject element)
=> (bool) element.GetValue(HideWhenClosingProperty);
public static readonly DependencyProperty ExtendContentToNonClientAreaProperty = DependencyProperty.RegisterAttached(
"ExtendContentToNonClientArea", typeof(bool), typeof(WindowAttach), new PropertyMetadata(ValueBoxes.FalseBox));
public static void SetExtendContentToNonClientArea(DependencyObject element, bool value)
=> element.SetValue(ExtendContentToNonClientAreaProperty, ValueBoxes.BooleanBox(value));
public static bool GetExtendContentToNonClientArea(DependencyObject element)
=> (bool) element.GetValue(ExtendContentToNonClientAreaProperty);
}