终端一体化运控平台
Du kannst nicht mehr als 25 Themen auswählen Themen müssen entweder mit einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.
 
 
 

136 Zeilen
4.7 KiB

  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Linq;
  5. using System.Text;
  6. using System.Threading.Tasks;
  7. using System.Windows;
  8. using System.Windows.Data;
  9. using System.Windows.Markup;
  10. namespace BeDesignerSCADA.Common
  11. {
  12. /// <summary>
  13. /// 编辑脚本时获取控件属性
  14. /// </summary>
  15. public class PropertyHelper
  16. {
  17. public static string[] ableProperties = new string[]
  18. {
  19. "IsChecked", "Value", "CurValue",
  20. "StatusValue", "NumberValue", "Text",
  21. "Direction","RefreshData",
  22. "ChangedText","Content","SendText","LeftTogIsChecked","RightTogIsChecked",
  23. "Visibility","Message","ReturnValue"
  24. };
  25. public static List<ControlName> GetCustomerControlProperty(List<FrameworkElement> selectItems)
  26. {
  27. List<ControlName> result = new List<ControlName>();
  28. foreach (var control in selectItems)
  29. {
  30. var typeCtl = control.GetType();
  31. if (typeCtl.GetProperties().Count(p => ableProperties.Contains(p.Name)) == 0)
  32. continue;
  33. var pare = new ControlName(control.Name, null);
  34. result.Add(pare);
  35. pare.Properties = typeCtl.GetProperties()
  36. .Where(p => ableProperties.Contains(p.Name))
  37. .Select(x => new ControlName(x.Name, pare)).ToList();
  38. }
  39. return result;
  40. }
  41. #region 查找属性
  42. /// <summary>
  43. /// 获取绑定
  44. /// </summary>
  45. /// <param name="selectproperty"></param>
  46. /// <param name="control"></param>
  47. /// <returns></returns>
  48. public static string GetBindString(DependencyProperty selectproperty,FrameworkElement control)
  49. {
  50. try
  51. {
  52. Binding binding = BindingOperations.GetBinding(control, selectproperty);
  53. return binding==null?"":BindingOperations.GetBinding(control, selectproperty).Path.Path;
  54. }
  55. catch (Exception ex)
  56. {
  57. return "";
  58. }
  59. }
  60. /// <summary>
  61. /// 获取一个对象中所有的依赖项属性。
  62. /// </summary>
  63. public static IEnumerable<DependencyProperty> GetDependencyProperties(object instance)
  64. => TypeDescriptor.GetProperties(instance, new Attribute[] { new PropertyFilterAttribute(PropertyFilterOptions.All) })
  65. .OfType<PropertyDescriptor>()
  66. .Select(x => DependencyPropertyDescriptor.FromProperty(x)?.DependencyProperty)
  67. .Where(x => x != null);
  68. /// <summary>
  69. /// 获取一个类型中所有的依赖项属性。
  70. /// </summary>
  71. public static IEnumerable<DependencyProperty> GetDependencyProperties(Type type)
  72. => TypeDescriptor.GetProperties(type, new Attribute[] { new PropertyFilterAttribute(PropertyFilterOptions.All) })
  73. .OfType<PropertyDescriptor>()
  74. .Select(x => DependencyPropertyDescriptor.FromProperty(x)?.DependencyProperty)
  75. .Where(x => x != null);
  76. #endregion
  77. }
  78. public class ControlName
  79. {
  80. public ControlName(string name, ControlName parent)
  81. {
  82. Name = name;
  83. Parent = parent;
  84. }
  85. public ControlName Parent { get; set; }
  86. public string Name { get; set; }
  87. public IEnumerable<ControlName> Properties { get; set; }
  88. }
  89. internal class BindingConvertor : ExpressionConverter
  90. {
  91. public override bool CanConvertTo(ITypeDescriptorContext context, Type destinationType)
  92. {
  93. if (destinationType == typeof(MarkupExtension))
  94. return true;
  95. else return false;
  96. }
  97. public override object ConvertTo(ITypeDescriptorContext context,
  98. System.Globalization.CultureInfo culture, object value, Type destinationType)
  99. {
  100. if (destinationType == typeof(MarkupExtension))
  101. {
  102. BindingExpression bindingExpression = value as BindingExpression;
  103. if (bindingExpression == null)
  104. throw new Exception();
  105. return bindingExpression.ParentBinding;
  106. }
  107. return base.ConvertTo(context, culture, value, destinationType);
  108. }
  109. }
  110. internal static class EditorHelper
  111. {
  112. public static void Register<T, TC>()
  113. {
  114. Attribute[] attr = new Attribute[1];
  115. TypeConverterAttribute vConv = new TypeConverterAttribute(typeof(TC));
  116. attr[0] = vConv;
  117. TypeDescriptor.AddAttributes(typeof(T), attr);
  118. }
  119. }
  120. }