当前位置:网站首页>【wpf】ListView 和 ItemsControl 的一点区别
【wpf】ListView 和 ItemsControl 的一点区别
2022-08-02 15:09:00 【code bean】
ItemsControl 实现背景间隔效果
<ItemsControl ItemsSource="{StaticResource datas}" AlternationCount="2">
<ItemsControl.ItemTemplate>
<DataTemplate>
<Grid Background="Transparent" Name="root">
<Grid.ColumnDefinitions>
<ColumnDefinition/>
<ColumnDefinition/>
<ColumnDefinition/>
</Grid.ColumnDefinitions>
<TextBlock Text="{Binding Name}"/>
<TextBlock Text="{Binding Age}" Grid.Column="1"/>
</Grid>
<DataTemplate.Triggers>
<Trigger Property="ItemsControl.AlternationIndex" Value="1">
<Setter Property="Background" Value="Orange" TargetName="root"/>
</Trigger>
</DataTemplate.Triggers>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
ListView实现背景间隔效果
listview无法直接通过DataTemplate.Triggers去设置,只能通过ItemContainerStyle的方式
<ListView ItemsSource="{StaticResource datas}" AlternationCount="2">
<ListView.ItemContainerStyle>
<Style TargetType="ListViewItem">
<Style.Triggers>
<Trigger Property="ItemsControl.AlternationIndex" Value="1">
<Setter Property="Background" Value="Orange"/>
</Trigger>
<Trigger Property="IsMouseOver" Value="True">
</Trigger>
</Style.Triggers>
</Style>
</ListView.ItemContainerStyle>
<ListView.ItemTemplate>
<DataTemplate>
<Grid Background="Transparent" Name="root">
<Grid.ColumnDefinitions>
<ColumnDefinition/>
<ColumnDefinition/>
<ColumnDefinition/>
</Grid.ColumnDefinitions>
<TextBlock Text="{Binding Name}"/>
<TextBlock Text="{Binding Age}" Grid.Column="1"/>
</Grid>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
通过snoop观察,listview比ItemsControl 多封装了几层:


由于多了个ListViewItem这一次,所以没法直接访问到ItemsControl.AlternationIndex
所以用了ItemContainerStyle的方式去访问。
边栏推荐
猜你喜欢

redis学习四redis消息订阅、pipeline、事务、modules、布隆过滤器、缓存LRU

威纶通触摸屏如何隐藏系统设置箭头图标及通过参数进入系统设置?

How to check the WeChat applet server domain name and modify it

太帅了!我用炫酷大屏展示爬虫数据!

第十五天笔记

类的比较大小(Comparable -> compareTo(类自己实现接口),Comparator -> compare(新建一个类作为比较器))

软件成分分析:华为云重磅发布开源软件治理服务

开篇-开启全新的.NET现代应用开发体验

最强分布式锁工具:Redisson

Win 10、Win 11 安装 MuJoCo 及 mujoco-py 教程
随机推荐
SIGIR'22 推荐系统论文之POI篇
Qt | 控件之 QCheckBox
暴力破解美团最新JVM面试题:无限执行
leetcode 504. Base 7 七进制数 (简单)
23.支持向量机的使用
【Untitled】
性能测试详解(理论篇)
再见Attention:建模用户长期兴趣的新范式
继续来学习有关淘宝的API接口的使用——获得店铺的所有商品 API
第十七天笔记
PAT Class A 1130 Infix Expressions
【 Leetcode string, the string transform/hexadecimal conversion 】 HJ1. The length of the string last word HJ2. Calculation of a certain number of characters appear HJ30. String merging processing
已经2022下半年了,居然还在说链动2+1!
WWW'22 推荐系统论文之多任务与对比学习篇
线程安全问题以及其解决方法
redis学习四redis消息订阅、pipeline、事务、modules、布隆过滤器、缓存LRU
【个人总结】2022.7月结
aPaaS低代码平台(二) | 快速构建业务模型
开篇-开启全新的.NET现代应用开发体验
【服务器数据恢复】Raid阵列更换故障硬盘后数据同步失败的数据恢复案例