WPF ListView 使用GridView 带有Header 以及点击header排序 sort
ListView:
<ListView x:Name="lvFiles" VerticalAlignment="Stretch" Background="Transparent" Width="Auto" AllowDrop="{Binding IsAllowDrop}" Margin="20,0,20,30"
ScrollViewer.HorizontalScrollBarVisibility="Auto" ScrollViewer.VerticalScrollBarVisibility="Auto"
ButtonBase.Click="ButtonBaseClickedHandler" ItemContainerStyle="{StaticResource ListViewItemStyle}" ItemsSource="{Binding ContentItemsView}"
dnd:DragDropHelper.IsDragSource="True"
local:FileViewStyle.SortDefaultStyle="{StaticResource NormalSortHeaderTemplate}"
local:FileViewStyle.SortAscStyle="{StaticResource AscSortHeaderTemplate}"
local:FileViewStyle.SortDescStyle="{StaticResource DescSortHeaderTemplate}">
<ListView.View>
<GridView ColumnHeaderContainerStyle="{StaticResource GridViewColumnHeader}">
<GridViewColumn Header="{StaticResource IDS_NAME}" x:Name="nameColumn"
HeaderStringFormat="Name" HeaderTemplate="{StaticResource NormalSortHeaderTemplate}"
Width="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=ListView}, Converter={StaticResource AutoListViewColumnWidthConverter}}">
<GridViewColumn.CellTemplate>
<DataTemplate >
<DockPanel>
<Image x:Name="img" Width="20" Height="20" Source="{Binding Icon,Converter={StaticResource ImgPathToImageConverter}}"
SnapsToDevicePixels="True" UseLayoutRounding="True"
RenderTransformOrigin="0.5,0.5" Margin="0,0,5,0"></Image>
<customControl:EnhancedTextBlock Text="{Binding Name}" VerticalAlignment="Center" TextTrimming="CharacterEllipsis" />
</DockPanel>
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>
<GridViewColumn HeaderStringFormat="Type" HeaderTemplate="{StaticResource NormalSortHeaderTemplate}"
Width="80" Header="{StaticResource IDS_TYPE}" local:RangeColumn.MinWidth="80">
<GridViewColumn.CellTemplate>
<DataTemplate >
<customControl:EnhancedTextBlock Text="{Binding Type}" VerticalAlignment="Center" TextTrimming="CharacterEllipsis" />
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>
<GridViewColumn HeaderStringFormat="Size" HeaderTemplate="{StaticResource NormalSortHeaderTemplate}"
Width="80" Header="{StaticResource IDS_SIZE}" local:RangeColumn.MinWidth="80">
<GridViewColumn.CellTemplate>
<DataTemplate >
<customControl:EnhancedTextBlock Text="{Binding SizeStr}" VerticalAlignment="Center" TextTrimming="CharacterEllipsis" />
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>
<GridViewColumn HeaderStringFormat="Modified" HeaderTemplate="{StaticResource NormalSortHeaderTemplate}"
Width="120" Header="{StaticResource IDS_MODIFIED}" local:RangeColumn.MinWidth="120">
<GridViewColumn.CellTemplate>
<DataTemplate >
<customControl:EnhancedTextBlock Text="{Binding LastModifyTimeString}" VerticalAlignment="Center" TextTrimming="CharacterEllipsis" />
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>
</GridView>
</ListView.View>
</ListView>
ListViewItemStyle:
<Style x:Key="ListViewItemStyle" TargetType="{x:Type ListViewItem}" BasedOn="{StaticResource {x:Type ListViewItem}}">
<Setter Property="ContextMenu" Value="{StaticResource ItemContextMenu}" />
<Setter Property="IsSelected" Value="{Binding IsSelected}"/>
<Setter Property="Height" Value="22" />
<EventSetter Event="RequestBringIntoView" Handler="ListViewItem_RequestBringIntoView"></EventSetter>
<!--<Setter Property="helper:DragSelectionHelper.IsDragSelecting" Value="False"/>-->
<!--<Style.Triggers>
<Trigger Property="ListBoxItem.IsMouseOver" Value="True">
<Setter Property="helper:DragSelectionHelper.IsDragSelecting" Value="True" />
</Trigger>
</Style.Triggers>-->
</Style>
NormalSortHeaderTemplate:
<DataTemplate x:Key="NormalSortHeaderTemplate">
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*" />
<ColumnDefinition Width="Auto" />
</Grid.ColumnDefinitions>
<customControl:EnhancedTextBlock Grid.Column="0" Text="{Binding}"
VerticalAlignment="{TemplateBinding VerticalAlignment}" HorizontalAlignment="Left"
FontWeight="{TemplateBinding TextBlock.FontWeight}" Foreground="{TemplateBinding TextBlock.Foreground}"/>
</Grid>
</DataTemplate>
AscSortHeaderTemplate:
<DataTemplate x:Key="AscSortHeaderTemplate">
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*" />
<ColumnDefinition Width="Auto" />
</Grid.ColumnDefinitions>
<customControl:EnhancedTextBlock Grid.Column="0" Text="{Binding}"
VerticalAlignment="{TemplateBinding VerticalAlignment}"
FontWeight="{TemplateBinding TextBlock.FontWeight}" Foreground="{TemplateBinding TextBlock.Foreground}" />
<Path Grid.Column="1" x:Name="arrow" Margin="0,-6,6,0" VerticalAlignment="{TemplateBinding VerticalAlignment}"
StrokeThickness="1" Fill="#FF4C4A4A" Data="M 5,10 L 15,10 L 10,5 L 5,10" />
</Grid>
</DataTemplate>
DescSortHeaderTemplate:
<DataTemplate x:Key="DescSortHeaderTemplate">
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*" />
<ColumnDefinition Width="Auto" />
</Grid.ColumnDefinitions>
<customControl:EnhancedTextBlock Grid.Column="0" Text="{Binding}" VerticalAlignment="{TemplateBinding VerticalAlignment}"
FontWeight="{TemplateBinding TextBlock.FontWeight}" Foreground="{TemplateBinding TextBlock.Foreground}" />
<Path Grid.Column="1" x:Name="arrow" Margin="0,-6,6,0" VerticalAlignment="{TemplateBinding VerticalAlignment}"
StrokeThickness="1" Fill="#FF4C4A4A" Data="M 5,5 L 10,10 L 15,5 L 5,5" />
</Grid>
</DataTemplate>
GridViewColumnHeader:
<Style x:Key="GridViewColumnHeader" TargetType="{x:Type GridViewColumnHeader}">
<Setter Property="HorizontalContentAlignment" Value="Stretch"/>
<Setter Property="VerticalContentAlignment" Value="Center"/>
<Setter Property="VerticalAlignment" Value="Center"/>
<Setter Property="Foreground" Value="#646464"/>
<Setter Property="Background" Value="#f2f2f2" />
<Setter Property="Height" Value="22" />
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="GridViewColumnHeader">
<Grid Background="{TemplateBinding Background}">
<Border Name="HeaderBorder">
<ContentPresenter x:Name="HeaderContent" Content="{TemplateBinding Content}" ContentStringFormat="{TemplateBinding ContentStringFormat}"
VerticalAlignment="Center"
Margin="10,0,0,0" RecognizesAccessKey="True"
SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}"
ContentTemplate="{TemplateBinding ContentTemplate}">
</ContentPresenter>
</Border>
<Thumb x:Name="PART_HeaderGripper" HorizontalAlignment="Right" Style="{StaticResource GridViewColumnHeaderGripper}"/>
</Grid>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
ButtonBaseClickedHandler:
#region sort
/// <summary>
/// last sort header
/// </summary>
GridViewColumnHeader _lastHeaderClicked = null; /// <summary>
/// last sort direction
/// </summary>
ListSortDirection _lastDirection = ListSortDirection.Ascending; void ButtonBaseClickedHandler(object sender, RoutedEventArgs e)
{
#region sort
try
{
GridViewColumnHeader headerClicked =
e.OriginalSource as GridViewColumnHeader; ListSortDirection direction; if (headerClicked != null)
{
if (headerClicked.Column.HeaderStringFormat == null) return;
string header = headerClicked.Column.HeaderStringFormat as string;
if (headerClicked.Role != GridViewColumnHeaderRole.Padding)
{
if (headerClicked != _lastHeaderClicked)
{
direction = ListSortDirection.Descending;
}
else
{
if (_lastDirection == ListSortDirection.Ascending)
{
direction = ListSortDirection.Descending;
}
else
{
direction = ListSortDirection.Ascending;
}
} Sort(header, direction); if (direction == ListSortDirection.Ascending)
{
SetSortStyle(headerClicked.Column, true);
}
else
{
SetSortStyle(headerClicked.Column, false);
} // Remove arrow from previously sorted header
if (_lastHeaderClicked != null && _lastHeaderClicked != headerClicked)
{
//_lastHeaderClicked.Column.HeaderTemplate = null;
_lastHeaderClicked.Column.HeaderTemplate = FileViewStyle.GetSortDefaultStyle(lvFiles);
} _lastHeaderClicked = headerClicked;
_lastDirection = direction;
}
}
}
catch (Exception ex)
{
Debug.WriteLine(ex);
}
#endregion } void SetSortStyle(GridViewColumn column, bool isAsc)
{
DataTemplate dataTemplate = null; if (isAsc)
{
dataTemplate = FileViewStyle.GetSortAscStyle(lvFiles);
}
else
{
dataTemplate = FileViewStyle.GetSortDescStyle(lvFiles);
} if (dataTemplate != null)
{
column.HeaderTemplate = dataTemplate;
}
} void Sort(string sortBy, ListSortDirection direction)
{
if (string.IsNullOrEmpty(sortBy)) return; ICollectionView dataView =
CollectionViewSource.GetDefaultView(lvFiles.ItemsSource); dataView.SortDescriptions.Clear();
SortDescription sortByType = new SortDescription("IsFolder", direction);
dataView.SortDescriptions.Add(sortByType);
SortDescription sortByProperty = new SortDescription(sortBy, direction);
dataView.SortDescriptions.Add(sortByProperty);
dataView.Refresh();
}
#endregion
附上其他细节:
public FileManageListView()
{
InitializeComponent();
this.lvFiles.MouseDoubleClick += FilesView_MouseDoubleClick;
lvFiles.KeyDown += LvFiles_KeyDown;
lvFiles.SelectionChanged += LvFiles_SelectionChanged;
} private void LvFiles_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
foreach (var removedItem in e.RemovedItems.Cast<FileItemViewModel>())
{
removedItem.IsSelected = false;
}
foreach (var addedItem in e.AddedItems.Cast<FileItemViewModel>())
{
addedItem.IsSelected = true;
}
FileManagerViewModel.Instance.RaiseCanExecuteChanged();
}
private void Grid_DragOver(object sender, DragEventArgs e)
{
try
{
var itemViewModel = (sender as Grid).DataContext as FileItemViewModel;
FileManagerViewModel.Instance.DragOverItem = itemViewModel;
}
catch (Exception ex)
{
Debug.WriteLine(ex);
}
} private void LvFiles_KeyDown(object sender, KeyEventArgs e)
{
if (e.Key == Key.Delete)
{
var deleteCommand = FileManagerViewModel.Instance.DeleteCommand as ICommand;
if (deleteCommand.CanExecute(null))
{
deleteCommand.Execute(null);
} }
} private void ListViewItem_RequestBringIntoView(object sender, RequestBringIntoViewEventArgs e)
{
e.Handled = true;
}
WPF ListView 使用GridView 带有Header 以及点击header排序 sort的更多相关文章
- WPF ListView 选中问题
WPF ListView 选中问题 摘自:http://www.cnblogs.com/BBHor/archive/2013/04/28/VisualTreeHelper-PreviewMouseD ...
- [WPF]ListView点击列头排序功能实现
[转] [WPF]ListView点击列头排序功能实现 这是一个非常常见的功能,要求也很简单,在Column Header上显示一个小三角表示表示现在是在哪个Header上的正序还是倒序就可以了. ...
- WPF ListView点击删除某一行并获取绑定数据
最近在开发WPF程序时遇到一个问题,在gridview中希望实现在每一行最后添加一个删除的按钮,但是发现点击每行的button时只会触发button的点击事件,并没有选中这一行,此时调用list.Se ...
- WP8.1学习系列(第二十七章)——ListView和GridView入门
快速入门:添加 ListView 和 GridView 控件 (XAML) 在本文中 先决条件 选择 ListView 或 GridView 将项添加到项集合 设置项目源 指定项目的外观 指定视图 ...
- WPF ListView 居中显示
原文:WPF ListView 居中显示 今天遇到的问题: 方法1:设置GridViewColumn的ActualWidth <ListView > <ListView.View&g ...
- WPF listview item mouse enter/over popup
This is because the routing strategy of the Loaded event is Direct, which means that the routed even ...
- wpf listview
<Window x:Class="WpfTutorialSamples.ListView_control.ListViewGridViewSample" xml ...
- WPF:ListView 分组合并
CollectionViewSource 绑定的是从数据库取出的数据ListBind 以DeptName为分组依据 <Window.Resources> <CollectionVie ...
- WPF:ListView 分页
布局MainWindow.xaml <ListView Name="list_Reg" ItemsSource="{Binding Source={StaticRe ...
随机推荐
- [题解]luogu P4116 Qtree3
终于来到了Qtree3, 其实这是Qtree系列中最简单的一道题,并不需要线段树, 只要树链剖分的一点思想就吼了. 对于树链剖分剖出来的每一根重链,在重链上维护一个Set就好了, 每一个Set里存的都 ...
- (转)Linux 文件目录特殊权限设定(SUID,SGID,SBIT)
原文:https://blog.csdn.net/leshami/article/details/77184029 Linux文件及目录的权限设定,除了我们孰知的读写执行(rwx)之外,还有一些特殊的 ...
- java数据结构之队列
队列概述队列是一种特殊的线性表,它只允许在表的前端(front)进行删除操作,而在表的后端(rear)进行插入操作.进行插入操作的端称为队尾,进行删除操作的端称为队头.队列中没有元素时,称为空队列.– ...
- spring boot中使用JdbcTemplate
本文将介绍如何将spring boot 与 JdbcTemplate一起工作.Spring对数据库的操作在jdbc上面做了深层次的封装,使用spring的注入功能,可以把DataSource注册到Jd ...
- 利用System.IO.Compression操作压缩文件
引用: using System.IO.Compression; using (FileStream zipToOpen = new FileStream(@"D:\json.zip&quo ...
- 【数组】Missing Number
题目: Given an array containing n distinct numbers taken from 0, 1, 2, ..., n, find the one that is mi ...
- CentOS6.5下Ambari安装搭建部署大数据集群(图文分五大步详解)(博主强烈推荐)
第一步: Ambari安装之Ambari安装前准备(CentOS6.5)(一) 第二步: Ambari安装之部署本地库(镜像服务器)(二) 第三步: Ambari安装之安装并配置Ambari-serv ...
- WordPress导航菜单函数
导航菜单是每一个WordPress主题必须的元素,如果你要制作一个WordPress主题,那就必须熟悉WordPress导航菜单注册函数 register_nav_menus() 和 导航菜单调用函数 ...
- java主线程结束和子线程结束之间的关系
(一)Main线程是个非守护线程,不能设置成守护线程. 这是因为,main线程是由java虚拟机在启动的时候创建的.main方法开始执行的时候,主线程已经创建好并在运行了.对于运行中的线程,调用Thr ...
- 02-python基本数据类型
python的变量不需要声明, 但变量使用前必须复制, 因为python中所有的内容全部是对象 变量是没有类型的, 有类型的是指向内存对象的类型 a = ' a = 是合法的 此外, python还可 ...