|
- 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
- {
- /// <summary>
- /// 编辑脚本时获取控件属性
- /// </summary>
- 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<ControlName> GetCustomerControlProperty(List<FrameworkElement> selectItems)
- {
- List<ControlName> result = new List<ControlName>();
- 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 查找属性
- /// <summary>
- /// 获取绑定
- /// </summary>
- /// <param name="selectproperty"></param>
- /// <param name="control"></param>
- /// <returns></returns>
- 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 "";
- }
- }
-
- /// <summary>
- /// 获取一个对象中所有的依赖项属性。
- /// </summary>
- public static IEnumerable<DependencyProperty> GetDependencyProperties(object instance)
- => TypeDescriptor.GetProperties(instance, new Attribute[] { new PropertyFilterAttribute(PropertyFilterOptions.All) })
- .OfType<PropertyDescriptor>()
- .Select(x => DependencyPropertyDescriptor.FromProperty(x)?.DependencyProperty)
- .Where(x => x != null);
-
- /// <summary>
- /// 获取一个类型中所有的依赖项属性。
- /// </summary>
- public static IEnumerable<DependencyProperty> GetDependencyProperties(Type type)
- => TypeDescriptor.GetProperties(type, new Attribute[] { new PropertyFilterAttribute(PropertyFilterOptions.All) })
- .OfType<PropertyDescriptor>()
- .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<ControlName> 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<T, TC>()
- {
- Attribute[] attr = new Attribute[1];
- TypeConverterAttribute vConv = new TypeConverterAttribute(typeof(TC));
- attr[0] = vConv;
- TypeDescriptor.AddAttributes(typeof(T), attr);
- }
- }
- }
|