I have a GridView where the ItemsSource is bound to the PagedSource property of a DataPager. The DataPager ItemsSource is bound to an EntityDataSource. The problem is that this is blocking my UI thread. The GridView has a "DataLoadMode" property which can be set to Asynchronous... but the pager doesn't. How would I go about loading the data to the pager asynchronously? I tried doing this in another thread but I keep getting a cross-thread exception even though I use the dispatcher to set the property to which the pager is bound.
<telerik:RadGridView x:Name="RadGridView1" Grid.Row="0" Margin="5,5,5,0" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" AlternateRowBackground="LightBlue" AlternationCount="2" AutoGenerateColumns="False" CanUserDeleteRows="False" CanUserFreezeColumns="False" CanUserInsertRows="False" DataLoadMode="Asynchronous" GroupRenderMode="Nested" IsReadOnly="True" ItemsSource="{Binding ElementName=DataPager1, Path=PagedSource}" RowIndicatorVisibility="Collapsed" ShowGroupPanel="False" telerik:StyleManager.Theme="Summer"> <telerik:RadGridView.Columns> <telerik:GridViewDataColumn Width="60" DataMemberBinding="{Binding StoreNumber}" Header="Store" ShowDistinctFilters="True" ShowFieldFilters="False" TextAlignment="Center" /> <telerik:GridViewDataColumn Width="80" DataFormatString="dd-MMM-yyyy" DataMemberBinding="{Binding ASNDate}" Header="Ship Date" ShowDistinctFilters="False" SortingState="Descending" TextAlignment="Center" /> <telerik:GridViewDataColumn Width="Auto" MinWidth="80" DataMemberBinding="{Binding PONumber}" Header="PO" ShowDistinctFilters="False" TextAlignment="Center" /> <telerik:GridViewDataColumn Width="80" DataFormatString="dd-MMM-yyyy" DataMemberBinding="{Binding PODate}" Header="PO Date" ShowDistinctFilters="False" TextAlignment="Center" /> <telerik:GridViewDataColumn Width="Auto" MinWidth="80" DataMemberBinding="{Binding InvoiceNumber}" Header="Invoice" ShowDistinctFilters="False" TextAlignment="Center" /> <telerik:GridViewDataColumn Width="Auto" DataMemberBinding="{Binding ToteID}" Header="Tote" ShowDistinctFilters="True" TextAlignment="Center"> <telerik:GridViewDataColumn.CellTemplate> <DataTemplate> <TextBlock Width="Auto" Height="Auto" HorizontalAlignment="Center" VerticalAlignment="Center"> <Hyperlink Click="OnToteClick" Tag="{Binding}"> <TextBlock Width="Auto" Height="Auto" HorizontalAlignment="Center" VerticalAlignment="Center" Text="{Binding ToteID}" /> </Hyperlink> </TextBlock> </DataTemplate> </telerik:GridViewDataColumn.CellTemplate> </telerik:GridViewDataColumn> <telerik:GridViewDataColumn Width="80" DataFormatString="dd-MMM-yyyy" DataMemberBinding="{Binding Closed}" Header="Completed" ShowDistinctFilters="False" /> <telerik:GridViewDataColumn Width="Auto" DataMemberBinding="{Binding ClosedByName}" Header="Complete By" ShowDistinctFilters="True" ShowFieldFilters="False" TextAlignment="Center" /> </telerik:RadGridView.Columns> </telerik:RadGridView> <telerik:RadDataPager x:Name="DataPager1" Grid.Row="1" Width="{Binding ActualWidth, ElementName=RadGridView1}" Margin="5,0,5,5" DisplayMode="FirstLastPreviousNextNumeric" PageSize="{Binding PageSize, Mode=TwoWay}" Source="{Binding ShipmentData}" telerik:StyleManager.Theme="Summer" />
public class ShipmentsViewModel : ViewModelBase { private readonly TIMS.Data.TIMSContext ctx =new Data.TIMSContext(); public ShipmentsViewModel() { } #region Properties private int PageSizeValue = 20; public int PageSize { get { return PageSizeValue; } set { SetPropertyValue((() => PageSize), ref PageSizeValue, value); } } private QueryableEntityCollectionView<TIMS.Data.Entities.ToteView> ShipmentDataValue; public QueryableEntityCollectionView<TIMS.Data.Entities.ToteView> ShipmentData { get { return ShipmentDataValue; } private set { SetPropertyValue((() => ShipmentData), ref ShipmentDataValue, value); } } #endregion #region Methods public override void OnNavigatedTo(Uri view, object data) { try { IsBusy = true; System.Threading.ThreadPool.QueueUserWorkItem(delegate { var shipments = new QueryableEntityCollectionView<Data.Entities.ToteView>(((IObjectContextAdapter)ctx).ObjectContext, "ToteViews"); if (App.Store != null) { shipments.FilterDescriptors.Add(new FilterDescriptor("StoreNumber", FilterOperator.IsEqualTo, App.Store.Value, false)); } App.Current.Dispatcher.Invoke(new Action(delegate { ShipmentDataValue = shipments; IsBusy = false; })); }); } catch (Exception e) { App.Current.Dispatcher.BeginInvoke(new Action(delegate { var dlg = new ChildWindows.ErrorDialog("0012: An unknown error occured."); App.LogError(e); dlg.ShowDialog(); IsBusy = false; })); } } #endregion #region Commands #endregion }