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

相关推荐
dotent·1 天前
C#基于WPF UI框架的通用基础上位机测试WPF框架
ui·c#·wpf
咩图2 天前
WPF+Prism8.0.0.1909+C#创建一个桌面程序
c#·wpf·prism
雁于飞2 天前
分布式基础
java·spring boot·分布式·spring·wpf·cloud native
oioihoii2 天前
WPF入门指南:解析默认项目结构
wpf
极客智造3 天前
深入解析 ReactiveUI:WPF 响应式 MVVM 开发的 “终极方案”
wpf
Macbethad4 天前
使用WPF编写一个多维度伺服系统的程序
大数据·hadoop·wpf
lingxiao168884 天前
WPF Prism框架应用
c#·wpf·prism
Macbethad5 天前
使用WPF编写一个Ethercat主站的程序
wpf
难搞靓仔5 天前
WPF 弹出窗体Popup
wpf·popup
Macbethad5 天前
使用WPF编写一个MODBUSTCP通信的程序
wpf