原文: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设置分组的更多相关文章

  1. WPF ListView控件设置奇偶行背景色交替变换以及ListViewItem鼠标悬停动画

    原文:WPF ListView控件设置奇偶行背景色交替变换以及ListViewItem鼠标悬停动画 利用WPF的ListView控件实现类似于Winform中DataGrid行背景色交替变换的效果,同 ...

  2. WPF ListView 居中显示

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

  3. Android 开源库StickyListHeadersListView来实现ListView列表分组效果

    项目中有一新的需求,要求能像一些Android机带"联系人列表"一样,数据可以自动分组,且在列表滑动过程中,列表头固定在顶部,效果图如下: 下面就带大家实现上面的效果, 首先,我们 ...

  4. 给ListView设置emptyView

    给ListView设置emptyView 版权声明:本文为博主原创文章,未经博主允许不得转载. 使用ListView和GridView时,当列表为空时,默认是不显示任何内容的,这样对用户非常不友好,这 ...

  5. PB gird类型数据窗口 设置分组、分组小计、合计

    今天遇到一个需求,gird表格数据如下:  部门  类型 数据   A  类型1  1  A  类型2  2  B  类型1  3  B  类型2  4   合计 10 实际需要显示的结果为:  部门 ...

  6. 在为ListView设置adapter时出错

    为listView设置adapter,代码如下: SimpleAdapter simpleAdapter = new SimpleAdapter(this, listItems, R.layout.m ...

  7. WPF后台设置xaml控件的样式System.Windows.Style

    WPF后台设置xaml控件的样式System.Windows.Style 摘-自 :感谢 作者: IT小兵   http://3w.suchso.com/projecteac-tual/wpf-zhi ...

  8. WPF ListView 选中问题

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

  9. 【转】三十三、Android给ListView设置分割线Divider样式

    原文网址:http://www.cnblogs.com/linjiqin/archive/2011/11/12/2246349.html 给ListView设置分割线,只需设置如下两个属性: andr ...

随机推荐

  1. loadrunner 运行脚本-Run-time Settings->General->Additional attributes设置

    运行脚本-Run-time Settings->General->Additional attributes设置 by:授客 QQ:1033553122 作用说明 为Vuser脚本提供额外 ...

  2. IntelliJ IDEA 2017 永久注册方法

    https://blog.csdn.net/weixin_39913200/article/details/80859897 在安装的idea下面的bin目录下面有2个文件 : 一个是idea64.e ...

  3. 简单易懂的程序语言入门小册子(3):基于文本替换的解释器,let表达式,布尔类型,if表达式

    let表达式 let表达式用来声明一个变量. 比如我们正在写一个模拟掷骰子游戏的程序. 一个骰子有6个面. 所以这个程序多次用到了6这个数字. 有一天,我们忽然改变主意,要玩12个面的骰子. 于是我们 ...

  4. Servlet (HttpServletResponse)对象

    1.setStatus(int status)方法:用于设置HTTP响应消息的状态码,并生成响应状态行.响应状态行中的状态描述信息直接与状态码相关,HTTP版本由服务器确定,因此只需要通过这个方法设置 ...

  5. rpm安装时出现循环依赖

    在安装git包时提示要安装perl-git,当安装perl-git时又提示要安装git包.报错如下: [root@racdb1 Packages]# rpm -ivh perl-Git-1.7.1-4 ...

  6. git命令行解决冲突文件步骤

    原文https://blog.csdn.net/zwl18210851801/article/details/79106448 亲测有用,解决git冲突的好办法 方法一(推荐使用): git pull ...

  7. C#异步编程のWPF Invoke和BeginInvoke

    我们先看个简单的例子: private void Window_Loaded(object sender, RoutedEventArgs e) { Console.WriteLine("B ...

  8. js对象属性与Bom

    1JavaScript 的对象 1.1字符串 1.2Array对象 1.3Date对象 1.4Math对象 1.5Function对象(重点) 2 Bom对象 1   JavaScript对象 在Ja ...

  9. JQuery获取touchstart,touchmove,touchend坐标

    $('#id').on('touchstart',function(e) { ].pageX; }); JQuery如上. document.getElementById("id" ...

  10. maven 打包生成doc和源码插件

    <!--配置生成Javadoc包--> <plugin> <groupId>org.apache.maven.plugins</groupId> < ...