This is a migrated thread and some comments may be shown as answers.

PropertyBag enums not showing selected values in the PropertyGrid

1 Answer 63 Views
PropertyGrid
This is a migrated thread and some comments may be shown as answers.
Rafał
Top achievements
Rank 1
Rafał asked on 19 Jul 2016, 02:30 PM

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 : INotifyPropertyChanged
02.    {
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 Collection
12.        {
13.            get
14.            {
15.                return new PropertyBag(cpList);
16.            }
17.        }
18. 
19.        private object selectedItem;
20.        public object SelectedItem
21.        {
22.            get { return this.selectedItem; }
23.            set
24.            {
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 Enum
002.    {
003.        Value1 = 1,
004.        Value2 = 2
005.    }
006. 
007.    public class CustomProperty
008.    {
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.            set
016.            {
017.                currentValue = value;
018.            }
019.        }
020. 
021.        Type type;
022.        public Type Type
023.        {
024.            get
025.            {
026.                return type;
027.            }
028.            set
029.            {
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 Id
045.        {
046.            get { return string.Empty; }
047.        }
048. 
049.        public string Category
050.        {
051.            get { return "Category"; }
052.        }
053. 
054.        public string Hint
055.        {
056.            get
057.            {
058.                return "Hint";
059.            }
060.        }
061. 
062.        public bool UpperCase
063.        {
064.            get { return false; }
065.        }
066. 
067.        public string EditorTypeName
068.        {
069.            get { return "Naaah"; }
070.        }
071. 
072.        public string DropDownEditor
073.        {
074.            get { return "Uh-uh"; }
075.        }
076. 
077.        public string Caption
078.        {
079.            get { return Name; }
080.        }
081. 
082.        public string TypeName
083.        {
084.            get { return type.ToString(); }
085.        }
086. 
087.        public bool PermanentReadOnly
088.        {
089.            get { return false; }
090.        }
091. 
092.        public bool ReadOnly
093.        {
094.            get { return false; }
095.        }
096. 
097.        public string MinValue
098.        {
099.            get { return ""; }
100.        }
101. 
102.        public string MaxValue
103.        {
104.            get { return "0"; }
105.        }
106. 
107.        public bool Validate
108.        {
109.            get { return validate; }
110.        }
111. 
112.        public PropertyBag SubProperty
113.        {
114.            get {return subProperty;}
115.            set {subProperty = value;}
116.        }
117. 
118.        public int SortOrder
119.        {
120.            get { return 0; }
121.        }
122. 
123.        public bool Hide
124.        {
125.            get { return false; }
126.        }
127. 
128.        public string DevTypeConverter
129.        {
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 : PropertyDescriptor
140.    {
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 ComponentType
151.        {
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 IsReadOnly
163.        {
164.            get { return false; }
165.        }
166. 
167.        public override Type PropertyType
168.        {
169.            get
170.            {
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 GetPropId
191.        {
192.            get
193.            { return pSpec.Id; }
194.        }
195. 
196.        public override string Category
197.        {
198.            get
199.            {
200.                return pSpec.Category;
201.            }
202.        }
203. 
204.        public override string DisplayName
205.        {
206.            get
207.            {
208.                return pSpec.Caption;
209.            }
210.        }
211. 
212.        public override string Description
213.        {
214.            get
215.            {
216.                return pSpec.Hint;
217.            }
218.        }
219. 
220.        public override bool IsBrowsable
221.        {
222.            get
223.            {
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:)

1 Answer, 1 is accepted

Sort by
0
Rafał
Top achievements
Rank 1
answered on 21 Jul 2016, 11:44 AM
Fixed. Value was being returned as int, not Enum. Thank you legacy code.
Tags
PropertyGrid
Asked by
Rafał
Top achievements
Rank 1
Answers by
Rafał
Top achievements
Rank 1
Share this question
or