This is a corollary to one of my previous posts entitled ‘Nullable’ GridViewComboBoxColumn. Basically, I have a business object named Person. I am trying to bind a list of these Person business objects in a RadGridView. One of the properties in Person is an enumeration named PetType. I would like this column in the grid to be a combo box that will let the user pick from a list of possible PetType values.
At first, I was having trouble showing the desired display member (PetType.ToString()) after the user had selected a value. The column seemed to want to show the (integer) value of the enumeration (e.g. 1, 2, 3, etc.) instead of the string representation (‘Dog’, ‘Cat’, ‘Cow’ etc.). I made an attempt to remedy this by modifying my value converter. So, specifically...
My value converter looked like this, but the integer value was being displayed after a value was selected (not desired):
| public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture) |
| { |
| if (value != null) |
| { |
| return (int)value; |
| } |
| return null; |
| } |
I then modified my value converter to look like this. Now, the string value is being displayed after a value was selected (this is desired):
| public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture) |
| { |
| if (value != null) |
| { |
| return value.ToString(); |
| } |
| return null; |
| } |
So, the modification to my value converter is simple and seems to makes sense...
For the most, part this works great. However, there now seems to be a slight glitch when the user first enters edit mode. It seems that the binding is temporarily disrupted and the combo box’s value is cleared. Once the user selects a value, everything looks and behaves correctly.
I have included a couple of screen captures that show exactly what I am experiencing (http://www.4shared.com/file/123920806/aec5f96/IntegerValue.html and http://www.4shared.com/file/123920806/aec5f96/StringValue.html). Also, the relevant portions of my code are below. I am curious if this is a bug? Am I simply leaving something out? Or is there a different approach overall that I should use?
I should mention that I am using Silverlight 2 and the Q2/2009 controls.
Here is my Person business object and PetType enum:
| public class Person |
| { |
| public Person(int age, string firstName, string lastName, PetType petType) |
| { |
| this.Age = age; |
| this.FirstName = firstName; |
| this.LastName = lastName; |
| this.PetType = petType; |
| } |
| public int Age { get; set; } |
| public string FirstName { get; set; } |
| public string LastName { get; set; } |
| public PetType PetType { get; set; } |
| public static List<Person> GetSampleListOfPersons() |
| { |
| var persons = new List<Person> |
| { |
| new Person(30, "John", "Smith", PetType.Cat), |
| new Person(29, "Jane", "Smith", PetType.Cow), |
| new Person(30, "Peter", "Smith", PetType.Dog) |
| }; |
| return persons; |
| } |
| } |
| public enum PetType |
| { |
| Dog = 1, |
| Cat = 2, |
| Cow = 3 |
| } |
This is how I created my grid:
| this.RadGridView1.AutoGenerateColumns = false; |
| this.RadGridView1.Columns.Add(new GridViewDataColumn() { UniqueName = "Age" }); |
| this.RadGridView1.Columns.Add(new GridViewDataColumn() { UniqueName = "FirstName" }); |
| this.RadGridView1.Columns.Add(new GridViewDataColumn() { UniqueName = "LastName" }); |
| GridViewDataColumn comboBoxColumn = new GridViewDataColumn() { UniqueName = "PetType" }; |
| comboBoxColumn.DataMemberBinding = new Binding() |
| { |
| Path = new PropertyPath("PetType"), |
| Mode = BindingMode.TwoWay, |
| Converter = new ValueConverter() |
| }; |
| ComboBoxEditorSettings editorSettings = new ComboBoxEditorSettings(); |
| editorSettings.SelectedValuePath = "Key"; |
| editorSettings.DisplayMemberPath = "Value"; |
| editorSettings.ItemsSource = this.CreatePetTypeList(); |
| comboBoxColumn.EditorSettings = editorSettings; |
| this.RadGridView1.Columns.Add(comboBoxColumn); |
| this.RadGridView1.ItemsSource = Person.GetSampleListOfPersons(); |
The method CreatePetTypeList is defined as follows. ValueListItem is a simple class with a key(int)/value(string) pair.
| public IEnumerable<ValueListItem> CreatePetTypeList() |
| { |
| List<ValueListItem> list = new List<ValueListItem>(); |
| list.Add(new ValueListItem() { Key = 1, Value = PetType.Dog.ToString() }); |
| list.Add(new ValueListItem() { Key = 2, Value = PetType.Cat.ToString() }); |
| list.Add(new ValueListItem() { Key = 3, Value = PetType.Cow.ToString() }); |
| return list.AsEnumerable(); |
| } |