-
Notifications
You must be signed in to change notification settings - Fork 55
/
Copy pathViewModel.cs
75 lines (67 loc) · 2.58 KB
/
ViewModel.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace DigitalPlatform.RFID.UI
{
public class ViewModel : CustomTypeDescriptor
{
private LogicChipItem _instance;
private ICustomTypeDescriptor _originalDescriptor;
public ViewModel(LogicChipItem instance,
ICustomTypeDescriptor originalDescriptor)
: base(originalDescriptor)
{
_instance = instance;
_originalDescriptor = originalDescriptor;
// PropertyAttributeReplacements = new Dictionary<Type, IList<Attribute>>();
}
public static ViewModel DressUp(LogicChipItem instance)
{
return new ViewModel(instance, TypeDescriptor.GetProvider(instance).GetTypeDescriptor(instance));
}
public override PropertyDescriptorCollection GetProperties(Attribute[] attributes)
{
var properties = base.GetProperties(attributes).Cast<PropertyDescriptor>();
var bettered = properties.Select(pd =>
{
// 找元素名
if (Element.TryGetOidByName(pd.Name, out ElementOID oid) == false)
return pd;
bool is_readonly = false;
Element element = _instance.FindElement(oid);
if (element != null && element.Locked)
is_readonly = true;
// 从 .Name 找到 readonly 的应有值
{
List<Attribute> attribs = new List<Attribute>();
foreach (Attribute attr in pd.Attributes)
{
if (attr is ReadOnlyAttribute)
attribs.Add(new ReadOnlyAttribute(is_readonly));
else
attribs.Add(attr);
}
return TypeDescriptor.CreateProperty(typeof(LogicChipItem), pd, attribs.ToArray());
}
#if NO
if (PropertyAttributeReplacements.ContainsKey(pd.PropertyType))
{
return TypeDescriptor.CreateProperty(typeof(T), pd, PropertyAttributeReplacements[pd.PropertyType].ToArray());
}
else
{
return pd;
}
#endif
});
return new PropertyDescriptorCollection(bettered.ToArray());
}
public override PropertyDescriptorCollection GetProperties()
{
return GetProperties(null);
}
}
}