using System; using System.Collections.Generic; using System.ComponentModel; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows; using System.Windows.Data; using System.Windows.Markup; namespace BeDesignerSCADA.Common { /// /// 编辑脚本时获取控件属性 /// public class PropertyHelper { public static string[] ableProperties = new string[] { "IsChecked", "Value", "CurValue", "StatusValue", "NumberValue", "Text", "Direction","RefreshData", "ChangedText","Content","SendText","LeftTogIsChecked","RightTogIsChecked", "Visibility","Message","ReturnValue" }; public static List GetCustomerControlProperty(List selectItems) { List result = new List(); foreach (var control in selectItems) { var typeCtl = control.GetType(); if (typeCtl.GetProperties().Count(p => ableProperties.Contains(p.Name)) == 0) continue; var pare = new ControlName(control.Name, null); result.Add(pare); pare.Properties = typeCtl.GetProperties() .Where(p => ableProperties.Contains(p.Name)) .Select(x => new ControlName(x.Name, pare)).ToList(); } return result; } #region 查找属性 /// /// 获取绑定 /// /// /// /// public static string GetBindString(DependencyProperty selectproperty,FrameworkElement control) { try { Binding binding = BindingOperations.GetBinding(control, selectproperty); return binding==null?"":BindingOperations.GetBinding(control, selectproperty).Path.Path; } catch (Exception ex) { return ""; } } /// /// 获取一个对象中所有的依赖项属性。 /// public static IEnumerable GetDependencyProperties(object instance) => TypeDescriptor.GetProperties(instance, new Attribute[] { new PropertyFilterAttribute(PropertyFilterOptions.All) }) .OfType() .Select(x => DependencyPropertyDescriptor.FromProperty(x)?.DependencyProperty) .Where(x => x != null); /// /// 获取一个类型中所有的依赖项属性。 /// public static IEnumerable GetDependencyProperties(Type type) => TypeDescriptor.GetProperties(type, new Attribute[] { new PropertyFilterAttribute(PropertyFilterOptions.All) }) .OfType() .Select(x => DependencyPropertyDescriptor.FromProperty(x)?.DependencyProperty) .Where(x => x != null); #endregion } public class ControlName { public ControlName(string name, ControlName parent) { Name = name; Parent = parent; } public ControlName Parent { get; set; } public string Name { get; set; } public IEnumerable Properties { get; set; } } internal class BindingConvertor : ExpressionConverter { public override bool CanConvertTo(ITypeDescriptorContext context, Type destinationType) { if (destinationType == typeof(MarkupExtension)) return true; else return false; } public override object ConvertTo(ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, Type destinationType) { if (destinationType == typeof(MarkupExtension)) { BindingExpression bindingExpression = value as BindingExpression; if (bindingExpression == null) throw new Exception(); return bindingExpression.ParentBinding; } return base.ConvertTo(context, culture, value, destinationType); } } internal static class EditorHelper { public static void Register() { Attribute[] attr = new Attribute[1]; TypeConverterAttribute vConv = new TypeConverterAttribute(typeof(TC)); attr[0] = vConv; TypeDescriptor.AddAttributes(typeof(T), attr); } } }