C# Avalonia UI的ItemControl

Avalonia UI 中 ItemControl

在 Avalonia UI 中,ItemsControl 是用于显示集合数据的基础控件,其样式可以通过 StyleDataTemplate 自定义。以下是一个示例代码,展示如何为 ItemsControl 定义样式并绑定数据:

csharp 复制代码
// 定义数据模型
public class ItemModel
{
    public string Name { get; set; }
    public string Description { get; set; }
}

// 在 ViewModel 中绑定数据
public class MainViewModel
{
    public ObservableCollection<ItemModel> Items { get; } = new ObservableCollection<ItemModel>
    {
        new ItemModel { Name = "Item 1", Description = "Description 1" },
        new ItemModel { Name = "Item 2", Description = "Description 2" }
    };
}

在 XAML 中定义 ItemsControl 样式

xml 复制代码
<ItemsControl ItemsSource="{Binding Items}">
    <ItemsControl.Styles>
        <!-- 定义 ItemsControl 的样式 -->
        <Style Selector="ItemsControl">
            <Setter Property="Template">
                <ControlTemplate>
                    <Border Background="#F0F0F0" Padding="10">
                        <ItemsPresenter/> <!-- 用于显示子项 -->
                    </Border>
                </ControlTemplate>
            </Setter>
        </Style>

        <!-- 定义子项的样式 -->
        <Style Selector="ItemsControl > ContentPresenter">
            <Setter Property="Margin" Value="0 5"/>
        </Style>

        <!-- 定义数据项的样式 -->
        <Style Selector="ContentPresenter > Border">
            <Setter Property="Background" Value="White"/>
            <Setter Property="Padding" Value="10"/>
            <Setter Property="BorderBrush" Value="#DDD"/>
            <Setter Property="BorderThickness" Value="1"/>
            <Setter Property="CornerRadius" Value="4"/>
        </Style>
    </ItemsControl.Styles>

    <!-- 定义数据项的 DataTemplate -->
    <ItemsControl.ItemTemplate>
        <DataTemplate>
            <Border>
                <StackPanel>
                    <TextBlock Text="{Binding Name}" FontWeight="Bold"/>
                    <TextBlock Text="{Binding Description}" Foreground="#666"/>
                </StackPanel>
            </Border>
        </DataTemplate>
    </ItemsControl.ItemTemplate>
</ItemsControl>

注意事项

1. 层级选择器

  • ItemsControl > ContentPresenter 选择直接子项容器。
  • ContentPresenter > Border 选择数据项模板中的根元素。

2. 数据绑定

  • 确保 ItemsSource 绑定到正确的集合属性。
  • DataTemplate 中的绑定路径需与数据模型属性匹配。

3. 性能优化

  • 对于大数据集,建议使用 VirtualizingStackPanel 作为 ItemsPanel 以减少内存占用:

    xml 复制代码
    <ItemsControl.ItemsPanel>
        <ItemsPanelTemplate>
            <VirtualizingStackPanel/>
        </ItemsPanelTemplate>
    </ItemsControl.ItemsPanel>

4. 模板覆盖

  • 若需完全自定义布局,可重写 ControlTemplate 并指定 ItemsPresenter 的位置。

5. 样式作用域

  • 样式定义在 ItemsControl.Styles 中时,仅作用于当前控件及其子项。若需全局样式,应定义在 App.axamlApplication.Styles 中。

完整示例效果

  • 外层 ItemsControl 带灰色背景和内边距。
  • 每个数据项显示为白色卡片,包含名称(粗体)和描述(灰色文字)。
  • 子项之间通过 Margin 分隔。
相关推荐
wuyk5551 分钟前
Python实战项目02:学生成绩管理系统(控制台|CSV导出|完整落地)
开发语言·python
浪子明X7 小时前
十六位账本怎样发现传输差错:Internet Checksum 生活类比
开发语言
大蓝头8 小时前
安装包UI美化之路-借助AI快速使用nsNiuniuSkin制作安装包
ui·安装包·nsis·安装包美化·nsniuniuskin
xiangyun6110 小时前
【408数据结构 08】队列:循环队列判空判满,408年年考,一次讲清
c语言·开发语言·数据结构·c++·算法
xiangyun6110 小时前
【408数据结构 06】双链表、循环链表、静态链表
c语言·开发语言·数据结构·c++·算法
唐青枫11 小时前
对象到底住在哪里?C#.NET 托管堆从分配、GC 到内存排查
c#·.net
白远山11 小时前
家政服务家政社区派单实战:调度系统架构设计与实现指南
java·开发语言·小程序·架构·需求分析
DevRay12 小时前
Java 21虚拟线程深度实战:告别线程池焦虑,百万并发吞吐量直接翻倍(原理+代码+避坑)
java·开发语言
兰亭妙微UI设计公司12 小时前
兰亭妙微APP界面设计公司分享: iPhone Duo 登场|3 大核心要点,拆解折叠屏 UI 适配设计思路
ui·交互
free-elcmacom12 小时前
C++学习<1>程序分区
开发语言·c++