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 { /// /// 用于同步ComboBox的背景与边框画刷,同时设置下拉按钮的宽度与高度相同;也可以用选择的设置下拉按钮的内容UIElement。 /// /// 要进行设置的ComboBox对象 /// 鼠标悬停时的下拉按钮背景画刷 /// 下拉按钮的内容UI元素 /// 待选列表展开时下拉列表的内容UI元素 /// 下拉按钮宽度 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; //TODO:ComboBox重置按钮插入:可以在这个布局面板中插入 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; }; } } } /// /// 用于同步DatePicker的背景与边框画刷,同时设置下拉按钮的宽度与高度相同;也可以用选择的设置下拉按钮的内容UIElement。 /// /// 要进行画笔与布局重设的DatePicker对象 /// 水印信息 /// 鼠标悬停在下拉按钮上时的背景色 /// 用于更换下拉按钮上的图案 /// 下拉按钮的宽度值 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 = "" + "\r\n" + " \r\n" + " \r\n" + " \r\n" + " \r\n" + " \r\n" + "\r\n"; ResourceDictionary dictionary = XamlReader.Parse(templateString) as ResourceDictionary; ControlTemplate template = dictionary["dpButtonTemplate"] as ControlTemplate; partButton.Template = template; partButton.Content = handle; } } /// /// 将一个Path矢量图进行深拷贝后封装于ViewBox中 /// /// 要封装的Path矢量图 /// 填充画笔,不设置则使用源Path的画笔 /// 描边画笔,不设置则使用源Path的画笔 /// 封装好的ViewBox对象 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 }; } } }