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的更多相关文章

  1. WPF ListView 选中问题

    WPF ListView 选中问题  摘自:http://www.cnblogs.com/BBHor/archive/2013/04/28/VisualTreeHelper-PreviewMouseD ...

  2. [WPF]ListView点击列头排序功能实现

    [转]   [WPF]ListView点击列头排序功能实现 这是一个非常常见的功能,要求也很简单,在Column Header上显示一个小三角表示表示现在是在哪个Header上的正序还是倒序就可以了. ...

  3. WPF ListView点击删除某一行并获取绑定数据

    最近在开发WPF程序时遇到一个问题,在gridview中希望实现在每一行最后添加一个删除的按钮,但是发现点击每行的button时只会触发button的点击事件,并没有选中这一行,此时调用list.Se ...

  4. WP8.1学习系列(第二十七章)——ListView和GridView入门

    快速入门:添加 ListView 和 GridView 控件 (XAML)   在本文中 先决条件 选择 ListView 或 GridView 将项添加到项集合 设置项目源 指定项目的外观 指定视图 ...

  5. WPF ListView 居中显示

    原文:WPF ListView 居中显示 今天遇到的问题: 方法1:设置GridViewColumn的ActualWidth <ListView > <ListView.View&g ...

  6. 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 ...

  7. wpf listview

    <Window x:Class="WpfTutorialSamples.ListView_control.ListViewGridViewSample"        xml ...

  8. WPF:ListView 分组合并

    CollectionViewSource 绑定的是从数据库取出的数据ListBind 以DeptName为分组依据 <Window.Resources> <CollectionVie ...

  9. WPF:ListView 分页

    布局MainWindow.xaml <ListView Name="list_Reg" ItemsSource="{Binding Source={StaticRe ...

随机推荐

  1. 【LeetCode】390. 消除游戏

    题目 给定一个从1 到 n 排序的整数列表. 首先,从左到右,从第一个数字开始,每隔一个数字进行删除,直到列表的末尾. 第二步,在剩下的数字中,从右到左,从倒数第一个数字开始,每隔一个数字进行删除,直 ...

  2. python代码的那些设计

    一.Django的ORM 1.类QuerySet (django) :QuerySet 可以被构造,过滤,切片,做为参数传递,这些行为都不会对数据库进行操作.只要你查询的时候才真正的操作数据库. 2. ...

  3. linux中的tar命令的使用

    参考网址: https://www.cnblogs.com/newcaoguo/p/5896975.html https://www.cnblogs.com/xccjmpc/p/6035397.htm ...

  4. 【树】Construct Binary Tree from Inorder and Postorder Traversal

    题目: Given inorder and postorder traversal of a tree, construct the binary tree. 思路: 后序序列的最后一个元素就是树根, ...

  5. YAOLEI

    http://www.cnblogs.com/skyblue/p/3356933.html

  6. mongodb-地理坐标存储查询

    mongodb可支持空间地理搜索: 查询器 $geoWithin Selects geometries within a bounding GeoJSON geometry. The 2dsphere ...

  7. Linux的MySQL不能远程访问

    1.首先,你要确认用户是否只允许localhost访问: 在linux下登录mysql mysql -uroot -p密码;     use mysql;     select `host`,`use ...

  8. 常见的接口与类 -- Comparable

    目录 1. 接口概述 2. 接口方法详读 3. 接口方法的实践操作 3.1  String和Integer对于compareTo()的实现 正文 接口Comparable 我们在字符串中见到过Comp ...

  9. 自学C Primer Plus时还没想明白的问题

    2016年11月24日 1. 计算机中两个浮点数怎样进行加法运算.2. 为什么计算机在计算时存在损失精度的可能3. 无符号数和有符号数的运算是怎样的4. printf中使用格式控制符的截断问题,比如% ...

  10. mysql和mysql jdbc连接器mysql-connector-java对应关系

    mysql和mysql jdbc连接器mysql-connector-java对应关系,请参考下图:来源于mysql官网