我的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」。我的意圖是準備lstReportveiw
和DataPager
看起來將數據發佈到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。
什麼類型的都有排序前的'lstReportView'? – Herdo
其清單。這裏是that.foreach的代碼(var obj in lstReportView){ReportData.Add(obj); } ReportsPager.ItemsSource = ReportData; –
你使用MVVM模式嗎?如果是這樣,則不需要手動設置「ReportsPager.ItemSource」。 – Herdo