Advertisement
Guest User

Untitled

a guest
Aug 18th, 2019
62
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 13.63 KB | None | 0 0
  1. CustomClass myProperties = new CustomClass();
  2.  
  3. propertyGrid.SelectedObject = myProperties;
  4.  
  5. myProperties.Add(new CustomProperty("Name", "Sven", typeof(string), false, true,"Cat1"));
  6. myProperties.Add(new CustomProperty("Surname", "Bendo", typeof(string), false, true, "Cat1"));
  7. myProperties.Add(new CustomProperty("Card", "Visa", typeof(string), false, true, "Cat2"));
  8. myProperties.Add(new CustomProperty("Bank", "SB", typeof(string), false, true, "Cat2"));
  9.  
  10. propertyGrid.Update();
  11.  
  12. /// <summary>
  13. /// CustomClass (Which is binding to property grid)
  14. /// </summary>
  15. public class CustomClass : CollectionBase, ICustomTypeDescriptor
  16. {
  17. /// <summary>
  18. /// Add CustomProperty to Collectionbase List
  19. /// </summary>
  20. /// <param name="Value"></param>
  21. public void Add(CustomProperty Value)
  22. {
  23. base.List.Add(Value);
  24. }
  25.  
  26. /// <summary>
  27. /// Remove item from List
  28. /// </summary>
  29. /// <param name="Name"></param>
  30. public void Remove(string Name)
  31. {
  32. foreach (CustomProperty prop in base.List)
  33. {
  34. if (prop.Name == Name)
  35. {
  36. base.List.Remove(prop);
  37. return;
  38. }
  39. }
  40. }
  41.  
  42. /// <summary>
  43. /// Indexer
  44. /// </summary>
  45. public CustomProperty this[int index]
  46. {
  47. get
  48. {
  49. return (CustomProperty)base.List[index];
  50. }
  51. set
  52. {
  53. base.List[index] = (CustomProperty)value;
  54. }
  55. }
  56.  
  57.  
  58. #region "TypeDescriptor Implementation"
  59. /// <summary>
  60. /// Get Class Name
  61. /// </summary>
  62. /// <returns>String</returns>
  63. public String GetClassName()
  64. {
  65. return TypeDescriptor.GetClassName(this, true);
  66. }
  67.  
  68. /// <summary>
  69. /// GetAttributes
  70. /// </summary>
  71. /// <returns>AttributeCollection</returns>
  72. public AttributeCollection GetAttributes()
  73. {
  74. return TypeDescriptor.GetAttributes(this, true);
  75. }
  76.  
  77. /// <summary>
  78. /// GetComponentName
  79. /// </summary>
  80. /// <returns>String</returns>
  81. public String GetComponentName()
  82. {
  83. return TypeDescriptor.GetComponentName(this, true);
  84. }
  85.  
  86. /// <summary>
  87. /// GetConverter
  88. /// </summary>
  89. /// <returns>TypeConverter</returns>
  90. public TypeConverter GetConverter()
  91. {
  92. return TypeDescriptor.GetConverter(this, true);
  93. }
  94.  
  95. /// <summary>
  96. /// GetDefaultEvent
  97. /// </summary>
  98. /// <returns>EventDescriptor</returns>
  99. public EventDescriptor GetDefaultEvent()
  100. {
  101. return TypeDescriptor.GetDefaultEvent(this, true);
  102. }
  103.  
  104. /// <summary>
  105. /// GetDefaultProperty
  106. /// </summary>
  107. /// <returns>PropertyDescriptor</returns>
  108. public PropertyDescriptor GetDefaultProperty()
  109. {
  110. return TypeDescriptor.GetDefaultProperty(this, true);
  111. }
  112.  
  113. /// <summary>
  114. /// GetEditor
  115. /// </summary>
  116. /// <param name="editorBaseType">editorBaseType</param>
  117. /// <returns>object</returns>
  118. public object GetEditor(Type editorBaseType)
  119. {
  120. return TypeDescriptor.GetEditor(this, editorBaseType, true);
  121. }
  122.  
  123. public EventDescriptorCollection GetEvents(Attribute[] attributes)
  124. {
  125. return TypeDescriptor.GetEvents(this, attributes, true);
  126. }
  127.  
  128. public EventDescriptorCollection GetEvents()
  129. {
  130. return TypeDescriptor.GetEvents(this, true);
  131. }
  132.  
  133. public PropertyDescriptorCollection GetProperties(Attribute[] attributes)
  134. {
  135. PropertyDescriptor[] newProps = new PropertyDescriptor[this.Count];
  136. for (int i = 0; i < this.Count; i++)
  137. {
  138. CustomProperty prop = (CustomProperty)this[i];
  139. newProps[i] = new CustomPropertyDescriptor(ref prop, attributes);
  140. }
  141.  
  142. return new PropertyDescriptorCollection(newProps);
  143. }
  144.  
  145. public PropertyDescriptorCollection GetProperties()
  146. {
  147. return TypeDescriptor.GetProperties(this, true);
  148. }
  149.  
  150. public object GetPropertyOwner(PropertyDescriptor pd)
  151. {
  152. return this;
  153. }
  154. #endregion
  155.  
  156. }
  157.  
  158. /// <summary>
  159. /// Custom property class
  160. /// </summary>
  161. public class CustomProperty
  162. {
  163. private string sName = string.Empty;
  164. private string sCategory = "";
  165. private bool bReadOnly = false;
  166. private bool bVisible = true;
  167. private object objValue = null;
  168.  
  169. public CustomProperty(string sName, object value, Type type, bool bReadOnly, bool bVisible,string sCategory)
  170. {
  171. this.sName = sName;
  172. this.objValue = value;
  173. this.type = type;
  174. this.bReadOnly = bReadOnly;
  175. this.bVisible = bVisible;
  176. this.sCategory = sCategory;
  177. }
  178.  
  179. private Type type;
  180. public Type Type
  181. {
  182. get { return type; }
  183. }
  184.  
  185. public bool ReadOnly
  186. {
  187. get
  188. {
  189. return bReadOnly;
  190. }
  191. }
  192.  
  193. public string Name
  194. {
  195. get
  196. {
  197. return sName;
  198. }
  199. }
  200.  
  201. public string Category
  202. {
  203. get
  204. {
  205. return sCategory;
  206. }
  207.  
  208. }
  209.  
  210.  
  211. public bool Visible
  212. {
  213. get
  214. {
  215. return bVisible;
  216. }
  217. }
  218.  
  219. public object Value
  220. {
  221. get
  222. {
  223. return objValue;
  224. }
  225. set
  226. {
  227. objValue = value;
  228. }
  229. }
  230.  
  231. }
  232.  
  233.  
  234. /// <summary>
  235. /// Custom PropertyDescriptor
  236. /// </summary>
  237. public class CustomPropertyDescriptor : PropertyDescriptor
  238. {
  239. CustomProperty m_Property;
  240. public CustomPropertyDescriptor(ref CustomProperty myProperty, Attribute[] attrs) : base(myProperty.Name, attrs)
  241. {
  242. m_Property = myProperty;
  243. }
  244.  
  245. #region PropertyDescriptor specific
  246.  
  247. public override bool CanResetValue(object component)
  248. {
  249. return false;
  250. }
  251.  
  252. public override Type ComponentType
  253. {
  254. get { return null; }
  255. }
  256.  
  257. public override object GetValue(object component)
  258. {
  259. return m_Property.Value;
  260. }
  261.  
  262. public override string Description
  263. {
  264. get { return m_Property.Name; }
  265. }
  266.  
  267. public override string Category
  268. {
  269. get { return m_Property.Category; }
  270. }
  271.  
  272. public override string DisplayName
  273. {
  274. get { return m_Property.Name; }
  275. }
  276.  
  277. public override bool IsReadOnly
  278. {
  279. get { return m_Property.ReadOnly; }
  280. }
  281.  
  282. public override void ResetValue(object component)
  283. {
  284. //Have to implement
  285. }
  286.  
  287. public override bool ShouldSerializeValue(object component)
  288. {
  289. return false;
  290. }
  291.  
  292. public override void SetValue(object component, object value)
  293. {
  294. m_Property.Value = value;
  295. }
  296.  
  297. public override Type PropertyType
  298. {
  299. get { return m_Property.Type; }
  300. }
  301.  
  302. #endregion
  303.  
  304.  
  305. }
  306.  
  307. CustomClass myProperties = new CustomClass();
  308. propertyGrid.AutoGenerateProperties = false;
  309. propertyGrid.SelectedObject = myProperties;
  310. propertyGrid.PropertyDefinitions.Add(new Xceed.Wpf.Toolkit.PropertyGrid.PropertyDefinition() { Category = "Cat1", DisplayName = "Name", TargetProperties = new string[] { "Name"} });
  311.  
  312. public class EntityProperties : ICustomTypeDescriptor
  313. {
  314. public List<MyProperty> myProperties = new List<MyProperty>();
  315.  
  316. PropertyDescriptorCollection ICustomTypeDescriptor.GetProperties()
  317. {
  318. // Create a collection object to hold property descriptors
  319. PropertyDescriptorCollection pds = new PropertyDescriptorCollection(null);
  320.  
  321. for (int i = 0; i < myProperties.Count; i++)
  322. {
  323. pds.Add(new MyPropertyDescriptor(myProperties[i], this));
  324. }
  325.  
  326. return pds;
  327. }
  328.  
  329.  
  330. #region Use default TypeDescriptor stuff
  331.  
  332. AttributeCollection ICustomTypeDescriptor.GetAttributes()
  333. {
  334. return TypeDescriptor.GetAttributes(this, noCustomTypeDesc: true);
  335. }
  336.  
  337. string ICustomTypeDescriptor.GetClassName()
  338. {
  339. return TypeDescriptor.GetClassName(this, noCustomTypeDesc: true);
  340. }
  341.  
  342. string ICustomTypeDescriptor.GetComponentName()
  343. {
  344. return TypeDescriptor.GetComponentName(this, noCustomTypeDesc: true);
  345. }
  346.  
  347. TypeConverter ICustomTypeDescriptor.GetConverter()
  348. {
  349. return TypeDescriptor.GetConverter(this, noCustomTypeDesc: true);
  350. }
  351.  
  352. EventDescriptor ICustomTypeDescriptor.GetDefaultEvent()
  353. {
  354. return TypeDescriptor.GetDefaultEvent(this, noCustomTypeDesc: true);
  355. }
  356.  
  357. PropertyDescriptor ICustomTypeDescriptor.GetDefaultProperty()
  358. {
  359. return TypeDescriptor.GetDefaultProperty(this, noCustomTypeDesc: true);
  360. }
  361.  
  362. object ICustomTypeDescriptor.GetEditor(Type editorBaseType)
  363. {
  364. return TypeDescriptor.GetEditor(this, editorBaseType, noCustomTypeDesc: true);
  365. }
  366.  
  367. EventDescriptorCollection ICustomTypeDescriptor.GetEvents()
  368. {
  369. return TypeDescriptor.GetEvents(this, noCustomTypeDesc: true);
  370. }
  371.  
  372. EventDescriptorCollection ICustomTypeDescriptor.GetEvents(Attribute[] attributes)
  373. {
  374. return TypeDescriptor.GetEvents(this, attributes, noCustomTypeDesc: true);
  375. }
  376.  
  377. PropertyDescriptorCollection ICustomTypeDescriptor.GetProperties(Attribute[] attributes)
  378. {
  379. return TypeDescriptor.GetProperties(this, attributes, noCustomTypeDesc: true);
  380. }
  381.  
  382. object ICustomTypeDescriptor.GetPropertyOwner(PropertyDescriptor pd)
  383. {
  384. return this;
  385. }
  386.  
  387. #endregion
  388. }
  389.  
  390. public class MyPropertyDescriptor: PropertyDescriptor
  391. {
  392.  
  393. MyProperty _prop;
  394. object _parent;
  395. public MyPropertyDescriptor(MyProperty prop, object parent)
  396. : base(prop.PropertyName, null)
  397. {
  398. _prop = prop;
  399. _parent = parent;
  400. }
  401.  
  402. public override AttributeCollection Attributes
  403. {
  404. get
  405. {
  406. var attributes = TypeDescriptor.GetAttributes(GetValue(null), false);
  407.  
  408. return attributes;
  409. }
  410. }
  411.  
  412. public override bool CanResetValue(object component)
  413. {
  414. return false;
  415. }
  416.  
  417. public override object GetValue(object component)
  418. {
  419. switch (_prop.Type)
  420. {
  421. case MyProperty.PropertyType.String:
  422. return _prop.StringValue;
  423. case MyProperty.PropertyType.Number:
  424. return _prop.NumberValue;
  425. default:
  426. break;
  427. }
  428. return null;
  429. }
  430.  
  431.  
  432.  
  433. public override void ResetValue(object component)
  434. {
  435. throw new NotImplementedException();
  436. }
  437.  
  438. public override void SetValue(object component, object value)
  439. {
  440. switch (_prop.Type)
  441. {
  442. case MyProperty.PropertyType.String:
  443. _prop.StringValue = value as string;
  444. break;
  445. case MyProperty.PropertyType.Number:
  446. _prop.NumberValue = (double)value;
  447. break;
  448. default:
  449. break;
  450. }
  451. }
  452.  
  453. public override bool ShouldSerializeValue(object component)
  454. {
  455. return false;
  456. }
  457.  
  458. public override Type ComponentType
  459. => _parent.GetType();
  460.  
  461. public override bool IsReadOnly
  462. => false;
  463.  
  464. public override Type PropertyType
  465. {
  466. get
  467. {
  468. switch (_prop.Type)
  469. {
  470. case MyProperty.PropertyType.String:
  471. return _prop.StringValue.GetType();
  472. case MyProperty.PropertyType.Number:
  473. return _prop.NumberValue.GetType();
  474. default:
  475. return typeof(string);
  476. }
  477. }
  478. }
  479. }
  480.  
  481. /// <summary>
  482. /// Interaction logic for MainWindow.xaml
  483. /// </summary>
  484. public partial class MainWindow : Window
  485. {
  486. public MainWindow()
  487. {
  488. InitializeComponent();
  489.  
  490. EntityProperties pp = new EntityProperties()
  491. {
  492. myProperties = new List<MyProperty>()
  493. {
  494. new MyProperty() { Type = MyProperty.PropertyType.String, StringValue = "simple", PropertyName = "EntityType" },
  495. new MyProperty() { Type = MyProperty.PropertyType.Number, NumberValue = 123, PropertyName = "EntityID" }
  496. }
  497. };
  498.  
  499.  
  500. _propertyGrid.SelectedObject = pp;
  501.  
  502. _propertyGrid.AutoGenerateProperties = true;
  503. }
  504. }
  505.  
  506. public class MyProperty
  507. {
  508. public enum PropertyType
  509. {
  510. String,
  511. Number
  512. }
  513.  
  514. public PropertyType Type { get; set; }
  515.  
  516. public string StringValue { get; set; }
  517.  
  518. public double NumberValue { get; set; }
  519.  
  520. public string PropertyName { get; set; }
  521.  
  522. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement