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 ...
随机推荐
- Salesforce Live Agent 简介
Live Agent Salesforce 内置了即时聊天功能--Live Agent,可以用来作为客服工具. 本文简单介绍了使用 Live Agent 的步骤,主要包含: 启用 Live Agent ...
- 最新安全狗 apache v4.0 sql注入 bypass
前言 最近没事学习一下 waf 的 bypass , 本文介绍下 bypass 安全狗的笔记.个人感觉 bypass 的总思路(正则匹配型 waf)就是利用各种语法特性来逃避正则(当然要保证语法正确性 ...
- Java并发编程(十一)线程池的使用
1.new Thread的弊端如下: a. 每次new Thread新建对象性能差. b. 线程缺乏统一管理,可能无限制新建线程,相互之间竞争,及可能占用过多系统资源导致死机或oom. c. 缺乏更多 ...
- JavaScript大杂烩15 - 使用JQuery(下)
前面我们总结了使用各种selector拿到了jQuery对象了,下面就是对这个对象执行指定的行为了. 2. 操作对象 - 行为函数action 执行jQuery内置的行为函数的时候,JQuery自动遍 ...
- EF 更新部分字段写法
EF 更新部分字段写法 1.EF默认是查询出来,修改后保存: 2.设置不修改字段的IsModified为false,此方法不需要先从数据库查询出实体来(最优方法): db.Set<T>() ...
- sysbench对MySQL的压测,使用sysbench压测磁盘io
QPS - query per secondTPS - transaction per second 不是特别关注,每个业务场景中事务标准是不一样的 Ⅰ.sysbench测试框架 Ⅱ.常用测试脚本 [ ...
- Centos 下添加开机自启动服务和脚本
最近刚玩Centos7的系统,跟Centos6还是很多方面有改变的,这里记录一下怎么在Centos7下添加开机自启动脚本和服务的方法. 1.添加开机自启服务 我这里以docker 服务为例,设置如下两 ...
- centos7执行umount提示:device is busy或者target is busy解决方法
问题描述: 因为挂载错了,想取消挂载,但是umount报告如下错误: [root@zabbix /]# umount /dev/sdc1 umount: /data1: target is busy. ...
- kettle 合并记录步骤中的 关键字段和 比较字段的说明
该步骤用于将两个不同来源的数据合并,这两个来源的数据分别为旧数据和新数据,该步骤将旧数据和新数据按照指定的关键字匹配.比较.合并. 需要设置的参数: 旧数据来源:旧数据来源的步骤 新数据来源.新数据来 ...
- vue 去除前后空格trim
一.使用trim修饰符 <input v-model.trim = "massage" > 二.使用filter过去属性 html: <ul id="l ...