簡體   English   中英

與DataPager綁定時,排序不適用於DataGrid

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

我對DataGrid的排序功能有問題,其中分頁由DataPager完成。 我的DataPager控件根據為每個頁面定義的行數將數據分頁為多個頁面。

這是我的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>

為了向您簡要介紹上述代碼:我有一個DataGrid ,其中將ItemSource綁定到“ ReportsPager”並注冊了排序事件。 在下一節中,我有一個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;
        }

任何人都可以在我犯錯的地方幫助我。 通過調試,在驗證的最后一行,我可以看到排序后的數據,但是當它發布時,它並不能正確顯示數據。 我已經用谷歌搜索了這個問題,但是我沒有足夠的信息來解決我的問題。 提前致謝。

親愛的,西瓦。

您可以使用CollectionViewSource,如下所示:

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

然后像這樣綁定您的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