2013-12-12 71 views
0

我的DataGrid中的排序功能出现问题,其中的分页由DataPager完成。我的DataPager根据为每个页面定义的行数控制将数据分页成多个页面。使用DataPager绑定DataGrid时,DataGrid排序不起作用

这里是我的XAML代码:

 <UserControl x:Class="XXXXX.ViewData" 
      xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
      xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
      xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
      xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
      mc:Ignorable="d" xmlns:dtContr="http://schemas.microsoft.com/wpf/2008/toolkit" 
      xmlns:xtndrCntrl="http://schemas.microsoft.com/winfx/2006/xaml/presentation/toolkit/extended" 
      xmlns:localControls="clr-namespace:XXXXXX.Controls" 
      d:DesignHeight="550" d:DesignWidth="750" Loaded="ViewData_Loaded"> 
    <Grid Background="#FAF9F9"> 
     <Grid.RowDefinitions> 
      <RowDefinition Height="35"/> 
      <RowDefinition Height="*"/> 
      <RowDefinition Height="65"/> 
     </Grid.RowDefinitions> 
     <StackPanel Height="35" HorizontalAlignment="Left" Name="stackPanel1" VerticalAlignment="Top" Width="Auto" Grid.Row="0" Orientation="Horizontal" Margin="10,0"> 

     </StackPanel> 
     <StackPanel Orientation="Vertical" Grid.Row="1"> 
      <dtContr:DataGrid AutoGenerateColumns="False" HorizontalAlignment="Left" Height="340" Width="750" 
          Margin="10,10,10,0" Name="grvViewData" VerticalAlignment="Top" IsReadOnly="True" 
          CanUserResizeColumns="True" 
          ItemsSource="{Binding ElementName=ReportsPager,Path=CurrentPage}"> 

       <dtContr:DataGrid.Columns> 
        <localControls:QDDataGridColumn Header="Date" HeaderResourceID="lblDate" Width="100" Binding="{Binding Path=Date_Time}"> 
        </localControls:QDDataGridColumn> 
        <localControls:QDDataGridColumn Header="Name" HeaderResourceID="lblName" Width="75" Binding="{Binding Path=Name}"> 
        </localControls:QDDataGridColumn> 
       </dtContr:DataGrid.Columns> 
      </dtContr:DataGrid> 

      <localControls:DataPager x:Name="ReportsPager" ItemsPerPage="10" Margin="0,10,10,10" 
               HorizontalAlignment="Right" 
               ItemsSource="{Binding Source={StaticResource ReportCollection}}" /> 


     </StackPanel> 
     <Button Content="Export To Excel" Grid.Row="3" Height="35" HorizontalAlignment="Right" Margin="10,15" Name="btnExportToExcel" VerticalAlignment="Top" Width="175" Click="btnExportToExcel_Click" /> 
     <Button Content="Create Report" Grid.Row="3" Height="35" HorizontalAlignment="Right" Margin="10,15" Name="btnCreateReport" VerticalAlignment="Top" Width="175" Visibility="Collapsed" Click="btnCreateReport_Click"/> 
    </Grid> 

</UserControl> 

简要介绍一下上面的代码:我有,我绑定ItemSource到“ReportsPager”并注册了事件排序,以及一个DataGrid。在下一节中,我有一个DataPager - “ReportsPager” - 和ItemSource绑定到“lstReprotView”。我的意图是准备lstReportveiwDataPager看起来将数据发布到DataGrid后。然后,当我点击其中一个标题栏时,它会自动排序。

不幸的是,这并不适合我。所以在GridView.Sorting事件中,我明确地排序了“lstReprotView”,但仍然没有结果。

void grvViewData_Sorting(object sender,Microsoft.Windows.Controls.DataGridSortingEventArgs e) 
     { 
      //Assume i sorted the lstReportView data explicitly prior to this line of code. 
      ReportsPager.ItemsSource = lstReportView; 

      //Validation puropose 
      ObservableCollection<Object> lstReportViews = (ObservableCollection<Object>)ReportsPager.ItemsSource; 
     } 

任何人都可以帮助我,我犯了一个错误。通过调试,验证时的最后一行,我可以看到已排序的数据,但是当它发布时,它不会正确显示数据。我已经搜索了这个问题,但我没有足够的信息来解决我的问题。提前致谢。

Regards, Siva。

+0

什么类型的都有排序前的'lstReportView'? – Herdo

+0

其清单。这里是that.foreach的代码(var obj in lstReportView){ReportData.Add(obj); } ReportsPager.ItemsSource = ReportData; –

+0

你使用MVVM模式吗?如果是这样,则不需要手动设置“ReportsPager.ItemSource”。 – Herdo

回答

0

你可以使用一个CollectionViewSource,这样的:

<CollectionViewSource Source="{Binding lstReportView}" x:Key="ReportCollection" > 
    <CollectionViewSource.SortDescriptions> 
     <compMod:SortDescription PropertyName="PropertyIWantToSortOn" Direction="Ascending" /> 
    </CollectionViewSource.SortDescriptions> 
</CollectionViewSource> 

,然后绑定你的DataGrid(或寻呼机我猜,没有使用)是这样的:

<sdk:DataGrid ItemsSource="{Binding Source={StaticResource ReportCollection}}" SelectedItem="{Binding SelectedReport, Mode=TwoWay}" etc etc> 
+0

嗨Mashton,感谢您建议我使用CollectionViewSource。这就是我需要遵循的排序方法。但在我的XAML中,我无法在Datapager控件下添加集合viewsource。我在我的文章中添加了我的XAML。你能帮我补充一点吗?添加这个时,我得到编译时错误。 –