简体   繁体   English

与DataPager绑定时,排序不适用于DataGrid

[英]Sorting doesn't work for DataGrid when it's binded with a DataPager

I have a problem with the sorting feature in a DataGrid where the paging is done by a DataPager . 我对DataGrid的排序功能有问题,其中分页由DataPager完成。 My DataPager control pages the data into mulitple pages as per number of rows defined for each page. 我的DataPager控件根据为每个页面定义的行数将数据分页为多个页面。

Here is my XAML code: 这是我的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>

To brief you about the above code: I have a DataGrid where I bind the ItemSource to the "ReportsPager" and registered the sorting event as well. 为了向您简要介绍上述代码:我有一个DataGrid ,其中将ItemSource绑定到“ ReportsPager”并注册了排序事件。 In the next section I have a DataPager - "ReportsPager" - and the ItemSource is binded to "lstReprotView". 在下一节中,我有一个DataPager “ ReportsPager”-并将ItemSource绑定到“ lstReprotView”。 My intention here is to prepare the lstReportveiw and the DataPager looks after the publishing the data into the DataGrid . 我的目的是准备lstReportveiw ,然后DataPager负责将数据发布到DataGrid Then when I click on one of the header columns it will be sorted automatically. 然后,当我单击标题列之一时,它将自动进行排序。

Unfortunately that does not work for me. 不幸的是,这对我不起作用。 So in the GridView.Sorting event I am explicitly sorting the "lstReprotView", but still no result. 因此,在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;
        }

Can any one please help me where I am making a mistake. 任何人都可以在我犯错的地方帮助我。 With debugging, the last line while validating, I could see the sorted data, but when it's published, it's not showing the data properly. 通过调试,在验证的最后一行,我可以看到排序后的数据,但是当它发布时,它并不能正确显示数据。 I have googled for this problem, but I have not got enough information to resolve my issue. 我已经用谷歌搜索了这个问题,但是我没有足够的信息来解决我的问题。 Thanks in Advance. 提前致谢。

Regards, Siva. 亲爱的,西瓦。

You could use a CollectionViewSource, like this: 您可以使用CollectionViewSource,如下所示:

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

And then bind your DataGrid (or Pager I'm guessing, not having used one) like this: 然后像这样绑定您的DataGrid(或者我猜是没有使用过的Pager):

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM