WPF,ListView设置分组
今天遇到一个问题,就是在ListView中设置分组。想了很久在网上早了些资料作出一个例子。
分组字段也可以在后台中定义:
CollectionView view = (CollectionView)CollectionViewSource.GetDefaultView(listView1.ItemsSource);
PropertyGroupDescription groupDescription = new PropertyGroupDescription("Country");
view.GroupDescriptions.Add(groupDescription); //或者
var sourceView = new ListCollectionView(Trades);
var groupDesctripition = new PropertyGroupDescription("Country");
sourceView.GroupDescriptions.Add(groupDesctripition);
listView1.ItemsSource = sourceView;
XAML
<Window.Resources>
<CollectionViewSource x:Key="trades" Source="{Binding}">
<CollectionViewSource.GroupDescriptions>
<PropertyGroupDescription PropertyName="Country"/>
</CollectionViewSource.GroupDescriptions>
</CollectionViewSource> </Window.Resources>
<Grid>
<TextBox Height="23" HorizontalAlignment="Left" Margin="114,35,0,0" Name="textBox1" VerticalAlignment="Top" Width="211" />
<ListView Height="224" HorizontalAlignment="Left" Margin="68,81,0,0"
Name="listView1" VerticalAlignment="Top" Width="460" ItemsSource="{Binding Source={StaticResource trades}}">
<ListView.View>
<GridView>
<GridViewColumn DisplayMemberBinding="{Binding TradeType}" Header="TradeType" Width="100" />
<GridViewColumn DisplayMemberBinding="{Binding Units}" Header="Units" Width="100"/>
<GridViewColumn DisplayMemberBinding="{Binding USDAmount, StringFormat=C}" Header="USDAmount" Width="100"/>
</GridView>
</ListView.View>
<ListView.GroupStyle>
<GroupStyle>
<GroupStyle.ContainerStyle>
<Style TargetType="{x:Type GroupItem}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type GroupItem}">
<Expander IsExpanded="True" BorderBrush="#EA1730" BorderThickness="0,0,0,1">
<Expander.Header>
<DockPanel>
<TextBlock FontWeight="Bold" Text="{Binding Path=Name}" Margin="5,0,0,0" Width="150"/>
<TextBlock FontWeight="Bold" Text="{Binding Path=ItemCount}"/>
</DockPanel>
</Expander.Header>
<Expander.Content>
<Grid>
<Grid.RowDefinitions>
<RowDefinition></RowDefinition>
<RowDefinition></RowDefinition>
</Grid.RowDefinitions>
<ItemsPresenter Grid.Row="0"/>
</Grid>
</Expander.Content>
</Expander>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</GroupStyle.ContainerStyle>
</GroupStyle>
</ListView.GroupStyle>
</ListView>
</Grid>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
ObservableCollection<TradeTest> Trades = new ObservableCollection<TradeTest>();
Trades.Add(new TradeTest() { TradeType = "Subscription1", Units = , USDAmount = , Country = "UNITED STATES" });
Trades.Add(new TradeTest() { TradeType = "Subscription2", Units = , USDAmount = , Country = "UNITED STATES" });
Trades.Add(new TradeTest() { TradeType = "Subscription3", Units = , USDAmount = , Country = "UNITED STATES" }); Trades.Add(new TradeTest() { TradeType = "Subscription4", Units = , USDAmount = , Country = "UNITED KINGDOM" });
Trades.Add(new TradeTest() { TradeType = "Subscription5", Units = , USDAmount = , Country = "UNITED KINGDOM" });
Trades.Add(new TradeTest() { TradeType = "Subscription6", Units = , USDAmount = , Country = "UNITED KINGDOM" });
Trades.Add(new TradeTest() { TradeType = "Subscription7", Units = , USDAmount = , Country = "UNITED KINGDOM" });
Trades.Add(new TradeTest() { TradeType = "Subscription8", Units = , USDAmount = , Country = "UNITED KINGDOM" }); Trades.Add(new TradeTest() { TradeType = "Subscription9", Units = , USDAmount = , Country = "UNITED" });
Trades.Add(new TradeTest() { TradeType = "Subscription10", Units = , USDAmount = , Country = "UNITED" });
Trades.Add(new TradeTest() { TradeType = "Subscription11", Units = , USDAmount = , Country = "UNITED" });
Trades.Add(new TradeTest() { TradeType = "Subscription12", Units = , USDAmount = , Country = "UNITED" });
this.DataContext = Trades;
}
}
public class TradeTest
{
private string _tradetype;
private int _units;
private float _amount;
private string _country; public string TradeType
{
get { return _tradetype; }
set { _tradetype = value; }
}
public int Units
{
get { return _units; }
set { _units = value; }
}
public float USDAmount
{
get { return _amount; }
set { _amount = value; }
}
public string Country
{
get { return _country; }
set { _country = value; }
}
}
WPF,ListView设置分组的更多相关文章
- WPF ListView控件设置奇偶行背景色交替变换以及ListViewItem鼠标悬停动画
原文:WPF ListView控件设置奇偶行背景色交替变换以及ListViewItem鼠标悬停动画 利用WPF的ListView控件实现类似于Winform中DataGrid行背景色交替变换的效果,同 ...
- WPF ListView 居中显示
原文:WPF ListView 居中显示 今天遇到的问题: 方法1:设置GridViewColumn的ActualWidth <ListView > <ListView.View&g ...
- Android 开源库StickyListHeadersListView来实现ListView列表分组效果
项目中有一新的需求,要求能像一些Android机带"联系人列表"一样,数据可以自动分组,且在列表滑动过程中,列表头固定在顶部,效果图如下: 下面就带大家实现上面的效果, 首先,我们 ...
- 给ListView设置emptyView
给ListView设置emptyView 版权声明:本文为博主原创文章,未经博主允许不得转载. 使用ListView和GridView时,当列表为空时,默认是不显示任何内容的,这样对用户非常不友好,这 ...
- PB gird类型数据窗口 设置分组、分组小计、合计
今天遇到一个需求,gird表格数据如下: 部门 类型 数据 A 类型1 1 A 类型2 2 B 类型1 3 B 类型2 4 合计 10 实际需要显示的结果为: 部门 ...
- 在为ListView设置adapter时出错
为listView设置adapter,代码如下: SimpleAdapter simpleAdapter = new SimpleAdapter(this, listItems, R.layout.m ...
- WPF后台设置xaml控件的样式System.Windows.Style
WPF后台设置xaml控件的样式System.Windows.Style 摘-自 :感谢 作者: IT小兵 http://3w.suchso.com/projecteac-tual/wpf-zhi ...
- WPF ListView 选中问题
WPF ListView 选中问题 摘自:http://www.cnblogs.com/BBHor/archive/2013/04/28/VisualTreeHelper-PreviewMouseD ...
- 【转】三十三、Android给ListView设置分割线Divider样式
原文网址:http://www.cnblogs.com/linjiqin/archive/2011/11/12/2246349.html 给ListView设置分割线,只需设置如下两个属性: andr ...
随机推荐
- 贝塞尔曲线UIBezierPath简单使用
//常用属性 /* 1.CGPath: 将UIBezierPath类转换成CGPath 2.currentPoint: 当前path的位置,可以理解为path的终点 3.lineWidth: 线条宽度 ...
- (网页)Angular.js 中 copy 赋值与 = 赋值 区别
转自st.gg Angular.js 中 copy 赋值与 = 赋值 区别 为什么用 $scope.user = $scope.master; $scope.master 会跟着 $scope.use ...
- JavaScript大杂烩18 - Web开发的MVVM模式
MVC VS. MVP VS. MVVM 了解MVVM模式之前,我们先来简单了解一下从MVC到MVVM的变迁.这个变迁是耦合从紧到松的变迁,是对依赖处理的进化,是应对变化技术的成熟. MVC MV ...
- 对word2vec的理解及资料整理
对word2vec的理解及资料整理 无他,在网上看到好多对word2vec的介绍,当然也有写的比较认真的,但是自己学习过程中还是看了好多才明白,这里按照自己整理梳理一下资料,形成提纲以便学习. 介绍较 ...
- 10 种机器学习算法的要点(附 Python 和 R 代码)
本文由 伯乐在线 - Agatha 翻译,唐尤华 校稿.未经许可,禁止转载!英文出处:SUNIL RAY.欢迎加入翻译组. 前言 谷歌董事长施密特曾说过:虽然谷歌的无人驾驶汽车和机器人受到了许多媒体关 ...
- weblogic CVE-2018-2628漏洞利用工具
weblogic CVE-2018-2628漏洞利用 漏洞环境: Windows2018R2 weblogic10.3.6 漏洞利用过程: 搭建好存在CVE-2018-2628漏洞的weblogic平 ...
- [Demo_01] MapReduce 实现密码 Top10 统计
0. 说明 通过 MapReduce 实现密码 Top10 统计 通过两次 MapReduce 实现 1. 流程图 2. 程序编写 密码 Top10 统计代码
- Django电商项目---完成登录验证和用户中心(个人信息)day3
登录验证的实现 背景说明: 用户在商品界面选择商品后,在点击购物车或者结算订单之前 需要完成用户的登录验证,这里用装饰器来完成 创建装饰器类: df_user/user_decorator.py ...
- .NET Core launch.json 简介
1.环境 Windows,.NET Core 2.0,VS Code dotnet> dotnet new console -o myApp 2.launch.json配置文件 { // Use ...
- 解决eclipse启动慢
每次启动eclipse时都要等待半分钟左右,对于追求效率的程序员来说,等待每一秒都是一种折磨,出于高效开发的精神,这个问题必须解决掉.方案如下: ①在工具栏中的help中找到about eclipse ...