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博客

相关推荐
大霸王龙4 小时前
系统模块与功能设计框架
人工智能·wpf
明耀19 小时前
WPF DataGrid 默认显示行号
wpf
lph19721 天前
wpf的converter
wpf
fyifei05581 天前
WPF学习PropertyChanged
wpf
爱炸薯条的小朋友1 天前
C#由于获取WPF窗口名称造成的异常报错问题
windows·c#·wpf
baivfhpwxf20231 天前
wpf ListBox 去除item 单击样式
wpf
诗仙&李白1 天前
lnnovationHubTool,用prism+WPF编写的MVVM模式的快速上位机软件开发框架平台
wpf·mvvm·prism·上位机软件开发框架平台
程序员小刘1 天前
【HarmonyOS 5】教育开发实践详解以及详细代码案例
华为·wpf·harmonyos
Java Fans2 天前
在WPF项目中集成Python:Python.NET深度实战指南
python·.net·wpf
布伦鸽2 天前
C# WPF 左右布局实现学习笔记(1)
笔记·学习·c#·wpf