我正在使用Telerik的RadgridView和RadDataPager进行分页。附加行为以获取网格的所有选定记录。但是在分页时不保留选定的记录(移动到下一页时清除选定的记录)。
要解决此问题,请处理radgridview.Items.PageIndexChanging事件以检查分页是否已完成。订阅此事件会影响gridview的标题行选择。选中标题行复选框(选择所有选项)将选择所有网格行,但不选择标题行。第二次复选框选择选择标题行。
如何解决此问题?
<telerik:RadGridView Grid.Row="1"
Grid.Column="0"
GroupRenderMode="Flat"
ColumnWidth="*"
Margin="5"
x:Name="radGridViewSupportingDocument"
IsSynchronizedWithCurrentItem="False"
ItemsSource="{Binding PagedSource, ElementName=radDataPagerSupportingDocument}"
common:CustomFilterBehavior.TextBox="{Binding ElementName=radAutoCompleteBoxSearch}"
Style="{StaticResource radGridViewStyle}"
SelectionMode="Extended"
IsReadOnly="True"
>
<telerik:RadDataPager PageSize="30"
x:Name="radDataPagerSupportingDocument"
Margin="5"
Source="{Binding Documents}"
IsTotalItemCountFixed="True"
DisplayMode="FirstLastPreviousNextNumeric, Text"
Grid.ColumnSpan="2"
Grid.Row="2"
Grid.Column="2"
/>
public class MultiSelectBehavior : Behavior<RadGridView>
{
bool _collectionChangedSuspended = false;
private bool _isPageIndexChanged;
private ICollection removedItems = null;
protected override void OnAttached()
{
AssociatedObject.SelectedItems.CollectionChanged += GridSelectedItems_CollectionChanged;
AssociatedObject.SelectionChanging += AssociatedObject_SelectionChanging;
AssociatedObject.SelectionChanged += AssociatedObject_SelectionChanged;
AssociatedObject.Items.PageChanging += Items_PageChanging;
AssociatedObject.Loaded += AssociatedObject_Loaded;
base.OnAttached();
}
void AssociatedObject_Loaded(object sender, RoutedEventArgs e)
{
(sender as RadGridView).Focus();
}
void AssociatedObject_SelectionChanged(object sender, SelectionChangeEventArgs e)
{
if (_isPageIndexChanged)
{
foreach (var item in removedItems)
{
if (!AssociatedObject.Items.Contains(item))
{
AssociatedObject.SelectedItems.Add(item);
}
}
removedItems = null;
_isPageIndexChanged = false;
}
}
void AssociatedObject_SelectionChanging(object sender, SelectionChangingEventArgs e)
{
if (_isPageIndexChanged)
{
removedItems = e.RemovedItems;
}
}
void Items_PageChanging(object sender, Telerik.Windows.Data.PageChangingEventArgs e)
{
_isPageIndexChanged = true;
}
protected override void OnDetaching()
{
AssociatedObject.SelectedItems.CollectionChanged -= GridSelectedItems_CollectionChanged;
AssociatedObject.SelectionChanging -= AssociatedObject_SelectionChanging;
AssociatedObject.Items.PageChanging -= Items_PageChanging;
AssociatedObject.SelectionChanged -= AssociatedObject_SelectionChanged;
base.OnDetaching();
}
/// <summary>
/// Getter/Setter for DependencyProperty, bound to the DataContext's SelectedItems ObservableCollection
/// </summary>
public INotifyCollectionChanged SelectedItems
{
get { return (INotifyCollectionChanged)GetValue(SelectedItemsProperty); }
set { SetValue(SelectedItemsProperty, value); }
}
/// <summary>
/// Dependency Property "SelectedItems" is target of binding to DataContext's SelectedItems
/// </summary>
public static readonly DependencyProperty SelectedItemsProperty =
DependencyProperty.Register("SelectedItems", typeof(INotifyCollectionChanged), typeof(MultiSelectBehavior), new PropertyMetadata(OnSelectedItemsPropertyChanged));
/// <summary>
/// PropertyChanged handler for DependencyProperty "SelectedItems"
/// </summary>
private static void OnSelectedItemsPropertyChanged(DependencyObject target, DependencyPropertyChangedEventArgs args)
{
INotifyCollectionChanged collection = args.NewValue as INotifyCollectionChanged;
if (collection != null)
{
// Hook to the Network's SelectedItems
collection.CollectionChanged += (target as MultiSelectBehavior).ContextSelectedItems_CollectionChanged;
}
}
/// <summary>
/// Will be called, when the Network's SelectedItems collection changes
/// </summary>
void ContextSelectedItems_CollectionChanged(object sender, NotifyCollectionChangedEventArgs e)
{
if (_collectionChangedSuspended) return; // Don't react recursively to CollectionChanged events
_collectionChangedSuspended = true;
// Select and unselect items in the grid
if (e.NewItems != null)
foreach (object item in e.NewItems)
AssociatedObject.SelectedItems.Add(item);
if (e.OldItems != null)
foreach (object item in e.OldItems)
AssociatedObject.SelectedItems.Remove(item);
_collectionChangedSuspended = false;
}
/// <summary>
/// Will be called when the GridView's SelectedItems collection changes
/// </summary>
void GridSelectedItems_CollectionChanged(object sender, NotifyCollectionChangedEventArgs e)
{
if (_collectionChangedSuspended) return; // Don't react recursively to CollectionChanged events
_collectionChangedSuspended = true;
// Select and unselect items in the DataContext
if (e.NewItems != null)
foreach (object item in e.NewItems)
(SelectedItems as IList).Add(item);
if (e.OldItems != null)
foreach (object item in e.OldItems)
(SelectedItems as IList).Remove(item);
_collectionChangedSuspended = false;
}
}