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 ...
随机推荐
- Linux环境下运行简单java程序
一.安装java 1.下载jdk8 登录网址:http://www.oracle.com/technetwork/java/javase/downloads/jdk8-downloads-213315 ...
- Appium学习——安装appium Server
安装appium Server 下载地址:百度网盘的下载链接:https://pan.baidu.com/s/1pKMwdfX 下载后, AppiumForWindows.zip 进行解压,点击 ap ...
- C# 异步编程3 TPL Task 异步程序开发
.Net在Framework4.0中增加了任务并行库,对开发人员来说利用多核多线程CPU环境变得更加简单,TPL正符合我们本系列的技术需求.因TPL涉及内容较多,且本系列文章为异步程序开发,所以本文并 ...
- 将 Windows 虚拟机从非托管磁盘转换为托管磁盘
如果有使用非托管磁盘的现有 Windows 虚拟机 (VM),可通过 Azure 托管磁盘服务将 VM 转换为使用托管磁盘. 此过程会同时转换 OS 磁盘和任何附加的数据磁盘. 本文介绍如何使用 Az ...
- mobx 入门
observable(可观察的数据) 数组 import { observable, isArrayLike } from 'mobx' const arr = observable(['a', 'b ...
- 软件工程实践_结对Task2_ student_department_matching
1. 给出结对成员的学号及姓名. 结对成员 031502506 陈龙江 031502529 王国超 click 2.首页给出项目的Github链接. github:传送门 3. 贴出你们生成的一组最& ...
- Django admin 的模仿流程
- EBS-新增和更新价目表行
版权声明:本文为博主原创文章.未经博主同意不得转载. https://blog.csdn.net/gh320/article/details/36666133 新增和更新价目表行 --目的:在已 ...
- ECS简述
一.查看ECS实例 使用场景 •实例的日常维护 •实例运行状态和详细信息的查看 二.启动ECS实例 使用场景 •实例停止运行之后的再次启动 三.停止.重启ECS实例 使用场景 •实例运行的服务暂停时停 ...
- dubbo报错Data length too large: 10710120处理,及服务提供者协议配置详细说明
工作中遇到以下报错信息 cause: java.io.IOException: Data length too large: 10710120, max payload: 8388608, chann ...