我有一个带有页面的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>
答案 0 :(得分:0)
由于您过滤项目的方式,您必须对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();
}