-------------
Code
public
class ExtendedGridViewDataColumn : GridViewDataColumn
{
#region
"Properties"
public int Index { get; set; }
public int DataType { get; set; }
#endregion
#region
"Methods"
public override FrameworkElement CreateCellElement(GridViewCell cell, object dataItem)
{
FrameworkElement cellElement = null;
TextBlock textBlock = new TextBlock();
Binding binding = new Binding();
string valueProperty = GetPropertyNameForEditorType();
if (((AttributeDataType)DataType)== AttributeDataType.Date)
{
SetBindingProperties(binding, valueProperty,
true);
binding.Converter =
new DateTimeConverter();
textBlock.SetBinding(
TextBlock.TextProperty, binding);
}
else
{
SetBindingProperties(binding, valueProperty,
false);
textBlock.SetBinding(
TextBlock.TextProperty, binding);
}
cellElement = textBlock;
return cellElement;
}
public override FrameworkElement CreateCellEditElement(GridViewCell cell, object dataItem)
{
FrameworkElement cellEditElement = null;
string valueProperty = GetPropertyNameForEditorType();
Binding binding = new Binding();
switch ((AttributeDataType)DataType)
{
case AttributeDataType.Text:
{
cellEditElement =
this.CreateTextEditor(binding, valueProperty);
break;
}
case AttributeDataType.Flag:
{
cellEditElement =
this.CreateCheckBoxEditor(binding, valueProperty);
break;
}
case AttributeDataType.Date:
{
cellEditElement =
this.CreateDateTimeEditor(binding, valueProperty);
break;
}
default:
{
cellEditElement =
this.CreateTextEditor(binding, valueProperty);
break;
}
}
return cellEditElement;
}
private FrameworkElement CreateCheckBoxEditor(Binding binding, string valueProperty)
{
CheckBox checkBox = new CheckBox();
SetBindingProperties(binding, valueProperty,
true);
checkBox.SetBinding(
CheckBox.IsCheckedProperty, binding);
checkBox.Loaded +=
this.Editor_Loaded;
return checkBox;
}
private FrameworkElement CreateDateTimeEditor(Binding binding, string valueProperty)
{
RadDatePicker datePicker = new RadDatePicker();
SetBindingProperties(binding, valueProperty,
true);
binding.Converter =
new DateTimeConverter();
datePicker.SetBinding(
RadDatePicker.SelectedDateProperty, binding);
datePicker.Loaded +=
this.Editor_Loaded;
return datePicker;
}
private FrameworkElement CreateTextEditor(Binding binding, string valueProperty)
{
TextBox textBox = new TextBox();
SetBindingProperties(binding, valueProperty,
true);
textBox.SetBinding(
TextBox.TextProperty, binding);
textBox.Loaded +=
this.textBox_Loaded;
return textBox;
}
private static void SetBindingProperties(Binding valueBinding, string propertyName, bool twoway)
{
if (twoway)
{
valueBinding.Mode =
BindingMode.TwoWay;
}
else
{
valueBinding.Mode =
BindingMode.OneWay;
}
valueBinding.Path =
new PropertyPath(propertyName);
valueBinding.NotifyOnValidationError =
true;
valueBinding.ValidatesOnExceptions =
true;
}
private string GetPropertyNameForEditorType()
{
string propertyName;
switch ((AttributeDataType)DataType)
{
case AttributeDataType.Number:
{
propertyName =
"Number";
break;
}
case AttributeDataType.Flag:
{
propertyName =
"Flag";
break;
}
case AttributeDataType.Date:
{
propertyName =
"Date";
break;
}
default:
{
propertyName =
"Text";
break;
}
}
return propertyName;
}
#endregion
#region
"Events"
void Editor_Loaded(object sender, RoutedEventArgs e)
{
Control target = sender as Control;
if (target != null)
{
target.Focus();
}
}
void textBox_Loaded(object sender, RoutedEventArgs e)
{
var textBox = (sender as TextBox);
if (textBox != null)
{
textBox.Focus();
textBox.SelectAll();
}
}
#endregion
}