If you need a custom editor to edit a specific data, you can use one of the following approaches:
As almost anything in our life both approaches have some advantages and disadvantages. Although it is quite easy to implement the first option, there are some significant disadvantages like not so usable with many RadGridView instances, and more important this bypasses the RadGridView validation and editing engine. For more information about this approach you can take a look at this online example.
In this blog post I’ll try to explain how to implement and take maximum advantages from the second option (needs more efforts to implement).
I’ll demonstrate the second approach by creating a column with embedded color picker control as an editor. I will start with creating a class that inherits from GridViewBoundColumnBase (this is the base class used to create a column with editing capabilities). There are several methods you have to override in order to get this column to work:
1. CreateCellElement – override this method if you want to customize how cells that belongs to this column will look like. This method is called when GridViewCell is prepared and returned element will be used as a ContentPresenter. If you do not override this method a TextBlock control will be used as a default presenter.
There is nothing unusual here just a border with bound background to the color from data item.
2. CreateCellEditElement – override this method to create custom editor element (according to type of the property or some other business logic). This method must be overridden otherwise GridViewCell will have no content when enters into edit mode.
In this method we create and return an instance of a RadColorPicker control. In order to work properly as an editor you have to bind this editor to the underlying data property. Lets take a look at the CreateValueBinding method:
We set BindingMode to TwoWay, because this is an editor and we need to update data property which is bound to the parent GridViewColumn.
NotifyOnValidationError and ValidatesOnExceptions properties are related to validation engine (if any error or exception occurs while we set new value to the data object will result as validation error and editor will enter into invalid state (if editor has such state)).
Set UpdateSourceTrigger to Explicit and allow RadGridView to validate and update the value of the data item at the right moment.
Of course every TwoWay binding requires a path, so we take the path from the DataMemberBinding property.
Another interesting line of the CreateCellEditElement is: cellEditElement.MainPalette = this.MainPalette. This line shows how you can transfer properties from column to the actual editor. In order to allow such properties and transfer them to custom column instance you have to override CopyPropertiesFrom method:
Then you can set this custom property via xaml:
Call to the base method is very critical because if you do not call base method properties like DataMemberBinding will not be copied to the RadColorPickerColumn.
And now we have to integrate this column into the RadGridView validation and editing engine. There are two methods which you have to override:
As you can see first method gathers required information from the actual editor (used by the validation engine), after UI validation is successful then new value is submitted to the data item via second method. This second method returns errors (if any) that occurred while new value is set to the data item (Data layer validation).
There is another useful property which you probably already noticed - BindingTarget property. Generally the actual editor is a single control which has a ValueProperty bound to the data item. When you set BindingTarget property to this ValueProperty, there is no need to override GetNewValueFromEditor and UpdateSourceWithEditorValue methods. Base methods will take necessary actions to validate and update the value. There is only one restriction this ValueProperty must be a direct property of the CellEditElement returned as a result of the CreateCellEditElement method. In this example BindingTarget property is set to RadColorPicker.SelectedColorProperty. If you have a composite UserControl you have to set BindingTarget to UserControl1.ValueProperty.
You can play with this example:
Here is the source code of the example.
Subscribe to be the first to get our expert-written articles and tutorials for developers!