This question is locked. New answers and comments are not allowed.
Hi,
We are trying to find the best way to be able to maintain the grid view validation and its event CellValidating when extending the GridViewDataColum. Currently we have a grid that has 2 columns, one is a regular GridViewDataColumn and the other one is an extended class column we created that extended the GridViewDataColumn. On that custom class we overrided the CreateCellElement method. Due to that, we have lost the validation and we are trying to figure out how to recover it so that the grid still performs its validation.
Thanks
-Oscar
PS: Below is the code of our custom class
We are trying to find the best way to be able to maintain the grid view validation and its event CellValidating when extending the GridViewDataColum. Currently we have a grid that has 2 columns, one is a regular GridViewDataColumn and the other one is an extended class column we created that extended the GridViewDataColumn. On that custom class we overrided the CreateCellElement method. Due to that, we have lost the validation and we are trying to figure out how to recover it so that the grid still performs its validation.
Thanks
-Oscar
PS: Below is the code of our custom class
| public class PropertyGridColumn : GridViewDataColumn |
| { |
| #region Declarations |
| private enum ElementToCreate { CheckBox, ComboBox, DatePicker, ListBox, TextBox } |
| #endregion |
| #region Methods : Public |
| public override FrameworkElement CreateCellElement(GridViewCell cell, object dataItem) |
| { |
| FrameworkElement el = base.CreateCellEditElement(cell, dataItem); |
| PropertyGridItem item = (PropertyGridItem) dataItem; |
| Binding binding = new Binding() |
| { |
| Mode = BindingMode.TwoWay, |
| Source = item, |
| Path = new PropertyPath("Value", new object[] {}) |
| }; |
| if(item.ReadOnly) |
| { |
| return el; |
| } |
| switch (item.EditorType) |
| { |
| case EditorOption.Inherit: |
| if (item.ValidValues.ItemsSource == null) |
| if (item.ValueType == typeof(bool)) |
| el = CreateFrameworkElementWithBinding(ElementToCreate.CheckBox, binding); |
| else if (item.ValueType == typeof(DateTime)) |
| el = CreateFrameworkElementWithBinding(ElementToCreate.DatePicker, binding); |
| else if (item.ValueType == typeof(string) |
| || item.ValueType.IsPrimitive) |
| el = CreateFrameworkElementWithBinding(ElementToCreate.TextBox, binding); |
| else if (item.ValueType.IsEnum) |
| { |
| el = CreateFrameworkElementWithBinding(ElementToCreate.ComboBox, binding, |
| new PropertyGridControlValidValuesAttribute(){ItemsSource = EnumHelper.GetValues(item.ValueType)}); |
| } |
| else ; |
| else |
| { |
| el = CreateFrameworkElementWithBinding(ElementToCreate.ComboBox, binding, item.ValidValues); |
| } |
| break; |
| case EditorOption.CheckBox: |
| el = CreateFrameworkElementWithBinding(ElementToCreate.CheckBox, binding); |
| break; |
| case EditorOption.ComboBox: |
| el = CreateFrameworkElementWithBinding(ElementToCreate.ComboBox, binding, item.ValidValues); |
| break; |
| case EditorOption.DatePicker: |
| el = CreateFrameworkElementWithBinding(ElementToCreate.DatePicker, binding); |
| break; |
| case EditorOption.ListBox: |
| el = CreateFrameworkElementWithBinding(ElementToCreate.ListBox, binding, item.ValidValues); |
| break; |
| case EditorOption.TextBox: |
| el = CreateFrameworkElementWithBinding(ElementToCreate.TextBox, binding); |
| break; |
| } |
| return el; |
| } |
| #endregion |
| #region Methods : Private |
| private static FrameworkElement CreateFrameworkElementWithBinding(ElementToCreate elementToCreate, Binding binding) |
| { |
| var dummyValue = new PropertyGridControlValidValuesAttribute(); |
| return CreateFrameworkElementWithBinding(elementToCreate, binding, dummyValue); |
| } |
| private static FrameworkElement CreateFrameworkElementWithBinding(ElementToCreate elementToCreate, Binding binding, PropertyGridControlValidValuesAttribute validValues) |
| { |
| switch (elementToCreate) |
| { |
| case ElementToCreate.CheckBox: |
| { |
| CheckBox cb = new CheckBox(); |
| //cb.IsThreeState = false; |
| cb.SetBinding(CheckBox.IsCheckedProperty, binding); |
| return cb; |
| } |
| case ElementToCreate.ComboBox: |
| { |
| //RadComboBox |
| ComboBox cb = new ComboBox(); |
| AddValidValuesBindingToFrameworkElement(cb, validValues); |
| cb.SetBinding(ComboBox.SelectedItemProperty, binding); |
| return cb; |
| } |
| case ElementToCreate.DatePicker: |
| { |
| //RadDatePicker |
| DatePicker dp = new DatePicker(); |
| dp.SetBinding(DatePicker.SelectedDateProperty, binding); |
| return dp; |
| } |
| case ElementToCreate.ListBox: |
| { |
| //using statements |
| ListBox lb = new ListBox(); |
| AddValidValuesBindingToFrameworkElement(lb, validValues); |
| lb.SetBinding(ListBox.SelectedItemProperty, binding); |
| return lb; |
| } |
| case ElementToCreate.TextBox: |
| { |
| TextBox tb = new TextBox(); |
| tb.SetBinding(TextBox.TextProperty, binding); |
| return tb; |
| } |
| default: |
| throw new ArgumentException("Invalid Element to Create", "elementToCreate"); |
| } |
| } |
| private static void AddValidValuesBindingToFrameworkElement(Selector se, PropertyGridControlValidValuesAttribute validValues) |
| { |
| Binding binding = new Binding() |
| { |
| Mode = BindingMode.TwoWay, |
| Source = validValues, |
| Path = new PropertyPath("ItemsSource", new object[] {}) |
| }; |
| se.SetBinding(Selector.ItemsSourceProperty, binding); |
| if (!string.IsNullOrEmpty(validValues.DisplayMemberPath)) |
| se.DisplayMemberPath = validValues.DisplayMemberPath; |
| //if (!string.IsNullOrEmpty(validValues.SelectedValuePath)) |
| //se.SelectedValuePath = validValues.SelectedValuePath; |
| } |
| #endregion |
| } |