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

相关推荐
△曉風殘月〆6 分钟前
在WPF中保存控件内容为图片
wpf
芝麻科技1 小时前
Wpf使用NLog将日志输出到LogViewer
wpf·prism
△曉風殘月〆19 小时前
WPF颜色(SolidColorBrush)和Win32颜色(COLOREF)互转的方法
wpf·win32·solidcolorbrush·colorref
shanshan20991 天前
上位机系统架构 | 如何设计一个高效的多相机管理系统
c#·wpf·相机
充值内卷1 天前
WPF入门教学四 WPF控件概述
windows·ui·wpf
小白2 天前
WPF自定义Dialog模板,内容用不同的Page填充
wpf
Crazy Struggle2 天前
C# + WPF 音频播放器 界面优雅,体验良好
c#·wpf·音频播放器·本地播放器
James.TCG3 天前
WPF动画
wpf
He BianGu3 天前
笔记:简要介绍WPF中FormattedText是什么,主要有什么功能
笔记·c#·wpf
脚步的影子4 天前
.NET 6.0 + WPF 使用 Prism 框架实现导航
.net·wpf