I have a RadGridView setup as follows:
<tk:RadGridView Grid.Column="0" Grid.ColumnSpan="2" Grid.Row="1" Height="175" Margin="0,5,0,10" Width="960"
AutoGenerateColumns="False" CanUserDeleteRows="False" CanUserFreezeColumns="False" CanUserInsertRows="False"
CanUserReorderColumns="False" CanUserResizeColumns="False" CanUserSelect="True" CanUserSortColumns="False"
HorizontalAlignment="Left" IsFilteringAllowed="False" ItemsSource="{Binding EquipmentToAdd}" MinHeight="150"
RowIndicatorVisibility="Collapsed" SelectionUnit="Cell" ShowGroupPanel="False" VerticalAlignment="Bottom" ActionOnLostFocus="CommitEdit">
<i:Interaction.Behaviors>
<cv:CellValidationBehavior />
<cf:EquipmentQuickEntryGrid/>
</i:Interaction.Behaviors>
<tk:RadGridView.Columns>
<tk:GridViewDataColumn Width="50" DataMemberBinding="{Binding Path=Count}" Header="Count" IsReadOnly="True" />
<tk:GridViewDataColumn Width="110" DataMemberBinding="{Binding Path=BaseEquipmentType}" Header="Base Type" IsReadOnly="True" />
<tk:GridViewDataColumn Width="150" DataMemberBinding="{Binding Path=EquipmentType}" Header="Equipment Type" IsReadOnly="True" />
<tk:GridViewDataColumn Width="130" DataMemberBinding="{Binding Path=Manufacturer}" Header="Manufacturer" IsReadOnly="True" />
<tk:GridViewDataColumn Width="150" DataMemberBinding="{Binding Path=Model}" Header="Model" IsReadOnly="True" />
<tk:GridViewDataColumn Width="150" DataMemberBinding="{Binding Path=AssetNumber}" Header="Asset Number" IsReadOnly="True" />
<tk:GridViewDataColumn Width="150" Header="Serial Number" CellTemplateSelector="{StaticResource isAssetInUseSerialNumberTemplateSelector}" UniqueName="SerialNumber" IsReadOnlyBinding="{Binding IsInUse}"/>
<tk:GridViewDataColumn Width="55" Header="Remove" CellTemplateSelector="{StaticResource isAssetInUseDeleteTemplateSelector}" IsReadOnlyBinding="{Binding IsInUse}" />
<tk:GridViewDataColumn Width="50" DataMemberBinding="{Binding Path=IsInUse}" IsVisible="False" />
</tk:RadGridView.Columns>
<tk:RadGridView.GridViewGroupPanel>
<tk:GridViewGroupPanel AllowDrop="False" IsEnabled="False" Visibility="Hidden" />
</tk:RadGridView.GridViewGroupPanel>
</tk:RadGridView>
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??
<tk:RadGridView Grid.Column="0" Grid.ColumnSpan="2" Grid.Row="1" Height="175" Margin="0,5,0,10" Width="960"
AutoGenerateColumns="False" CanUserDeleteRows="False" CanUserFreezeColumns="False" CanUserInsertRows="False"
CanUserReorderColumns="False" CanUserResizeColumns="False" CanUserSelect="True" CanUserSortColumns="False"
HorizontalAlignment="Left" IsFilteringAllowed="False" ItemsSource="{Binding EquipmentToAdd}" MinHeight="150"
RowIndicatorVisibility="Collapsed" SelectionUnit="Cell" ShowGroupPanel="False" VerticalAlignment="Bottom" ActionOnLostFocus="CommitEdit">
<i:Interaction.Behaviors>
<cv:CellValidationBehavior />
<cf:EquipmentQuickEntryGrid/>
</i:Interaction.Behaviors>
<tk:RadGridView.Columns>
<tk:GridViewDataColumn Width="50" DataMemberBinding="{Binding Path=Count}" Header="Count" IsReadOnly="True" />
<tk:GridViewDataColumn Width="110" DataMemberBinding="{Binding Path=BaseEquipmentType}" Header="Base Type" IsReadOnly="True" />
<tk:GridViewDataColumn Width="150" DataMemberBinding="{Binding Path=EquipmentType}" Header="Equipment Type" IsReadOnly="True" />
<tk:GridViewDataColumn Width="130" DataMemberBinding="{Binding Path=Manufacturer}" Header="Manufacturer" IsReadOnly="True" />
<tk:GridViewDataColumn Width="150" DataMemberBinding="{Binding Path=Model}" Header="Model" IsReadOnly="True" />
<tk:GridViewDataColumn Width="150" DataMemberBinding="{Binding Path=AssetNumber}" Header="Asset Number" IsReadOnly="True" />
<tk:GridViewDataColumn Width="150" Header="Serial Number" CellTemplateSelector="{StaticResource isAssetInUseSerialNumberTemplateSelector}" UniqueName="SerialNumber" IsReadOnlyBinding="{Binding IsInUse}"/>
<tk:GridViewDataColumn Width="55" Header="Remove" CellTemplateSelector="{StaticResource isAssetInUseDeleteTemplateSelector}" IsReadOnlyBinding="{Binding IsInUse}" />
<tk:GridViewDataColumn Width="50" DataMemberBinding="{Binding Path=IsInUse}" IsVisible="False" />
</tk:RadGridView.Columns>
<tk:RadGridView.GridViewGroupPanel>
<tk:GridViewGroupPanel AllowDrop="False" IsEnabled="False" Visibility="Hidden" />
</tk:RadGridView.GridViewGroupPanel>
</tk:RadGridView>
The CellValidationBehavior logic executes on Attach and Detach:
/// <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??