当前位置:网站首页>【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的方式去访问。
边栏推荐
猜你喜欢
随机推荐
类的比较大小(Comparable -> compareTo(类自己实现接口),Comparator -> compare(新建一个类作为比较器))
MySQL-4-常规概念
Vest bag access process record
【Untitled】
Why do I no longer recommend the enumeration strategy pattern?
再见Attention:建模用户长期兴趣的新范式
How to use PHP to implement lexical analyzer and custom language
SQL学习笔记——REGEXP运算符
轻松入门自然语言处理系列 专题8 源码解读──基于HMM的结巴分词
CNN flower classification
ROS 之 KUKA iiwa编程
Basic management of mysql database in Linux system
Brute-force cracking of the latest JVM interview questions of Meituan: unlimited execution
esp32之arduino配置下载提速
Go-4-在vim中无法跳转到源代码
虚拟现实处理器(SXR2130P)ISO7640FMDW(数字隔离器)说明
线程安全问题以及其解决方法
vim的高级用法配置
NXP i.MX 8M Mini工业核心板B2B版本,4核ARM [email protected]设计
2022 Security Officer-A Certificate Exam Questions and Mock Exam









