对页面进行排序WPF ListView将只对当前页面上的项进行排序

本文关键字:排序 当前页 WPF ListView | 更新日期: 2023-09-27 18:05:10

我有一个带有页面的listView,我尝试在列标题单击上对其进行排序,但问题是它只会对当前页面上的项目进行排序。我希望排序器对所有项目进行排序,并以正确的顺序在页面上显示项目。例如:现在,如果我在第1页上有5,3,1,在第2页上有2,6,4,它将在第1页上显示1,3,5,在第2页上显示2,4,6,并且需要在第1页上显示1,2,3,在第2页上显示4,5,6。

我怎么做?下面是我使用的代码:

CollectionViewSource view = new CollectionViewSource();
ObservableCollection<Server> Source = new ObservableCollection<Server>();
int currentPageIndex = 0;
int itemPerPage = 13;
int totalPage = 0;
//populating the listview
public void refreshList()
{
    int itemcount = 0;
    foreach (string id in server_id)
    {
        if (id != null && id != "")
        {
            Source.Add(new Server
            {
                ID = Int32.Parse(id),
                NAME = server_name[Int32.Parse(id)],
                PING = 0
            }); 
            itemcount++;
        }
    }
    totalPage = itemcount / itemPerPage;
    if (itemcount % itemPerPage != 0)
    {
        totalPage += 1;
    }
    view.Source = Source;
    view.Filter += new FilterEventHandler(view_Filter);
    listView1.DataContext = view;
} 

//Paging
private void ShowCurrentPageIndex()
{
    this.next.Content = (currentPageIndex + 1).ToString();
}
void view_Filter(object sender, FilterEventArgs e)
{
    int index = Source.IndexOf((Server)e.Item);
    if (index >= itemPerPage * currentPageIndex && index < itemPerPage * (currentPageIndex     + 1))
    {
        e.Accepted = true;
    }
    else
    {
        e.Accepted = false;
    }
}
private void btnPrev_Click(object sender, RoutedEventArgs e)
{
// Display previous page
    if (currentPageIndex > 0)
    {
        currentPageIndex--;
        view.View.Refresh();
    }
    ShowCurrentPageIndex();
}
private void btnNext_Click(object sender, RoutedEventArgs e)
{
// Display next page
    if (currentPageIndex < totalPage - 1)
    {
        currentPageIndex++;
        view.View.Refresh();
    }
    ShowCurrentPageIndex();
}
//Sorting
GridViewColumnHeader _lastHeaderClicked = null;
ListSortDirection _lastDirection = ListSortDirection.Ascending;
void GridViewColumnHeaderClickedHandler(object sender, RoutedEventArgs e)
{
    GridViewColumnHeader headerClicked = e.OriginalSource as GridViewColumnHeader;
    ListSortDirection direction;
    if (headerClicked != null)
    {
        if (headerClicked != _lastHeaderClicked)
        {
            direction = ListSortDirection.Ascending;
        }
        else
        {
            if (_lastDirection == ListSortDirection.Ascending)
            {
                direction = ListSortDirection.Descending;
            }
            else
            {
                direction = ListSortDirection.Ascending;
            }
        }
        string header = headerClicked.Column.Header as string;
        Sort(header, direction);
        _lastHeaderClicked = headerClicked;
        _lastDirection = direction;
    }
}
private void Sort(string sortBy, ListSortDirection direction)
    {
        var sortProperty = typeof(Server).GetProperty(sortBy);
        if (direction == ListSortDirection.Ascending)
        {
            Source = new ObservableCollection<Server>(Source.OrderBy(s => sortProperty.GetValue(s)));
        }
        else
        {
            Source = new ObservableCollection<Server>(Source.OrderByDescending(s => sortProperty.GetValue(s)));
        }
        view.Source = Source;
        view.View.Refresh();
    }

ListView XAML:

    <ListView Name="listView1" ItemsSource="{Binding}"  ItemContainerStyle="{StaticResource LVitem}" BorderBrush="{x:Null}" Height="502" Margin="10,130,10,0" FontSize="20"  GridViewColumnHeader.Click="GridViewColumnHeaderClickedHandler" FontFamily="/WpfApplication2;component/Resources/#Purista SemiBold" Background="#66181F2C" BorderThickness="1.000001"  ScrollViewer.HorizontalScrollBarVisibility="Hidden" ScrollViewer.VerticalScrollBarVisibility="Hidden" >
            <ListView.View>
                <GridView ColumnHeaderContainerStyle="{StaticResource ColumnHeader}"  >
                    <GridViewColumn Header=" ID" Width="0" DisplayMemberBinding="{Binding ID}" />
                    <GridViewColumn Header=" NAME" Width="500"  DisplayMemberBinding="{Binding NAME}"  />
                    <GridViewColumn Header=" PING" Width="100" DisplayMemberBinding="{Binding PING}"  />
                </GridView>
            </ListView.View>
        </ListView>
        <Button x:Name="next" Content="Next" Margin="1179,0,0,0" Click="btnNext_Click"/>
        <Button x:Name="prev" Content="Prev" Margin="1179,0,0,0" Click="btnPrev_Click"/>

,这是listview的样式:

 <Style x:Key="LVitem" TargetType="{x:Type ListViewItem}">
        <Setter Property="Background">
            <Setter.Value>
                <LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0">
                    <GradientStop Color="#663F5768" Offset="0"/>
                    <GradientStop Color="#992E4051" Offset="1"/>
                </LinearGradientBrush>
            </Setter.Value>
        </Setter>
        <Setter Property="Foreground" Value="White"/>
        <Setter Property="FontSize" Value="20"/>
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type ListViewItem}">
                    <Grid SnapsToDevicePixels="true">
                        <Border x:Name="Border" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="0,1,0,1" Background="{TemplateBinding Background}">
                            <Grid>
                                <Grid.RowDefinitions>
                                    <RowDefinition MaxHeight="7"/>
                                    <RowDefinition/>
                                </Grid.RowDefinitions>
                                <Rectangle x:Name="UpperHighlight" Fill="#FFE3F7FF" Visibility="Collapsed"/>
                                <Border Padding="{TemplateBinding Padding}" Grid.RowSpan="2">
                                    <GridViewRowPresenter  x:Name="Content" HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" Margin="0,0,0,1"  SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" VerticalAlignment="{TemplateBinding VerticalContentAlignment}"/>
                                </Border>
                            </Grid>
                        </Border>
                    </Grid>
                    <ControlTemplate.Triggers>
                        <Trigger Property="IsMouseOver" Value="true">
                            <Setter Property="Background">
                                <Setter.Value>
                                    <LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0">
                                        <GradientStop Color="#66597B93" Offset="0"/>
                                        <GradientStop Color="#99364B5F" Offset="1"/>
                                    </LinearGradientBrush>
                                </Setter.Value>
                            </Setter>
                            <Setter Property="BorderBrush" Value="{x:Null}"/>
                        </Trigger>
                        <Trigger Property="IsSelected" Value="true">
                            <Setter Property="Background">
                                <Setter.Value>
                                    <LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0">
                                        <GradientStop Color="#FFDADADA" Offset="1"/>
                                        <GradientStop Color="White" Offset="0"/>
                                    </LinearGradientBrush>
                                </Setter.Value>
                            </Setter>
                            <Setter Property="Foreground" Value="Black"/>
                            <Setter Property="BorderBrush" Value="{x:Null}"/>
                        </Trigger>
                    </ControlTemplate.Triggers>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>

对页面进行排序WPF ListView将只对当前页面上的项进行排序

由于您筛选项目的方式,您必须对Source集合进行排序,而不是对项目进行排序。

private void Sort(string sortBy, ListSortDirection direction)
{
    var sortProperty = typeof(Server).GetProperty(sortBy);
    if(sortProperty == null) return;
    if (direction == ListSortDirection.Ascending)
    {
        Source = new ObservableCollection<Server>(Source.OrderBy(s => sortProperty.GetValue(s, null)));
    }
    else
    {
        Source = new ObservableCollection<Server>(Source.OrderByDescending(s => sortProperty.GetValue(s, null)));
    }
    view.Source = Source;
    view.View.Refresh();
}