Files
Future/Future.Contract/Extensions/ControlHelper.cs
2025-08-30 17:19:57 +08:00

167 lines
9.6 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Controls.Primitives;
using System.Windows.Controls;
using System.Windows.Media;
using System.Globalization;
using Shapes= System.Windows.Shapes;
using System.Windows;
using System.Windows.Markup;
namespace Future.Contract
{
public static partial class FutureHelper
{
/// <summary>
/// 用于同步ComboBox的背景与边框画刷同时设置下拉按钮的宽度与高度相同也可以用选择的设置下拉按钮的内容UIElement。
/// </summary>
/// <param name="comboBox">要进行设置的ComboBox对象</param>
/// <param name="hoverBrush">鼠标悬停时的下拉按钮背景画刷</param>
/// <param name="handle">下拉按钮的内容UI元素</param>
/// <param name="openedHandle">待选列表展开时下拉列表的内容UI元素</param>
/// <param name="handleWidth">下拉按钮宽度</param>
public static void RefreshBrushAddHandle(this ComboBox comboBox, Brush hoverBrush = null, UIElement handle = null, UIElement openedHandle = null, double handleWidth = 0)
{
if (comboBox == null) return;
Brush background = comboBox.Background;
Brush borderBrush = comboBox.BorderBrush;
Thickness thickness = comboBox.BorderThickness;
ToggleButton toggleButton = comboBox.Template.FindName("toggleButton", comboBox) as ToggleButton;
Border toggleBorder = toggleButton.Template.FindName("templateRoot", toggleButton) as Border;
toggleBorder.Background = background;
toggleBorder.BorderBrush = borderBrush;
toggleBorder.BorderThickness = thickness;
if (comboBox.IsEditable)
{
TextBox textBox = comboBox.Template.FindName("PART_EditableTextBox", comboBox) as TextBox;
Border textboxBorder = textBox.Parent as Border;
textboxBorder.Background = new SolidColorBrush(Colors.Transparent);
textboxBorder.BorderThickness = new Thickness(0);
}
Grid grid = toggleButton.Parent as Grid; //TODOComboBox重置按钮插入可以在这个布局面板中插入
grid.VerticalAlignment = VerticalAlignment.Stretch;
grid.ClearValue(Grid.HeightProperty);
grid.ColumnDefinitions[1].Width = new GridLength((handleWidth == 0) ? comboBox.ActualHeight : handleWidth);
Border splitBorder = toggleBorder.Child as Border;
splitBorder.Width = (handleWidth == 0) ? comboBox.ActualHeight : handleWidth;
splitBorder.BorderThickness = new Thickness(0.5, 0, 0, 0);//在下拉按钮左侧画一条线
splitBorder.BorderBrush = borderBrush;
if (hoverBrush != null)
{
splitBorder.MouseEnter += (s, e) => splitBorder.Background = hoverBrush;
splitBorder.MouseLeave += (s, e) => splitBorder.Background = background;
}
if (handle != null)
{
splitBorder.Child = handle;
if (openedHandle != null)
{
comboBox.DropDownOpened += (s, e) => { splitBorder.Child = openedHandle; };
comboBox.DropDownClosed += (s, e) => { splitBorder.Child = handle; };
}
}
}
/// <summary>
/// 用于同步DatePicker的背景与边框画刷同时设置下拉按钮的宽度与高度相同也可以用选择的设置下拉按钮的内容UIElement。
/// </summary>
/// <param name="datePicker">要进行画笔与布局重设的DatePicker对象</param>
/// <param name="waterMark">水印信息</param>
/// <param name="hoverBrush">鼠标悬停在下拉按钮上时的背景色</param>
/// <param name="handle">用于更换下拉按钮上的图案</param>
/// <param name="handleWidth">下拉按钮的宽度值</param>
public static void RefreshBrushAddHandle(this DatePicker datePicker, string waterMark = "", Brush hoverBrush = null, UIElement handle = null, int handleWidth = 0)
{
DatePickerTextBox dpTextBox = datePicker.Template.FindName("PART_TextBox", datePicker) as DatePickerTextBox;
dpTextBox.Background = datePicker.Background;
dpTextBox.BorderBrush = new SolidColorBrush(Colors.Transparent);
//处理水印区域边框
(dpTextBox.Template.FindName("ContentElement", dpTextBox) as Border).BorderBrush = new SolidColorBrush(Colors.Transparent);
(dpTextBox.Template.FindName("watermark_decorator", dpTextBox) as Border).BorderBrush = new SolidColorBrush(Colors.Transparent);
//处理水印容器控件
if (!string.IsNullOrEmpty(waterMark))
{
ContentControl watermarkControl = dpTextBox.Template.FindName("PART_Watermark", dpTextBox) as ContentControl;
watermarkControl.Foreground = new SolidColorBrush(Colors.Gray);
watermarkControl.Content = waterMark;
}
//PART_Root [Grid]次一级布局容器内部的布局结构由这个Grid控制其父节点[Border]的Padding属性继承自DatePicket的Padding默认2px可以向其内部插入重置按钮
//PART_Button [Button]下拉按钮,决定按钮的高度,间接决定输入框的高度;清除模板后可替换成自定义显示样式
//PART_DisabledVisual [Grid]内部第二个[Rectangle]元素决定了按钮的宽度,间接决定输入框的宽度
//让编辑区与按钮尺寸随控件高度变化
datePicker.VerticalContentAlignment = VerticalAlignment.Center;
Grid partRoot = datePicker.Template.FindName("PART_Root", datePicker) as Grid;//TODO:选择重置按钮可以在这里插入
partRoot.VerticalAlignment = VerticalAlignment.Stretch;
partRoot.HorizontalAlignment = HorizontalAlignment.Stretch;
partRoot.ColumnDefinitions[1].Width = new GridLength((handleWidth == 0) ? datePicker.ActualHeight : handleWidth);
dpTextBox.VerticalContentAlignment = VerticalAlignment.Center;
Button partButton = datePicker.Template.FindName("PART_Button", datePicker) as Button;
partButton.ClearValue(Button.WidthProperty);
partButton.VerticalAlignment = VerticalAlignment.Stretch;
partButton.HorizontalAlignment = HorizontalAlignment.Stretch;
//处理鼠标悬浮于日期面板展开按钮上时的背景变化
if (hoverBrush != null)
{
Border hoverBorder = new Border()
{
VerticalAlignment = VerticalAlignment.Stretch,
HorizontalAlignment = HorizontalAlignment.Stretch,
BorderBrush = new SolidColorBrush(Colors.Gray),
BorderThickness = new Thickness(0.5, 0, 0, 0),
Visibility = Visibility.Visible
};
partRoot.Children.Insert(0, hoverBorder);
Grid.SetColumn(hoverBorder, 1);
partButton.Width = hoverBorder.Width;
partButton.MouseEnter += (s, e) => { hoverBorder.Background = hoverBrush; };
partButton.MouseLeave += (s, e) => { hoverBorder.Background = new SolidColorBrush(Colors.Transparent); };
}
//处理更换日期面板展开按钮的内容
if (handle != null)
{
string templateString = "" +
"<ResourceDictionary xmlns=\"http://schemas.microsoft.com/winfx/2006/xaml/presentation\"\r\n" +
" xmlns:x=\"http://schemas.microsoft.com/winfx/2006/xaml\">\r\n" +
" <ControlTemplate x:Key=\"dpButtonTemplate\" TargetType=\"Button\">\r\n" +
" <Border BorderBrush=\"{TemplateBinding BorderBrush}\" Background=\"{TemplateBinding Background}\" BorderThickness=\"{TemplateBinding BorderThickness}\">\r\n" +
" <ContentPresenter/>\r\n" +
" </Border>\r\n" +
" </ControlTemplate>\r\n" +
"</ResourceDictionary>\r\n";
ResourceDictionary dictionary = XamlReader.Parse(templateString) as ResourceDictionary;
ControlTemplate template = dictionary["dpButtonTemplate"] as ControlTemplate;
partButton.Template = template;
partButton.Content = handle;
}
}
/// <summary>
/// 将一个Path矢量图进行深拷贝后封装于ViewBox中
/// </summary>
/// <param name="pathGeometry">要封装的Path矢量图</param>
/// <param name="fillBrush">填充画笔不设置则使用源Path的画笔</param>
/// <param name="strokeBrush">描边画笔不设置则使用源Path的画笔</param>
/// <returns>封装好的ViewBox对象</returns>
public static Viewbox BoxingToViewBox(this Shapes.Path pathGeometry, Brush fillBrush = null, Brush strokeBrush = null)
{
if (pathGeometry == null) return null;
string pathData = pathGeometry.Data.ToString();
Shapes.Path targetPath = new Shapes.Path() { Data = Geometry.Parse(pathData), Fill = fillBrush ?? pathGeometry.Fill, Stroke = strokeBrush ?? pathGeometry.Stroke, Width = pathGeometry.Width, Height = pathGeometry.Height };
return new Viewbox() { Child = targetPath, Stretch = Stretch.Uniform };
}
}
}