Hi guys!
I was tasked with finding a way to rewrite our old WinForms app to WPF (due to some experience with it in time long past), with special attention for PropertyGrid controls. Decided to go with Telerik and to my surprise it has the same problem as most open source solutions, namely: when binding to PropertyBag containing enum definitions, enum values are not selected in the dropdowns - the options are there, just unselected. I'm pretty sure I do something wrong, so I'd appreciate a look at the code and some tips. Code for View, ViewModel and CustomProperty/Descriptor can be found below:
View:
1.<Window.Resources>2. <my:MyViewModel x:Key="MyViewModel"/>3. </Window.Resources>4. <Grid DataContext="{StaticResource MyViewModel}">5. <telerik:RadPropertyGrid HorizontalAlignment="Left" Name="radPropertyGrid1" VerticalAlignment="Top" Height="452" Width="465" Item="{Binding Collection}"/>6. </Grid>ViewModel:
01.public class MyViewModel : INotifyPropertyChanged02. {03. public event PropertyChangedEventHandler PropertyChanged;04. ObservableCollection<CustomProperty> cpList = new ObservableCollection<CustomProperty>()05. {06. new CustomProperty { Name = "Foo", Desc = "Test", Type = typeof(int), Value1 = 0 },07. new CustomProperty { Name = "Foo2", Desc = "Test2", Type = typeof(int), Value1 = 1 },08. new CustomProperty { Name = "Foo3", Desc = "Test3", Type = typeof(WpfApplication1.Enum), Value1 = WpfApplication1.Enum.Value2 }09. };10. 11. public PropertyBag Collection12. {13. get14. {15. return new PropertyBag(cpList);16. }17. }18. 19. private object selectedItem;20. public object SelectedItem21. {22. get { return this.selectedItem; }23. set24. {25. if (value != this.selectedItem)26. {27. this.selectedItem = value;28. this.OnPropertyChanged("SelectedItem");29. }30. }31. }32. 33. protected virtual void OnPropertyChanged(PropertyChangedEventArgs args)34. {35. PropertyChangedEventHandler handler = this.PropertyChanged;36. if (handler != null)37. {38. handler(this, args);39. }40. }41. 42. private void OnPropertyChanged(string propertyName)43. {44. this.OnPropertyChanged(new PropertyChangedEventArgs(propertyName));45. }46. }CustomProperty/Descriptor:
001.public enum Enum002. {003. Value1 = 1,004. Value2 = 2005. }006. 007. public class CustomProperty008. {009. public string Name { get; set; }010. public string Desc { get; set; }011. public object DefaultValue { get; set; }012. public object Value1 013. { 014. get { return currentValue; } 015. set016. { 017. currentValue = value;018. } 019. }020. 021. Type type;022. public Type Type023. {024. get025. {026. return type;027. }028. set029. {030. type = value;031. DefaultValue = Activator.CreateInstance(value);032. }033. }034. 035. private bool validate;036. private PropertyBag subProperty;037. private object currentValue;038. 039. private string GetText(string textId)040. {041. return textId;042. }043. 044. public string Id045. {046. get { return string.Empty; }047. }048. 049. public string Category050. {051. get { return "Category"; }052. }053. 054. public string Hint055. {056. get057. {058. return "Hint";059. }060. }061. 062. public bool UpperCase063. {064. get { return false; }065. }066. 067. public string EditorTypeName068. {069. get { return "Naaah"; }070. }071. 072. public string DropDownEditor073. {074. get { return "Uh-uh"; }075. }076. 077. public string Caption078. {079. get { return Name; }080. }081. 082. public string TypeName083. {084. get { return type.ToString(); }085. }086. 087. public bool PermanentReadOnly088. {089. get { return false; }090. }091. 092. public bool ReadOnly093. {094. get { return false; }095. }096. 097. public string MinValue098. {099. get { return ""; }100. }101. 102. public string MaxValue103. {104. get { return "0"; }105. }106. 107. public bool Validate108. {109. get { return validate; }110. }111. 112. public PropertyBag SubProperty113. {114. get {return subProperty;}115. set {subProperty = value;}116. }117. 118. public int SortOrder119. {120. get { return 0; }121. }122. 123. public bool Hide124. {125. get { return false; }126. }127. 128. public string DevTypeConverter129. {130. get { return "None"; }131. }132. 133. public string GetAttribute(string key)134. {135. return "";// this.deviceData != null ? this.deviceData.GetAttribute(key) : null;136. }137. }138. 139. public class CustomPropertyDescriptor : PropertyDescriptor140. {141. public CustomProperty pSpec = null;142. public PropertyBag pBag;143. 144. public CustomPropertyDescriptor(CustomProperty cp, PropertyBag pb, Attribute[] attrs) : base(cp.Name, attrs)145. {146. pSpec = cp;147. pBag = pb;148. }149. 150. public override Type ComponentType151. {152. get { return typeof(CustomProperty); }153. }154. 155. public override object GetValue(object component)156. {157. if (pSpec.DefaultValue is System.Enum)158. return (int)pSpec.Value1;159. return pSpec.Value1; 160. }161. 162. public override bool IsReadOnly163. {164. get { return false; }165. }166. 167. public override Type PropertyType168. {169. get170. {171. return pSpec.Type;172. }173. }174. 175. public override void ResetValue(object component)176. {177. SetValue(component, pSpec.DefaultValue);178. }179. 180. public override void SetValue(object component, object value)181. {182. pSpec.Value1 = value;183. }184. 185. private string GetText(string textId)186. {187. return textId;188. }189. 190. public string GetPropId191. {192. get193. { return pSpec.Id; }194. }195. 196. public override string Category197. {198. get199. {200. return pSpec.Category;201. }202. }203. 204. public override string DisplayName205. {206. get207. {208. return pSpec.Caption;209. }210. }211. 212. public override string Description213. {214. get215. {216. return pSpec.Hint;217. }218. }219. 220. public override bool IsBrowsable221. {222. get223. {224. return true;225. }226. }227. 228. public override bool CanResetValue(object component)229. {230. return false;231. }232. 233. public override bool ShouldSerializeValue(object component)234. {235. if (pSpec.TypeName == "BoolDefined")236. {237. if (pSpec.Value1 == null)238. return false;239. if (pSpec.Value1.ToString().Length > 0)240. if (Convert.ToBoolean(pSpec.Value1, CultureInfo.InvariantCulture))241. return true;242. }243. return false;244. }245. }Thanks in advance for any help you can offer:)