我们在用到ItemsControl时,有时会用到分组,如ListBox,ListView,DataGrid。WPF的ItemsControl可以实现分组,是依托于GroupStyle,以ListBox为例,他的分组效果图为:

  以下为前台:

 <ListBox Name="lbMain">
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal">
<TextBlock Text="{Binding FileName}"
Width="" />
<TextBlock Text="{Binding AuthorName}"
Width="" />
<TextBlock Text="{Binding UpTime}"
Width="" />
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
<ListBox.GroupStyle>
<GroupStyle>
<GroupStyle.ContainerStyle>
<Style TargetType="{x:Type GroupItem}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type GroupItem}">
<Expander IsExpanded="True"
ExpandDirection="Down">
<Expander.Header>
<StackPanel Orientation="Horizontal">
<TextBlock Text="{Binding Path=Name}"
VerticalAlignment="Center" />
<TextBlock Text="{Binding Path=ItemCount, StringFormat=数量:{0}}"
VerticalAlignment="Center"
Margin="5,0,0,0" />
<Button Content="Sale"
Margin="5,0,0,0" />
</StackPanel>
</Expander.Header>
<ItemsPresenter />
</Expander>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</GroupStyle.ContainerStyle>
</GroupStyle>
</ListBox.GroupStyle>
</ListBox>

  从16行可以看出,GroupStyle定义的是控件内部样式,所以有人尝试在这里绑实体数据属性的话肯定是失败的,注意25行只能是Name,不管分组的属性叫什么名,这都只能是Name,我写了个Button在里面,如果想知道为什么只能是Name,写个Click处理,把Button的DataContext打印出来就什么都知道了。如果想在这里做更多的处理,比如进行一些负责的运算,可以写加转换器。

  这里只是弄了一个原始的Expander装载分组控件,需要美化可以另写样式。

  以下是后台:

 public class ModelFile
{
public string FileName { get; set; }
public string AuthorName { get; set; }
public string UpTime { get; set; }
} public partial class MainWindow : Window
{
public ObservableCollection<ModelFile> CollectionModelFile = new ObservableCollection<ModelFile>(); public MainWindow()
{
InitializeComponent(); CollectionModelFile.Add(new ModelFile() { FileName = "WPF进化史", AuthorName = "王鹏", UpTime = "2014-10-10" });
CollectionModelFile.Add(new ModelFile() { FileName = "WPF概论", AuthorName = "大飞", UpTime = "2014-10-10" });
CollectionModelFile.Add(new ModelFile() { FileName = "WPF之美", AuthorName = "小虫", UpTime = "2014-10-11" });
CollectionModelFile.Add(new ModelFile() { FileName = "WPF之道", AuthorName = "青草", UpTime = "2014-11-11" });
CollectionModelFile.Add(new ModelFile() { FileName = "WPF之禅", AuthorName = "得瑟鬼", UpTime = "2014-11-11" });
CollectionModelFile.Add(new ModelFile() { FileName = "WPF入门", AuthorName = "今晚吃什么", UpTime = "2014-11-11" });
CollectionModelFile.Add(new ModelFile() { FileName = "WPF神技", AuthorName = "无间道王二", UpTime = "2014-12-12" });
CollectionModelFile.Add(new ModelFile() { FileName = "WPF不为人知之密", AuthorName = "星期八", UpTime = "2014-12-12" });
CollectionModelFile.Add(new ModelFile() { FileName = "WPF之革命", AuthorName = "两把刀", UpTime = "2014-12-12" }); lbMain.ItemsSource = CollectionModelFile; ICollectionView cv = CollectionViewSource.GetDefaultView(lbMain.ItemsSource);
cv.GroupDescriptions.Add(new PropertyGroupDescription("UpTime"));
}
}

  重点是28、29行,有了这两句,ListBox就能准确得分组显示了,其他ItemsControl的分组类同。

  至此一个简单的ListBox分组显示就完成了,Demo已放群里,需要的可以下载来看。

最新文章

  1. JavaScript:编程改变文本样式
  2. Weblogic日志机制详解
  3. hibernate 问题
  4. Unity封装dll教程整理
  5. Dubbo架构设计详解--转载
  6. POJ 3159 Candies 差分约束dij
  7. java -Xmx3550m -Xms3550m -Xmn2g -Xss128k
  8. android-个性化进度条
  9. 方形布局SquareLayout
  10. 解决Sqlite UTF-8中文数据格式在DOS窗口下中文乱码
  11. 干货|人人都是翻译项目的Master
  12. Netty——高级发送和接收数据handler处理器
  13. 微信小程序版2048
  14. hdu 5427(水)
  15. linux 使用sh@d0ws0cks server
  16. Spring的单例模式底层实现
  17. Django的form表单
  18. Android中软键盘展示、EditText焦点获取及windowSoftInputMode属性探究
  19. 自动化测试框架对比(UIAutomator、Appium)
  20. ansible笔记(3):ansible模块的基本使用

热门文章

  1. Html编码(&amp;#数字型)与解码小结 - 针对Puny Code(中文域名)的解码处理
  2. Win10 设备补丁更新
  3. MySQL的四种主要存储引擎
  4. 如何让Oracle数据库保持优良性能的方法
  5. 修改hosts工具推荐SwitchHosts
  6. 【转】jpg png区别和使用
  7. theano中tensor的构造方法
  8. BCB::TClientSocket,TServerSocket控件
  9. java的四个元注解 @Retention @Target @Document @Inherited
  10. cudaMallocPitch – 向GPU分配存储器