2013-12-12 74 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。你能幫我補充一點嗎?添加這個時,我得到編譯時錯誤。 –