CHEN-ZW\acer c76c8ada14 Merge branch 'main' of http://27.154.35.18:7053/yuxingheng/009_DI-Elec
# Conflicts:
#	newFront/c#前端/SWS.CAD/Commands.cs
#	newFront/c#前端/SWS.Electrical/obj/Debug/Views/DialogTest2.g.i.cs
#	newFront/c#前端/SWS.Electrical/obj/Debug/Views/SWSDialogWindow.g.cs
#	newFront/c#前端/SWS.Electrical/obj/Debug/Views/SWSDialogWindow.g.i.cs
#	newFront/c#前端/SWS.WPF/obj/Debug/Views/CustomDialogWindow.g.cs
#	newFront/c#前端/SWS.WPF/obj/Debug/Views/CustomDialogWindow.g.i.cs
2025-10-09 18:08:19 +08:00

1526 lines
67 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.Collections.ObjectModel;
using System.ComponentModel;
using System.Linq;
using System.Reflection;
using System.Text.RegularExpressions;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Input;
using System.Windows.Media;
using Prism.Events;
using Prism.Ioc;
using Prism.Services.Dialogs;
using SWS.CAD.Event;
using SWS.Commons;
using SWS.Model;
//using Teigha.DatabaseServices;
using Teigha.GraphicsInterface;
using Teigha.GraphicsSystem;
using Telerik.Windows.Controls;
using Unity;
using static System.Net.Mime.MediaTypeNames;
using Label = System.Windows.Controls.Label;
using WControls = System.Windows.Controls;
namespace SWS.CAD.Views.CustomControl
{
public class GridSouce
{
/// <summary>
/// 属性Grid
/// </summary>
public PropertyGrid ProGrid { get; set; }
/// <summary>
/// 属性值
/// </summary>
public SWS.Model.propertyModel Source { get; set; }
/// <summary>
/// 控件名称
/// </summary>
public string ControlName { get; set; }
/// <summary>
/// 是否最后一个属性
/// </summary>
public bool isLast { get; set; }
}
/// <summary>
/// 自定义属性显示控件
/// </summary>
public class PropertyGrid : StackPanel, INotifyPropertyChanged
{
static IEventAggregator eventAggregator;
static PropertyGrid()
{
//设置该控件引用样式的键
// set the key to reference the style for this control
FrameworkElement.DefaultStyleKeyProperty.OverrideMetadata(
typeof(PropertyGrid), new FrameworkPropertyMetadata(typeof(PropertyGrid)));
eventAggregator = GlobalObject.container.Resolve<IEventAggregator>();
}
#region
/// <summary>
/// 记录一个板块对应的Grid
/// </summary>
private Dictionary<string, Grid> _KeyValuePairs = new Dictionary<string, Grid>();
/// <summary>
/// 需要显示属性的类型
/// </summary>
private object _ShowProp;
private static string oldTagNumber = string.Empty;
#endregion
#region
/// <summary>
/// 显示该类的属性编辑
/// </summary>
public object ShowProp
{
get { return (object)GetValue(ShowPropProperty); }
set { SetValue(ShowPropProperty, value); OnPropertyChanged("ShowProp"); }
}
public event PropertyChangedEventHandler PropertyChanged;
protected virtual void OnPropertyChanged(string propertyName)
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
// Using a DependencyProperty as the backing store for ShowProp. This enables animation, styling, binding, etc...
public static readonly DependencyProperty ShowPropProperty =
DependencyProperty.Register("ShowProp", typeof(object), typeof(PropertyGrid),
new PropertyMetadata(default(object), new PropertyChangedCallback((d, e) =>
{
//属性更改事件
OnShowPropChanged(d, e);
})));
/// <summary>
/// 默认属性名称
/// </summary>
public string CurrentPropertyName
{
get { return GetValue(ShowPropertyName).ToString(); }
set
{
SetValue(ShowPropertyName, value);
OnPropertyChanged("CurrentPropertyName");
}
}
public static readonly DependencyProperty ShowPropertyName =
DependencyProperty.Register("CurrentPropertyName", typeof(object), typeof(PropertyGrid),
new PropertyMetadata(default(object), new PropertyChangedCallback((d, e) =>
{
})));
/// <summary>
/// 默认属性名称
/// </summary>
public bool IsBasicGroup
{
get { return bool.Parse(GetValue(BasicGroup).ToString()); }
set
{
SetValue(BasicGroup, value);
OnPropertyChanged("IsBasicGroup");
}
}
public static readonly DependencyProperty BasicGroup =
DependencyProperty.Register("IsBasicGroup", typeof(object), typeof(PropertyGrid),
new PropertyMetadata(default(object), new PropertyChangedCallback((d, e) =>
{
var sender = d as PropertyGrid;
ObservableCollection<Model.propertyModel> listPairs = (ObservableCollection<Model.propertyModel>)sender.ShowProp;
if (listPairs != null && listPairs.Any())
{
sender.Children.Clear();
sender._KeyValuePairs.Clear();
for (int i = 0; i < listPairs.Count; i++)
{
var isLast = i == listPairs.Count - 1;//最后一个属性
if (!isLast)
{
var item1 = listPairs[i];
var item2 = listPairs[i + 1];
isLast = item1.GroupName != item2.GroupName;//当前属性与下个属性的分组不一样,则当前属性是在分组的最后一个属性
}
CreatePropertyControl(sender, listPairs[i], i, isLast);
}
}
})));
#endregion
#region private方法
/// <summary>
/// ShowProp属性更改事件
/// </summary>
/// <param name="d"></param>
/// <param name="e"></param>
private static void OnShowPropChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
var sender = d as PropertyGrid;
sender.Children.Clear();
sender._KeyValuePairs.Clear();
var newValue = e.NewValue;
if (newValue != null)
{
//Type t = newValue.GetType();
//sender._ShowProp = newValue;
//Object[] obj = t.GetProperties();
////取属性上的自定义特性
//foreach (PropertyInfo propInfo in obj)
//{
// CreateControlByAttribute(sender, newValue, propInfo);
//}
//Object[] objFields = t.GetFields();
////取公有字段上的自定义特性
//foreach (FieldInfo propInfo in objFields)
//{
// CreateControlByAttribute(sender, newValue, propInfo);
//}
//Object[] objMethods = t.GetMethods();
////取公有方法上的自定义特性
//foreach (MethodInfo propInfo in objMethods)
//{
// CreateControlByAttribute(sender, newValue, propInfo);
//}
////CJB
//ObservableCollection<propertyModel> listProperty = (ObservableCollection<propertyModel>)newValue;
//foreach (propertyModel model in listProperty)
//{
// CreatePropertyControl(sender, model);
//}
var listPro = (ObservableCollection<Model.propertyModel>)newValue;
//foreach (propertyModel model in listPro)
//{
// var isLast = listPro.Last() == model;
// CreatePropertyControl(sender, model, isLast);
//}
for (int i = 0; i < listPro.Count; i++)
{
var isLast = false;
if (!sender.IsBasicGroup)
{
isLast = i == listPro.Count - 1;//最后一个属性
if (!isLast)
{
var item1 = listPro[i];
var item2 = listPro[i + 1];
isLast = item1.GroupName != item2.GroupName;//当前属性与下个属性的分组不一样,则当前属性是在分组的最后一个属性
}
}
else
{
var item = listPro[i];
var list = listPro.Where(a => a.IsBasicGroup = item.IsBasicGroup).ToList();
if (list != null && list.Count() != 0)
{
isLast = item.DisplayText == list.Last().DisplayText;
}
}
CreatePropertyControl(sender, listPro[i], i, isLast);
}
}
}
/// <summary>
/// 根据属性特征创建控件
/// </summary>
/// <param name="sender"></param>
/// <param name="model"></param>
/// <param name="i">是否最后一个</param>
private static void CreatePropertyControl(PropertyGrid sender, Model.propertyModel model, int index, bool isLast)
{
//获取编辑的属性特征
LsPropertyGridAttribute attr = new LsPropertyGridAttribute(model.ControlTypeName, model.GroupName, model.DisplayText, model.Item);
Create(sender, attr, model, index, isLast);
}
/// <summary>
/// 根据属性特征创建控件
/// </summary>
/// <param name="objAttrs"></param>
/// <param name="sender"></param>
/// <param name="Source"></param>
/// <param name="path"></param>
private static void CreateControlByAttribute(PropertyGrid sender, object Source, MemberInfo memberInfo)
{
//object[] objAttrs = memberInfo.GetCustomAttributes(typeof(LsPropertyGridAttribute), true);
//if (objAttrs.Length > 0)
//{
// //获取编辑的属性特征
// LsPropertyGridAttribute attr = objAttrs[0] as LsPropertyGridAttribute;
// if (attr != null)
// {
// //Console.WriteLine("Type : {0}", attr.TypeName);
// Create(sender, attr, Source, memberInfo);
// }
//}
}
/// <summary>
/// 创建
/// </summary>
/// <param name="sender">PropertyGrid</param>
/// <param name="attr"></param>
/// <param name="Source">绑定的对象</param>
/// <param name="isLast">是否最后一个属性</param>
public static void Create(PropertyGrid sender, LsPropertyGridAttribute attr, object Source, int index, bool isLast, MemberInfo memberInfo = null)
{
double positionLeft = 5;//距离左边
double positionTop = 10;//距离上
Model.propertyModel pro = (Model.propertyModel)Source;
if (sender.IsBasicGroup)
{
attr.Plate = pro.IsBasicGroup ? "常用属性" : "不常用属性";
}
else
{ attr.Plate = pro.GroupName; }
//判断板块是否已存在 _KeyValuePairs[attr.Plate] = grid;
if (sender._KeyValuePairs.ContainsKey(attr.Plate))
{
var grid = sender._KeyValuePairs[attr.Plate];
//存在直接在Grid后面添加控件
if (memberInfo == null)
{ CreateControl(sender, grid, attr, Source, index, isLast); }
else
{
CreateControl(sender, grid, attr, Source, memberInfo);
}
}
else
{
//板块不存在创建
TextBlock tb = new TextBlock();
tb.Text = "- " + attr.Plate;
tb.HorizontalAlignment = HorizontalAlignment.Left;
tb.Margin = new Thickness(positionLeft, positionTop, 0, 5);
tb.FontSize = 12;
//超过400才有粗效果
tb.FontWeight = FontWeight.FromOpenTypeWeight(400);
tb.Visibility = Visibility.Visible;
tb.MouseDown += txtGrid_MouseDown;
sender.Children.Add(tb);
//板块的Grid
Grid grid = new Grid();
//grid.Width = 200;
grid.Margin = new Thickness(positionLeft, 0, 0, 4);
grid.HorizontalAlignment = HorizontalAlignment.Left;
grid.Background = Brushes.Transparent;
//添加左边列
var column = new ColumnDefinition();
column.Width = new GridLength(120);// GridLength.Auto;
column.MaxWidth = 300;
column.MinWidth = 100;
grid.ColumnDefinitions.Add(column);
//添加分隔列
grid.ColumnDefinitions.Add(new ColumnDefinition() { Width = new GridLength(2) });
//添加右边列
var column3 = new ColumnDefinition();
column3.Width = new GridLength(1.0, GridUnitType.Auto);
column3.MinWidth = 130;
column3.MaxWidth = 500;
grid.ColumnDefinitions.Add(column3);
//添加属性组
sender._KeyValuePairs[attr.Plate] = grid;
//sender._KeyValuePairs.Add(attr.Plate, grid);
tb.Tag = grid;
sender.Children.Add(grid);
if (memberInfo == null)
{ CreateControl(sender, grid, attr, Source, index, isLast); }
else
{
CreateControl(sender, grid, attr, Source, memberInfo);
}
}
}
private static void txtGrid_MouseDown(object sender, MouseButtonEventArgs e)
{
TextBlock tb = sender as TextBlock;
Grid grid = tb.Tag as Grid;
if (grid.Visibility == Visibility.Visible)
{
tb.Text = tb.Text.Replace("-", "+");
grid.Visibility = Visibility.Collapsed;
}
else if (grid.Visibility == Visibility.Collapsed)
{
tb.Text = tb.Text.Replace("+", "-");
grid.Visibility = Visibility.Visible;
}
}
static double height = 25;
static double width = 130;
static double fontSize = 12;
static int rightColumm = 2;
/// <summary>
/// 创建并绑定控件
/// </summary>
/// <param name="pROPERTYType"></param>
/// <param name="path"></param>
/// <returns></returns>
private static void CreateControl(PropertyGrid sender, Grid grid, LsPropertyGridAttribute attr, object obj, int index, bool isLast)
{
//Control control = new Control();
SWS.Model.propertyModel Source = (SWS.Model.propertyModel)obj;
string controlName = attr.Plate + index.ToString();// GlobalObject.ControlNameFilterSqcStr(Source.DisplayText);
if (attr.TypeName != SWS.Model.PROPERTYType.Size)
{
var row = new RowDefinition();
row.MinHeight = 10;
grid.RowDefinitions.Add(row); //添加行
//左边显示名称
Label lb = new Label();
lb.Name = "lbl" + controlName;
if (!string.IsNullOrEmpty(Source.Unit))
{
lb.Content = Source.DisplayText + $"({Source.Unit})";
lb.ToolTip = Source.DisplayText + $"({Source.Unit})";
}
else
{
lb.Content = Source.DisplayText;
lb.ToolTip = Source.DisplayText;
}
lb.Tag = controlName;
lb.HorizontalAlignment = HorizontalAlignment.Left;
lb.VerticalAlignment = VerticalAlignment.Center;
lb.Margin = new Thickness(0, 0, 0, 0);
lb.Height = 25;
lb.FontSize = 12; lb.HorizontalAlignment = HorizontalAlignment.Stretch;
lb.Foreground = new SolidColorBrush(Colors.White);
lb.BorderBrush = new SolidColorBrush(Colors.Black);
if (isLast)
{ lb.BorderThickness = new Thickness(1, 1, 0, 1); }
else { lb.BorderThickness = new Thickness(1, 1, 0, 0); }
//通过代码修改控件的Grid.Row属性
Grid.SetRow(lb, grid.RowDefinitions.Count - 1);
Grid.SetColumn(lb, 0);
grid.Children.Add(lb);
//添加分隔列
var sp = new GridSplitter();
sp.Width = 2;
sp.HorizontalAlignment = HorizontalAlignment.Stretch;
sp.VerticalAlignment = VerticalAlignment.Stretch;
sp.Background = Brushes.Gray;
sp.ResizeBehavior = GridResizeBehavior.PreviousAndCurrent;
sp.ShowsPreview = true;
sp.ResizeDirection = GridResizeDirection.Columns;
grid.Children.Add(sp);
Grid.SetRow(sp, grid.RowDefinitions.Count - 1);
Grid.SetColumn(sp, 1);
}
//根据执行属性的名称绑定到控件
Binding binding = new Binding("PropertyValue");
binding.Source = Source;
binding.Mode = BindingMode.TwoWay;
switch (attr.TypeName)
{
case SWS.Model.PROPERTYType.Folder:
#region Folder
double tbFolderWidth = 100;
var btnFolder = new WControls.Button();
btnFolder.Name = "btn" + controlName;
btnFolder.Content = "...";
btnFolder.Width = 29;
btnFolder.Height = 25;
btnFolder.Margin = new Thickness(1);
btnFolder.BorderBrush = Brushes.Black;
btnFolder.Background = Brushes.Transparent;
btnFolder.HorizontalAlignment = HorizontalAlignment.Left;
btnFolder.Margin = new Thickness(tbFolderWidth + 1, 0, 0, 0);
btnFolder.Tag = new GridSouce() { ProGrid = sender, Source = Source, ControlName = controlName, isLast = isLast };
btnFolder.Click += new RoutedEventHandler(btnFolder_Click);
//通过代码修改控件的Grid.Row属性
Grid.SetRow(btnFolder, grid.RowDefinitions.Count - 1);
Grid.SetColumn(btnFolder, rightColumm);
btnFolder.Style = null;
btnFolder.IsEnabled = Source.IsEnable;
var tbFolder = new NumericBox();
tbFolder.Name = "txt" + controlName;
tbFolder.IsReadOnly = !Source.IsEnable;
tbFolder.Margin = new Thickness(0);
tbFolder.Width = tbFolderWidth;
tbFolder.Padding = new Thickness(2, 0, 0, 0);
tbFolder.Foreground = new SolidColorBrush(Colors.White);
tbFolder.BorderBrush = new SolidColorBrush(Colors.Black);
if (isLast)
{ tbFolder.BorderThickness = new Thickness(0, 1, 0, 1); }
else { tbFolder.BorderThickness = new Thickness(0, 1, 0, 0); }
tbFolder.Background = Brushes.Transparent;
tbFolder.HorizontalAlignment = HorizontalAlignment.Left;
tbFolder.VerticalContentAlignment = VerticalAlignment.Center;
tbFolder.HorizontalContentAlignment = HorizontalAlignment.Left;
tbFolder.Maximum = 10000;
tbFolder.Minimum = -10000;
tbFolder.UpDownButtonsWidth = 0;
tbFolder.LostFocus += Number_LostFocus;
tbFolder.GotFocus += Control_GotFocus;// Number_GotFocus;
tbFolder.Tag = new GridSouce() { ProGrid = sender, Source = Source, ControlName = controlName, isLast = isLast };
Grid.SetRow(tbFolder, grid.RowDefinitions.Count - 1);
Grid.SetColumn(tbFolder, rightColumm);
binding.UpdateSourceTrigger = UpdateSourceTrigger.Explicit;
//tbFolder.SetBinding(System.Windows.Controls.TextBox.TextProperty, binding);
tbFolder.SetBinding(NumericBox.ValueProperty, binding);
grid.Children.Add(btnFolder);
grid.Children.Add(tbFolder);
#endregion
return;
case SWS.Model.PROPERTYType.BoldItalic:
return;
case SWS.Model.PROPERTYType.Button:
break;
case SWS.Model.PROPERTYType.TextBox:
var textBox = new WControls.TextBox();
textBox.Name = "txt" + controlName;
textBox.Margin = new Thickness(0);
textBox.Height = height;
textBox.Width = width;
textBox.FontSize = fontSize;
textBox.Padding = new Thickness(2, 0, 0, 0);
textBox.Foreground = new SolidColorBrush(Colors.White);
textBox.BorderBrush = new SolidColorBrush(Colors.Black);
if (isLast)
{ textBox.BorderThickness = new Thickness(0, 1, 1, 1); }
else { textBox.BorderThickness = new Thickness(0, 1, 1, 0); }
textBox.Background = Brushes.Transparent;
textBox.HorizontalAlignment = HorizontalAlignment.Left;
textBox.VerticalContentAlignment = VerticalAlignment.Center;
textBox.HorizontalContentAlignment = HorizontalAlignment.Left;
textBox.ToolTip = Source.PropertyValue;
textBox.IsReadOnly = !Source.IsEnable;
textBox.VerticalAlignment = VerticalAlignment.Center;
textBox.Tag = new GridSouce() { ProGrid = sender, Source = Source, ControlName = controlName, isLast = isLast };
//失去焦点绑定
textBox.LostFocus += Text_LostFocus;
textBox.GotFocus += Control_GotFocus;// Text_GotFocus;
binding.UpdateSourceTrigger = UpdateSourceTrigger.LostFocus;
textBox.SetBinding(WControls.TextBox.TextProperty, binding);
//通过代码修改控件的Grid.Row属性
Grid.SetRow(textBox, grid.RowDefinitions.Count - 1);
Grid.SetColumn(textBox, rightColumm);
grid.Children.Add(textBox);
break;
case SWS.Model.PROPERTYType.Number:
var txtNumber = new NumericBox();
txtNumber.Name = "txt" + controlName;
txtNumber.Margin = new Thickness(0);
txtNumber.Height = height;
txtNumber.Width = width;
txtNumber.FontSize = fontSize;
txtNumber.Padding = new Thickness(2, 0, 0, 0);
txtNumber.Foreground = new SolidColorBrush(Colors.White);
txtNumber.BorderBrush = new SolidColorBrush(Colors.Black);
//txtNumber.BorderThickness = new Thickness(1, 1, 1, 1);
if (isLast)
{ txtNumber.BorderThickness = new Thickness(0, 1, 1, 1); }
else { txtNumber.BorderThickness = new Thickness(0, 1, 1, 0); }
txtNumber.Background = Brushes.Transparent;
txtNumber.HorizontalAlignment = HorizontalAlignment.Left;
txtNumber.VerticalAlignment = VerticalAlignment.Center;
txtNumber.VerticalContentAlignment = VerticalAlignment.Center;
txtNumber.HorizontalContentAlignment = HorizontalAlignment.Left;
txtNumber.IsReadOnly = !Source.IsEnable;
binding.UpdateSourceTrigger = UpdateSourceTrigger.LostFocus;
txtNumber.Tag = new GridSouce() { ProGrid = sender, Source = Source, ControlName = controlName, isLast = isLast };
txtNumber.SetBinding(NumericBox.ValueProperty, binding);
txtNumber.Maximum = 10000;
txtNumber.Minimum = -10000;
txtNumber.UpDownButtonsWidth = 0;
txtNumber.LostFocus += Number_LostFocus;
txtNumber.GotFocus += Control_GotFocus;// Number_GotFocus;
txtNumber.Visibility = Visibility.Visible;
//通过代码修改控件的Grid.Row属性
Grid.SetRow(txtNumber, grid.RowDefinitions.Count - 1);
Grid.SetColumn(txtNumber, rightColumm);
grid.Children.Add(txtNumber);
break;
case SWS.Model.PROPERTYType.CheckBox:
var checkBox = new CheckBox();
checkBox.Style = null;
checkBox.Name = "checkBox" + controlName;
checkBox.Margin = new Thickness(0);
checkBox.Height = height;
checkBox.Width = width;
checkBox.IsEnabled = Source.IsEnable;
checkBox.VerticalAlignment = VerticalAlignment.Center;
checkBox.VerticalContentAlignment = VerticalAlignment.Center;
checkBox.HorizontalAlignment = HorizontalAlignment.Center;
checkBox.BorderBrush = new SolidColorBrush(Colors.Black);
if (isLast)
{ checkBox.BorderThickness = new Thickness(0, 1, 1, 1); }
else { checkBox.BorderThickness = new Thickness(0, 1, 1, 0); }
checkBox.Tag = new GridSouce() { ProGrid = sender, Source = Source, ControlName = controlName, isLast = isLast };
checkBox.SetBinding(CheckBox.IsCheckedProperty, binding);
//通过代码修改控件的Grid.Row属性
var bor = new Border();
bor.BorderBrush = new SolidColorBrush(Colors.Black);
if (isLast)
{ bor.BorderThickness = new Thickness(0, 1, 1, 1); }
else { bor.BorderThickness = new Thickness(0, 1, 1, 0); }
Grid.SetRow(bor, grid.RowDefinitions.Count - 1);
Grid.SetColumn(bor, rightColumm);
grid.Children.Add(bor);
Grid.SetRow(checkBox, grid.RowDefinitions.Count - 1);
Grid.SetColumn(checkBox, rightColumm);
grid.Children.Add(checkBox);
break;
case SWS.Model.PROPERTYType.Label:
break;
case SWS.Model.PROPERTYType.ComboBox:
var cbox = new RadComboBox();
cbox.Margin = new Thickness(0);
cbox.Height = height - 1;
cbox.Width = width;
cbox.FontSize = fontSize;
cbox.Padding = new Thickness(0);
cbox.Foreground = new SolidColorBrush(Colors.Blue);
cbox.BorderBrush = new SolidColorBrush(Colors.Black);
cbox.Background = Brushes.Transparent;
cbox.HorizontalAlignment = HorizontalAlignment.Left;
cbox.VerticalContentAlignment = VerticalAlignment.Center;
cbox.HorizontalContentAlignment = HorizontalAlignment.Left;
cbox.VerticalAlignment = VerticalAlignment.Center;
//cbox.Style = GlobalObject.TransparentComboBoxStyle;
cbox.BorderThickness = new Thickness(0, 1, 1, 1);
//这个必须放在前面设置
if (attr.Tag != null && !attr.Tag.Equals(""))
{
cbox.DisplayMemberPath = "Value";
cbox.Items.Add(new KeyValueModel() { Key = "", Value = "" });
Dictionary<string, string> items = (Dictionary<string, string>)Source.Item;
foreach (var item in items)
{
if (string.IsNullOrEmpty(item.Key) || string.IsNullOrEmpty(item.Value))
{ continue; }
KeyValueModel model = new KeyValueModel();
model.Key = item.Key;
model.Value = item.Value;
cbox.Items.Add(model);
}
}
cbox.TextBoxStyle = GlobalObje.WhiteStyle;
cbox.Foreground = new SolidColorBrush(Colors.Blue);
cbox.Background = new SolidColorBrush(Color.FromRgb(45, 49, 53));
cbox.IsEditable = true;
cbox.IsEnabled = Source.IsEnable;
cbox.Tag = new GridSouce() { ProGrid = sender, Source = Source, ControlName = controlName, isLast = isLast };
cbox.SetBinding(RadComboBox.TextProperty, binding);
//cbox.SetBinding(WControls.ComboBox.TextProperty, binding);
cbox.SelectionChanged += cbox_SelectionChanged;
cbox.LostFocus += cbox_LostFocus;
cbox.GotFocus += Control_GotFocus;
//通过代码修改控件的Grid.Row属性
Grid.SetRow(cbox, grid.RowDefinitions.Count - 1);
Grid.SetColumn(cbox, rightColumm);
grid.Children.Add(cbox);
break;
case SWS.Model.PROPERTYType.MultiSelectComboBox:
var mscbox = new MultiSelectComboBox();
mscbox.IsEnabled = Source.IsEnable;
mscbox.Margin = new Thickness(0);
mscbox.Height = height - 1;
mscbox.Width = width;
mscbox.FontSize = fontSize;
mscbox.Foreground = new SolidColorBrush(Colors.Black);
mscbox.BorderBrush = new SolidColorBrush(Colors.Black);
mscbox.BorderThickness = new Thickness(0, 1, 1, 1);
mscbox.Background = Brushes.Black;
mscbox.HorizontalAlignment = HorizontalAlignment.Left;
mscbox.VerticalContentAlignment = VerticalAlignment.Center;
mscbox.HorizontalContentAlignment = HorizontalAlignment.Left;
mscbox.VerticalAlignment = VerticalAlignment.Center;
mscbox.SelectionMode = WControls.SelectionMode.Multiple;
//这个必须放在前面设置
if (!attr.Tag.Equals(""))
{
mscbox.DisplayMemberPath = "Value";
Dictionary<string, string> items = (Dictionary<string, string>)Source.Item;
foreach (var item in items)
{
if (string.IsNullOrEmpty(item.Key) || string.IsNullOrEmpty(item.Value))
{ continue; }
KeyValueModel model = new KeyValueModel();
model.Key = item.Key;
model.Value = item.Value;
mscbox.Items.Add(model);
if (Source.PropertyValue.Contains(model.Value))
{
mscbox.SelectedItems.Add(model);
}
}
}
mscbox.Tag = new GridSouce() { ProGrid = sender, Source = Source, ControlName = controlName, isLast = isLast };
mscbox.SetBinding(MultiSelectComboBox.TextProperty, binding);
//mscbox.SelectionChanged += multcbox_SelectionChanged;
mscbox.GotFocus += Control_GotFocus;
mscbox.LostFocus += Control_LostFocus;
//mscbox.Text = Source.PropertyValue;
//通过代码修改控件的Grid.Row属性
Grid.SetRow(mscbox, grid.RowDefinitions.Count - 1);
Grid.SetColumn(mscbox, rightColumm);
grid.Children.Add(mscbox);
break;
}
//control.VerticalAlignment = VerticalAlignment.Center;
////通过代码修改控件的Grid.Row属性
//Grid.SetRow(control, grid.RowDefinitions.Count - 1);
//Grid.SetColumn(control, 1);
//grid.Children.Add(control);
}
private static void btnFolder_Click(object sender, RoutedEventArgs e)
{
var btnFolde = sender as Button;
var gridsource = btnFolde.Tag as GridSouce;
var grid = gridsource.ProGrid;
var source = gridsource.Source;
IDialogParameters x = new Prism.Services.Dialogs.DialogParameters();
x.Add(GlobalObject.dialogPar.unitTypeId.ToString(), source.UnitTypeId);
x.Add(GlobalObject.dialogPar.textYes.ToString(), source.Unit);
var _dialogService = GlobalObject._prismContainer.Resolve<IDialogService>();
_dialogService.ShowDialog(nameof(DialogUnitSelect), x, (RES) =>
{
if (RES.Result == ButtonResult.Yes)
{
var unit = RES.Parameters.GetValue<Unit>(GlobalObject.dialogPar.unit.ToString());
var controlame = "lbl" + gridsource.ControlName;
Label label = null;
source.Unit = unit.UnitName;
label = GlobalObje.GetChildObject<Label>(grid, controlame);
if (label != null)
{
label.Content = label.ToolTip = label.Tag.ToString() + $"({unit.UnitName})";
if (source.IsReturnChanged)
{
eventAggregator.GetEvent<propertyChangeEvent>().Publish(source);
}
}
NumericBox txt = null;
controlame = "txt" + gridsource.ControlName;
txt = GlobalObje.GetChildObject<NumericBox>(grid, controlame);
if (txt != null)
{
txt.Focus();
}
}
else if (RES.Result == ButtonResult.No)
{ }
});
}
/// <summary>
/// 对象转换为字典
/// </summary>
/// <param name="obj">待转化的对象</param>
/// <returns></returns>
public Dictionary<string, string> ObjectToMap(object obj)
{
Dictionary<string, string> map = new Dictionary<string, string>(); //
Type t = obj.GetType(); // 获取对象对应的类, 对应的类型string
PropertyInfo[] pi = t.GetProperties(BindingFlags.Public | BindingFlags.Instance); // 获取当前type公共属性io
foreach (PropertyInfo p in pi)
{
MethodInfo m = p.GetGetMethod();
if (m != null && m.IsPublic)
{
// 进行判NULL处理
if (m.Invoke(obj, new object[] { }) != null)
{
map.Add(p.Name, m.Invoke(obj, new object[] { }).ToString()); // 向字典添加元素
}
}
}
return map;
}
/// <summary>
/// 创建并绑定控件
/// </summary>
/// <param name="pROPERTYType"></param>
/// <param name="path"></param>
/// <returns></returns>
private static void CreateControl(PropertyGrid sender, Grid grid, LsPropertyGridAttribute attr, object Source, MemberInfo memberInfo)
{
Control control = new Control();
if (attr.TypeName != SWS.Model.PROPERTYType.Size)
{
var row = new RowDefinition();
row.MinHeight = 22;
grid.RowDefinitions.Add(row); //添加行
//左边显示名称
TextBlock tb = new TextBlock();
tb.Text = attr.ShowName;
tb.HorizontalAlignment = HorizontalAlignment.Left;
tb.VerticalAlignment = VerticalAlignment.Center;
tb.Margin = new Thickness(0, 0, 0, 0);
//通过代码修改控件的Grid.Row属性
Grid.SetRow(tb, grid.RowDefinitions.Count - 1);
Grid.SetColumn(tb, 0);
grid.Children.Add(tb);
}
//根据执行属性的名称绑定到控件
Binding binding = new Binding(memberInfo.Name);
binding.Source = Source;
binding.Mode = BindingMode.TwoWay;
//if ((attr.TypeName & PROPERTYType.TextBox) == PROPERTYType.TextBox)
//{
// control = new WControls.TextBox();
// control.Style = null;
// binding.UpdateSourceTrigger = UpdateSourceTrigger.PropertyChanged;
// control.SetBinding(System.Windows.Controls.TextBox.TextProperty, binding);
//}
switch (attr.TypeName)
{
case SWS.Model.PROPERTYType.Folder:
#region Folder
double tbFolderWidth = 210;
var btnFolder = new WControls.Button();
btnFolder.Content = "...";
btnFolder.Width = 40;
btnFolder.HorizontalAlignment = HorizontalAlignment.Left;
btnFolder.Margin = new Thickness(tbFolderWidth, 0, 0, 0);
//通过代码修改控件的Grid.Row属性
Grid.SetRow(btnFolder, grid.RowDefinitions.Count - 1);
Grid.SetColumn(btnFolder, 1);
btnFolder.Style = null;
var tbFolder = new WControls.TextBox();
tbFolder.Width = tbFolderWidth;
tbFolder.HorizontalAlignment = HorizontalAlignment.Left;
Grid.SetRow(tbFolder, grid.RowDefinitions.Count - 1);
Grid.SetColumn(tbFolder, 1);
tbFolder.Style = null;
//方法绑定在Button控件
sender.MethodSeBinding(btnFolder, memberInfo);
//属性两个都绑定 所有绑定必须要绑定两个都有的属性
sender.RelationSeBinding(tbFolder, memberInfo, grid);
//再次绑定就不需要绑定grid第一列设置false
sender.RelationSeBinding(btnFolder, memberInfo, grid, false);
//回车触发绑定
//tbFolder.PreviewKeyDown += Control_PreviewKeyDown;
//binding.UpdateSourceTrigger = UpdateSourceTrigger.PropertyChanged;
binding.UpdateSourceTrigger = UpdateSourceTrigger.Explicit;
tbFolder.SetBinding(System.Windows.Controls.TextBox.TextProperty, binding);
grid.Children.Add(btnFolder);
grid.Children.Add(tbFolder);
#endregion
return;
case SWS.Model.PROPERTYType.BoldItalic:
//grid.Children.RemoveAt(grid.Children.Count - 1);
string[] vsBoldItalic = attr.Tag.ToString().Split(',');
#region
//粗体
string[] vsBold = vsBoldItalic[0].Split(':');
var controlBold = new WControls.Button();
controlBold.Width = 40;
controlBold.Content = vsBold[1];
controlBold.HorizontalAlignment = HorizontalAlignment.Left;
//通过代码修改控件的Grid.Row属性
Grid.SetRow(controlBold, grid.RowDefinitions.Count - 1);
Grid.SetColumn(controlBold, 1);
controlBold.Style = null;
grid.Children.Add(controlBold);
//根据执行属性的名称绑定到控件
Binding bindingBold = new Binding(vsBold[0]);
bindingBold.Source = Source;
bindingBold.Mode = BindingMode.TwoWay;
//绑定到tag根据绑定的数据变化颜色
controlBold.SetBinding(TagProperty, bindingBold);
controlBold.Click += ControlBold_Click;
#endregion
#region
//斜体
string[] vsItalic = vsBoldItalic[1].Split(':');
var controlItalic = new WControls.Button();
controlItalic.Style = null;
controlItalic.Width = 40;
controlItalic.Content = vsItalic[1];
controlItalic.Margin = new Thickness(40, 0, 0, 0);
controlItalic.HorizontalAlignment = HorizontalAlignment.Left;
//通过代码修改控件的Grid.Row属性
Grid.SetRow(controlItalic, grid.RowDefinitions.Count - 1);
Grid.SetColumn(controlItalic, 1);
grid.Children.Add(controlItalic);
//根据执行属性的名称绑定到控件
Binding bindingItalic = new Binding(vsItalic[0]);
bindingItalic.Source = Source;
bindingItalic.Mode = BindingMode.TwoWay;
//绑定到tag根据绑定的数据变化颜色
controlItalic.SetBinding(TagProperty, bindingItalic);
controlItalic.Click += ControlBold_Click;
#endregion
//这样两个按钮都绑定了同一个事件,所有需要判断
sender.MethodSeBinding(controlBold, memberInfo);
sender.RelationSeBinding(controlBold, memberInfo, grid);
sender.MethodSeBinding(controlItalic, memberInfo);
sender.RelationSeBinding(controlItalic, memberInfo, grid);
return;
case SWS.Model.PROPERTYType.Button:
control = new WControls.Button();
var tempbtn = control as Button;
tempbtn.Width = 40;
tempbtn.Content = attr.Content;
tempbtn.HorizontalAlignment = HorizontalAlignment.Left;
sender.MethodSeBinding(control, memberInfo);
sender.RelationSeBinding(control, memberInfo, grid);
control.Style = null;
break;
case SWS.Model.PROPERTYType.TextBox:
control = new WControls.TextBox();
sender.MethodSeBinding(control, memberInfo);
sender.RelationSeBinding(control, memberInfo, grid);
control.Style = null;
//回车触发绑定
//control.PreviewKeyDown += Control_PreviewKeyDown;
//binding.UpdateSourceTrigger = UpdateSourceTrigger.PropertyChanged;
binding.UpdateSourceTrigger = UpdateSourceTrigger.Explicit;
control.SetBinding(System.Windows.Controls.TextBox.TextProperty, binding);
break;
case SWS.Model.PROPERTYType.Size:
#region
string[] vs = attr.ShowName.Split(',');
if (vs.Length == 2)
{
attr.TypeName = SWS.Model.PROPERTYType.TextBox;
attr.ShowName = vs[0];
//宽度
CreateControl(sender, grid, attr, Source, memberInfo);
//高度
attr.ShowName = vs[1];
CreateControl(sender, grid, attr, Source, memberInfo);
}
#endregion
return;
case SWS.Model.PROPERTYType.Color:
control = new Button();
control.MinHeight = 18;
sender.MethodSeBinding(control, memberInfo);
sender.RelationSeBinding(control, memberInfo, grid);
control.Style = null;
var temp = control as Button;
temp.Click += Color_Click;
temp.SetBinding(Button.BackgroundProperty, binding);
break;
case SWS.Model.PROPERTYType.CheckBox:
control = new CheckBox();
sender.MethodSeBinding(control, memberInfo);
sender.RelationSeBinding(control, memberInfo, grid);
control.Style = null;
control.SetBinding(CheckBox.IsCheckedProperty, binding);
break;
case SWS.Model.PROPERTYType.Label:
control = new WControls.Label();
sender.MethodSeBinding(control, memberInfo);
sender.RelationSeBinding(control, memberInfo, grid);
control.Style = null;
control.SetBinding(ContentControl.ContentProperty, binding);
break;
case SWS.Model.PROPERTYType.ComboBox:
control = new WControls.ComboBox();
control.Style = null;
var tempCB = control as WControls.ComboBox;
//这个必须放在前面设置
if (!attr.Tag.Equals(""))
{
string[] attrMV = attr.Tag.ToString().Split(',');
//Key
tempCB.SelectedValuePath = attrMV[0];
//Value
tempCB.DisplayMemberPath = attrMV[1];
}
#region
sender.MethodSeBinding(control, memberInfo);
sender.RelationSeBinding(control, memberInfo, grid);
#endregion
//考虑到该属性可能绑定SelectedValue或者SelectedItem所有这里不直接硬性绑定
//tempCB.SetBinding(WControls.ComboBox.SelectedValueProperty, binding);
break;
}
control.VerticalAlignment = VerticalAlignment.Center;
//通过代码修改控件的Grid.Row属性
Grid.SetRow(control, grid.RowDefinitions.Count - 1);
Grid.SetColumn(control, 1);
grid.Children.Add(control);
}
#region
/// <summary>
/// 纯数字校验
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private static void Number_PreviewTextInput(object sender, System.Windows.Input.TextCompositionEventArgs e)
{
Regex _numericRegex = new Regex(@"^-?\d*\.?\d*$");//数字,小数,负数
// 全量文本验证
if (!_numericRegex.IsMatch(e.Text))
{
e.Handled = true;
}
}
/// <summary>
/// 文本框失焦点数据改变
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private static void Text_LostFocus(object sender, RoutedEventArgs e)
{
var temp = sender as WControls.TextBox;
BindingExpression binding = temp.GetBindingExpression(WControls.TextBox.TextProperty);
binding.UpdateSource();
var model = (SWS.Model.propertyModel)binding.DataItem;
if (model.IsReturnChanged) //通知属性值改变,更新到服务器
{
eventAggregator.GetEvent<propertyChangeEvent>().Publish(model);
}
//失去焦点左边的label背景变透明
var gridsource = temp.Tag as GridSouce;
var grid = gridsource.ProGrid;
var name = gridsource.ControlName;
var isLast = gridsource.isLast;
var controlName = "lbl" + name;
Label label = null;
label = GlobalObje.GetChildObject<Label>(grid, controlName);
if (label != null)
{
label.Foreground = Brushes.White;
label.Background = Brushes.Transparent;
}
controlName = "txt" + name;
TextBox textBox = null;
textBox = GlobalObje.GetChildObject<TextBox>(grid, controlName);
if (textBox != null)
{
if (isLast)
{ textBox.BorderThickness = new Thickness(0, 1, 1, 1); }
else { textBox.BorderThickness = new Thickness(0, 1, 1, 0); }
}
}
/// <summary>
/// 文本框获取焦点时label变色
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private static void Text_GotFocus(object sender, RoutedEventArgs e)
{
var txt = sender as TextBox;
var gridsource = txt.Tag as GridSouce;
var grid = gridsource.ProGrid;
var name = gridsource.ControlName;
grid.CurrentPropertyName = gridsource.Source.DisplayText;
var controlName = "lbl" + name;
Label label = null;
label = GlobalObje.GetChildObject<Label>(grid, controlName);
if (label != null)
{
label.Foreground = Brushes.Black;
label.Background = Brushes.CadetBlue;
}
controlName = "txt" + name;
TextBox textBox = null;
textBox = GlobalObje.GetChildObject<TextBox>(grid, controlName);
if (textBox != null)
{
textBox.BorderThickness = new Thickness(1);
}
}
/// <summary>
/// 数字框失焦点数据改变
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private static void Number_LostFocus(object sender, RoutedEventArgs e)
{
var temp = sender as NumericBox;
BindingExpression binding = temp.GetBindingExpression(NumericBox.ValueProperty);
binding.UpdateSource();
var model = (SWS.Model.propertyModel)binding.DataItem;
if (model.IsReturnChanged) //通知属性值改变,更新到服务器
{
eventAggregator.GetEvent<propertyChangeEvent>().Publish(model);
}
//失去焦点左边的label背景变透明
var gridsource = temp.Tag as GridSouce;
var grid = gridsource.ProGrid;
var name = gridsource.ControlName;
var isLast = gridsource.isLast;
var controlName = "lbl" + name;
Label label = null;
label = GlobalObje.GetChildObject<Label>(grid, controlName);
if (label != null)
{
label.Foreground = Brushes.White;
label.Background = Brushes.Transparent;
}
controlName = "txt" + name;
NumericBox box = null;
box = GlobalObje.GetChildObject<NumericBox>(grid, controlName);
if (box != null)
{
if (isLast)
{ box.BorderThickness = new Thickness(0, 1, 0, 1); }
else { box.BorderThickness = new Thickness(0, 1, 0, 0); }
}
}
/// <summary>
/// 数字框获取焦点时label变色
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private static void Number_GotFocus(object sender, RoutedEventArgs e)
{
var num = sender as NumericBox;
var gridsource = num.Tag as GridSouce;
var grid = gridsource.ProGrid;
var name = gridsource.ControlName;
var controlName = "lbl" + name;
Label label = null;
label = GlobalObje.GetChildObject<Label>(grid, controlName);
if (label != null)
{
label.Foreground = Brushes.Black;
label.Background = Brushes.CadetBlue;
}
controlName = "txt" + name;
NumericBox box = null;
box = GlobalObje.GetChildObject<NumericBox>(grid, controlName);
if (box != null)
{
box.BorderThickness = new Thickness(1);
}
}
/// <summary>
/// 控件获取焦点时label变色
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private static void Control_GotFocus(object sender, RoutedEventArgs e)
{
var gridsource = new GridSouce();
if (sender.GetType() == typeof(TextBox))
{
var txt = sender as TextBox;
gridsource = txt.Tag as GridSouce;
var g = gridsource.ProGrid;
var txtname = "txt" + gridsource.ControlName;
TextBox textBox = null;
textBox = GlobalObje.GetChildObject<TextBox>(g, txtname);
if (textBox != null)
{
textBox.BorderThickness = new Thickness(1);
}
}
else if (sender.GetType() == typeof(NumericBox))
{
var num = sender as NumericBox;
gridsource = num.Tag as GridSouce;
var g = gridsource.ProGrid;
var numname = "txt" + gridsource.ControlName;
NumericBox box = null;
box = GlobalObje.GetChildObject<NumericBox>(g, numname);
if (box != null)
{
box.BorderThickness = new Thickness(1);
}
}
else if (sender.GetType() == typeof(RadComboBox))
{
var cbox = sender as RadComboBox;
gridsource = cbox.Tag as GridSouce;
}
else if (sender.GetType() == typeof(MultiSelectComboBox))
{
var cbox = sender as MultiSelectComboBox;
gridsource = cbox.Tag as GridSouce;
}
var grid = gridsource.ProGrid;
var name = gridsource.ControlName;
grid.CurrentPropertyName = gridsource.Source.DisplayText;
var controlName = "lbl" + name;
Label label = null;
label = GlobalObje.GetChildObject<Label>(grid, controlName);
if (label != null)
{
label.Foreground = Brushes.Black;
label.Background = Brushes.CadetBlue;
}
}
/// <summary>
/// 下拉框选中改变
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private static void cbox_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
var temp = sender as RadComboBox;
BindingExpression binding = temp.GetBindingExpression(RadComboBox.TextProperty);
binding.UpdateSource();
var model = (SWS.Model.propertyModel)binding.DataItem;
if (model.IsReturnChanged) //通知属性值改变,更新到服务器
{
if (model.WhichPage.Equals("新增元件"))
{
if (model.DisplayText.Equals("位号"))
{
model.OldValue = oldTagNumber;
eventAggregator.GetEvent<SelectionChangeEvent>().Publish(model);
oldTagNumber = model.PropertyValue; //记录上次的值
}
}
else if (model.WhichPage.Equals("电缆位号选择"))
{
eventAggregator.GetEvent<NewPositional_SelectChangeEvent>().Publish(model);
}
else
{
eventAggregator.GetEvent<propertyChangeEvent>().Publish(model);
}
if (temp != null && temp.SelectedItem != null)
{
}
}
}
/// <summary>
/// 失焦点数据改变
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private static void cbox_LostFocus(object sender, RoutedEventArgs e)
{
var temp = sender as RadComboBox;
BindingExpression binding = temp.GetBindingExpression(RadComboBox.TextProperty);
binding.UpdateSource();
var model = (SWS.Model.propertyModel)binding.DataItem;
if (model.IsReturnChanged) //通知属性值改变,更新到服务器
{
if (model.WhichPage.Equals("新增元件"))
{
//model.OldValue = oldTagNumber;
eventAggregator.GetEvent<NewComponent_LostFocusEvent>().Publish(model);
//oldTagNumber = model.PropertyValue; //记录上次的值
}
else
{
eventAggregator.GetEvent<propertyChangeEvent>().Publish(model);
}
}
//失去焦点左边的label背景变透明
var gridsource = temp.Tag as GridSouce;
var grid = gridsource.ProGrid;
var name = gridsource.ControlName;
var controlName = "lbl" + name;
Label label = null;
label = GlobalObje.GetChildObject<Label>(grid, controlName);
if (label != null)
{
label.Foreground = Brushes.White;
label.Background = Brushes.Transparent;
}
}
/// <summary>
/// 多选下拉框选中改变
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private static void multcbox_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
var temp = sender as IconButton;
BindingExpression binding = temp.GetBindingExpression(MultiSelectComboBox.TextProperty);
binding.UpdateSource();
var model = (SWS.Model.propertyModel)binding.DataItem;
if (model.IsReturnChanged) //通知属性值改变,更新到服务器
{
eventAggregator.GetEvent<propertyChangeEvent>().Publish(model);
}
}
private static void Control_LostFocus(object sender, RoutedEventArgs e)
{
var temp = sender as MultiSelectComboBox;
BindingExpression binding = temp.GetBindingExpression(MultiSelectComboBox.TextProperty);
binding.UpdateSource();
var model = (SWS.Model.propertyModel)binding.DataItem;
if (model.IsReturnChanged) //通知属性值改变,更新到服务器
{
eventAggregator.GetEvent<propertyChangeEvent>().Publish(model);
}
//失去焦点左边的label背景变透明
var gridsource = temp.Tag as GridSouce;
var grid = gridsource.ProGrid;
var name = gridsource.ControlName;
var controlName = "lbl" + name;
Label label = null;
label = GlobalObje.GetChildObject<Label>(grid, controlName);
if (label != null)
{
label.Foreground = Brushes.White;
label.Background = Brushes.Transparent;
}
}
/// <summary>
/// 回车触发数据改变
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private static void Control_PreviewKeyDown(object sender, System.Windows.Input.KeyEventArgs e)
{
if (e.Key == Key.Enter)
{
var temp = sender as WControls.TextBox;
BindingExpression binding = temp.GetBindingExpression(WControls.TextBox.TextProperty);
binding.UpdateSource();
}
}
private static void ControlBold_Click(object sender, RoutedEventArgs e)
{
var btn = sender as Button;
bool tag = (bool)btn.Tag;
// 粗体 和斜体
if (tag)
{
btn.Tag = false;
btn.Background = Brushes.LightGray;
}
else
{
btn.Tag = true;
btn.Background = Brushes.Gold;
}
}
#endregion
/// <summary>
/// 设置关联事件
/// </summary>
/// <param name="control"></param>
public void MethodSeBinding(Control control, MemberInfo memberInfo)
{
//Type t = _ShowProp.GetType();
//Object[] obj = t.GetProperties();
//取属性上的自定义特性
object[] objAttrs = memberInfo.GetCustomAttributes(typeof(RelationMethodAttribute), true);
if (objAttrs.Length > 0)
{
//获取编辑的属性特征
for (int i = 0; i < objAttrs.Length; i++)
{
RelationMethodAttribute attrTemp = objAttrs[i] as RelationMethodAttribute;
//反射为控件事件,添加指定方法
var click = control.GetType().GetEvents().FirstOrDefault(ei => ei.Name.ToLower() == attrTemp.CrEventName.ToLower());
if (click != null)
{
//根据名称查找方法
var method = _ShowProp.GetType().GetMethod(attrTemp.ClMethodName);
//创造委托
var handler = Delegate.CreateDelegate(click.EventHandlerType, _ShowProp, method);
click.AddEventHandler(control, handler);
}
}
}
}
/// <summary>
/// 设置关联属性
/// </summary>
/// <param name="control"></param>
public void RelationSeBinding(Control control, MemberInfo memberInfo, Grid grid, bool IsVisibility = true)
{
//取属性上的自定义特性
object[] objAttrs = memberInfo.GetCustomAttributes(typeof(RelationAttribute), true);
if (objAttrs.Length > 0)
{
//获取编辑的属性特征
for (int i = 0; i < objAttrs.Length; i++)
{
RelationAttribute attrTemp = objAttrs[i] as RelationAttribute;
RelationSeBinding(control, attrTemp, grid);
}
}
}
/// <summary>
/// Visibility转换器
/// </summary>
private VisibilityBoolConverter _VisibilityBool = new VisibilityBoolConverter();
private VisibilityValueConverter _VisibilityValue = new VisibilityValueConverter();
/// <summary>
/// 设置关联属性
/// </summary>
/// <param name="control"></param>
/// <param name="IsVisibility">如果绑定Visibility属性这个可以true设置需不需要隐藏grid第一列的控件
///true则隐藏
/// </param>
public void RelationSeBinding(Control control, RelationAttribute attr, Grid grid, bool IsVisibility = true)
{
if (attr != null)
{
//获取类的关联属性 和 控件的关联属性
string[] crName = attr.CrPropName.Split(',');
string[] clName = attr.ClPropName.Split(',');
for (int i = 0; i < crName.Length; i++)
{
//根据执行属性的名称绑定到控件
Binding binding = new Binding(clName[i]);
binding.Source = _ShowProp;
binding.Mode = BindingMode.TwoWay;
#region
//如果是使用bool控制显示隐藏VisibilityBool
if (crName[i] == "VisibilityBool")
{
//使用转换器
crName[i] = "Visibility";
binding.Converter = _VisibilityBool;
}
else if (crName[i] == "VisibilityValue")
{
//使用转换器
crName[i] = "Visibility";
binding.Converter = _VisibilityValue;
binding.ConverterParameter = attr.VisibilityValue;
}
//把gird这行的也绑定隐藏显示属性
if (crName[i] == "Visibility" && IsVisibility)
{
grid.RowDefinitions[grid.RowDefinitions.Count - 1].MinHeight = 0;
var cr = grid.Children[grid.Children.Count - 1] as TextBlock;
cr.SetBinding(Control.VisibilityProperty, binding);
}
#endregion
//获取依赖属性
BindingFlags mPropertyFlags = BindingFlags.Instance | BindingFlags.Public | BindingFlags.FlattenHierarchy
| BindingFlags.Static | BindingFlags.NonPublic;//筛选
//获取控件关联属性
var fieldInfo = control.GetType().GetField(crName[i] + "Property", mPropertyFlags);
if (fieldInfo != null)
{
control.SetBinding((DependencyProperty)fieldInfo.GetValue(control), binding);
}
}
}
}
/// <summary>
/// 选择颜色
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private static void Color_Click(object sender, RoutedEventArgs e)
{
var tempBtn = sender as Button;
//var picker = SingleOpenHelper.CreateControl<ColorPicker>();
//var window = new PopupWindow
//{
// PopupElement = picker,
// WindowStartupLocation = WindowStartupLocation.CenterScreen,
// AllowsTransparency = true,
// WindowStyle = WindowStyle.None,
// MinWidth = 0,
// MinHeight = 0,
// Title = "颜色选择器"
//};
//picker.SelectedColorChanged += delegate
//{
// window.Close();
//};
//picker.Canceled += delegate { window.Close(); };
//window.Show();
//var picker = SingleOpenHelper.CreateControl<ColorPicker>();
//var window = new PopupWindow
//{
// PopupElement = picker
//};
//picker.SelectedColorChanged += delegate
//{
// tempBtn.Background = picker.SelectedBrush;
// window.Close();
//};
//picker.Canceled += delegate { window.Close(); };
//window.ShowDialog(tempBtn, false);
}
#endregion
#region public方法
#endregion
}
}