WPF美化ItemsControl1:不同颜色间隔

首先我们有的是一个绑定好数据的ItemsControl

XML 复制代码
<ItemsControl ItemsSource="{Binding Starts}">
</ItemsControl>

运行后呢是朴素的将数据竖着排列

如果想要数据之间有间距,可以使用数据模板,将数据放到TextBlock中显示,这样就可以设置间距了

XML 复制代码
<ItemsControl ItemsSource="{Binding Starts}">
    <ItemsControl.ItemTemplate>
        <DataTemplate>
            <TextBlock Padding="5" Text="{Binding}"></TextBlock>
        </DataTemplate>
    </ItemsControl.ItemTemplate>
</ItemsControl>

如果还想要让数据隔一行显示不同背景就可以使用触发器来进行设置

XML 复制代码
<ItemsControl ItemsSource="{Binding Starts}" AlternationCount="2">
    <ItemsControl.ItemTemplate>
        <DataTemplate>
            <Border x:Name="border" Padding="5">
                <TextBlock Text="{Binding}"></TextBlock>
            </Border>

            <DataTemplate.Triggers>
                <Trigger Property="ItemsControl.AlternationIndex" Value="1">
                    <Setter TargetName="border" Property="Background" Value="lightgray"></Setter>
                </Trigger>
            </DataTemplate.Triggers>
        </DataTemplate>
    </ItemsControl.ItemTemplate>
</ItemsControl>

--- 重点是设置2个就是一个间隔

对于这个间隔的触发器是有很多种写法的:WPF触发器的多种写法-CSDN博客

相关推荐
ALex_zry15 小时前
C++高性能日志与监控系统设计
c++·unity·wpf
zhojiew1 天前
使用flink agent框架实现流式情感分析的示例
大数据·flink·wpf
海盗12341 天前
ScottPlot在WPF的基本使用和中文乱码问题
c#·.net·wpf
俄城杜小帅1 天前
C++线程异步和wpf中比较
java·c++·wpf
ZoeJoy81 天前
WPF 从入门到实践:基础、ModernUI 与 MVVM 完全指南
c#·wpf
△曉風殘月〆2 天前
WPF Prism中的MVVM实现
wpf·mvvm
量子物理学2 天前
.NET8 中 WPF与ScottPlot 报表 的完美结合
.net·wpf
△曉風殘月〆2 天前
WPF Prism区域导航功能详解
wpf·mvvm
星河Cynthia3 天前
WPF基于resx资源文件的多语言实现
c#·wpf
量子物理学3 天前
WPF 标签预览可以显示图片运行后不显示
c#·wpf