I have a RadGridView setup as follows:
The CellValidationBehavior logic executes on Attach and Detach:
using Telerik.Windows.Controls;
/// <summary>
/// TODO: Update summary.
/// </summary>
public class CellValidationBehavior : Behavior<RadGridView>
{
private EquipmentQuickEntryViewModel _viewModel = null;
/// <summary>
/// Overrides the OnAttached event so we can hook up the RowLoaded event
/// </summary>
protected override void OnAttached()
{
base.OnAttached();
this.AssociatedObject.DataLoaded += new EventHandler<EventArgs>(DataLoaded);
this.AssociatedObject.CellValidating += new EventHandler<GridViewCellValidatingEventArgs>(CellValidating);
this.AssociatedObject.RowValidated += new EventHandler<GridViewRowValidatedEventArgs>(RowValidated);
}
/// <summary>
/// Overrides the OnDetaching method to add handlers to some DragDrop events
/// </summary>
protected override void OnDetaching()
{
base.OnDetaching();
this.AssociatedObject.DataLoaded -= new EventHandler<EventArgs>(DataLoaded);
this.AssociatedObject.CellValidating -= new EventHandler<GridViewCellValidatingEventArgs>(CellValidating);
this.AssociatedObject.RowValidated -= new EventHandler<GridViewRowValidatedEventArgs>(RowValidated);
}
/// <summary>
/// Captures the DataLoaded event from the grid
/// </summary>
/// <param name="sender">The RadGridView we attached to</param>
/// <param name="e">The EventArgs</param>
private void DataLoaded(object sender, EventArgs e)
{
var grid = this.AssociatedObject;
var dataSource = (ObservableCollection<EquipmentToAdd>)grid.ItemsSource;
if (dataSource.Count>0)
{
if (_viewModel == null)
{
_viewModel = grid.DataContext as EquipmentQuickEntryViewModel;
}
var selectedRow = _viewModel.SelectedRowNumber;
grid.ScrollIntoView(grid.Items[selectedRow], grid.Columns["SerialNumber"]);
grid.CurrentCellInfo = new GridViewCellInfo(grid.Items[selectedRow], grid.Columns["SerialNumber"]);
var equipment = (EquipmentToAdd)grid.Items[selectedRow];
if (equipment.IsInUse)
{
return;
}
grid.BeginEdit();
}
}
/// <summary>
/// Captures the CellValidating event from the grid
/// </summary>
/// <param name="sender">The RadGridView we attached to</param>
/// <param name="e">The GridViewCellValidatingEventArgs</param>
private void CellValidating(object sender, GridViewCellValidatingEventArgs e)
{
if (e.Cell.Column.UniqueName != "SerialNumber")
{
return;
}
var value = e.NewValue.ToString();
if (!string.IsNullOrEmpty(value))
{
if (this._viewModel == null)
{
this._viewModel = ((RadGridView)sender).DataContext as EquipmentQuickEntryViewModel;
}
EquipmentToAdd thisEquipment = (EquipmentToAdd)e.Row.DataContext;
var isDuplicate = this._viewModel.IsDuplicateSerialNumber(value, thisEquipment);
if (isDuplicate)
{
e.IsValid = false;
e.ErrorMessage = "That serial number already exists";
return;
}
_viewModel.UpdateCanSave();
}
}
/// <summary>
/// Captures the RowValidated event from the grid
/// </summary>
/// <param name="sender">The RadGridView we attached to</param>
/// <param name="e">The GridViewRowValidatedEventArgs</param>
private void RowValidated(object sender, GridViewRowValidatedEventArgs e)
{
if (_viewModel == null)
{
_viewModel = ((RadGridView)sender).DataContext as EquipmentQuickEntryViewModel;
}
_viewModel.UpdateCanSave();
}
}
I confirmed that it is being passed the RadGridView object. However when I enter data into the cell containing the serial number (only editable field) I do not get any validation events.
WHY??