Hi,
following the demo I've created an attached property to my RadGridView that adds a column that displays the number of the row.
Everything works fine, but if my grid has a group descriptor defined in xaml or loaded from the custom settings saved by the user, then the numbers appear not sorted.
It seems like the numeration happens before the grouping, but I don't know how to fix it.
Here's my code:
public class RowNumeratorBehavior
{
public static bool GetRowNumerator(RadGridView grid)
{
return (bool)grid.GetValue(RowNumeratorProperty);
}
public static void SetRowNumerator(RadGridView grid, bool value)
{
grid.SetValue(RowNumeratorProperty, value);
}
public static readonly DependencyProperty RowNumeratorProperty =
DependencyProperty.RegisterAttached("RowNumerator", typeof(bool), typeof(RowNumeratorBehavior), new UIPropertyMetadata(OnAutoSelectedChanged));
static void OnAutoSelectedChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
{
RadGridView source = (sender as RadGridView);
if (source == null) return;
if ((bool)e.NewValue)
{
source.DataContextChanged += source_DataContextChanged;
Telerik.Windows.Persistence.Services.ServiceProvider.RegisterPersistenceProvider<Telerik.Windows.Persistence.Services.ICustomPropertyProvider>(typeof(RadGridView), new GridViewCustomPropertyProvider());
}
else
{
source.DataContextChanged -= source_DataContextChanged;
}
}
private static void source_DataContextChanged(object sender, DependencyPropertyChangedEventArgs e)
{
RadGridView source = (sender as RadGridView);
var numerator = new RowNumberColumn();
numerator.Header = "";
numerator.Name = "Numerator";
numerator.UniqueName = "Numerator";
numerator.IsReorderable = false;
numerator.Background = System.Windows.Media.Brushes.White;
source.Columns.Insert(0,numerator);
}
}
public class RowNumberColumn : Telerik.Windows.Controls.GridViewDataColumn
{
public override System.Windows.FrameworkElement CreateCellElement(Telerik.Windows.Controls.GridView.GridViewCell cell, object dataItem)
{
TextBlock textBlock = cell.Content as TextBlock;
if (textBlock == null)
{
textBlock = new TextBlock();
}
textBlock.Text = ((this.DataControl.ItemsSource as IList).IndexOf(dataItem) + 1).ToString();
return textBlock;
}
protected override void OnPropertyChanged(System.ComponentModel.PropertyChangedEventArgs args)
{
base.OnPropertyChanged(args);
if (args.PropertyName == "DataControl")
{
if (this.DataControl != null && this.DataControl.Items != null)
{
this.DataControl.Items.CollectionChanged += (s, e) =>
{
if (e.Action == System.Collections.Specialized.NotifyCollectionChangedAction.Remove)
{
this.Refresh();
}
};
}
}
}
}
Thanks in advance,
Matteo