属性名 | 属性的类名 | 功能 | 示例 |
Template | ControlTemplate | 定义控件自身的外观. 其子元素的布局可以自定义,也可以由ItemsPresenter定义. |
<Style TargetType="ListBox"> <Setter Property="Template"> <Setter.Value> <ControlTemplate TargetType="ListBox"> <Border> <ScrollViewer> <StackPanel IsItemsHost="True"/> </ScrollViewer> </Border> </ControlTemplate> </Setter.Value> </Setter> </Style> IsItemsHost=true表示子元素将显示在此容器中.此处StackPanel也可以用<ItemsPresenter/>代替. |
ItemsPanel | ItemsPanelTemplate | 定义子元素的布局, 其内容为StackPanel, Grid, WrapPanel, DockPanel等布局容器. ItemsPresenter会创建该属性指定的布局容器 |
<Style TargetType="ListBox"> <Setter Property="ItemsPanel"> <Setter.Value> <ItemsPanelTemplate> <StackPanel Orientation="Horizontal" VerticalAlignment="Center" HorizontalAlignment="Center"/> </ItemsPanelTemplate> </Setter.Value> </Setter> </Style> |
ItemTemplate | DataTemplate | 定义每个子元素的外观. 这个值会拷贝给ListBoxItem的ContentTemplate属性 |
<ListBox> <ListBox.ItemTemplate> <DataTemplate> <StackPanel> <TextBlock Text="{Binding Path=TaskName}" /> <TextBlock Text="{Binding Path=Description}"/> <TextBlock Text="{Binding Path=Priority}"/> </StackPanel> </DataTemplate> </ListBox.ItemTemplate> </ListBox> |
一起使用:
<Style TargetType="{x:Type ListBox}">
<!-- 定义每个子元素的外观, 显示每个子元素的数据 -->
<Setter Property="ItemTemplate">
<Setter.Value>
<DataTemplate>
<StackPanel>
<TextBlock Text="{Binding Path=TaskName}" />
<TextBlock Text="{Binding Path=Description}"/>
<TextBlock Text="{Binding Path=Priority}"/> </StackPanel>
</DataTemplate>
</Setter.Value>
</Setter><!-- 定义子元素的布局容器, 比如:横向,纵向 -->
<Setter Property="ItemsPanel">
<Setter.Value>
<ItemsPanelTemplate>
<StackPanel Orientation="Horizontal" VerticalAlignment="Center" HorizontalAlignment="Center"/>
</ItemsPanelTemplate>
</Setter.Value>
</Setter>
<!-- 定义ListBox自身外观, 比如: 圆角边框-->
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="ListBox">
<Border CornerRadius="5" Background="{TemplateBinding ListBox.Background}">
<ScrollViewer HorizontalScrollBarVisibility="Auto">
<ItemsPresenter/>
</ScrollViewer>
</Border>
</ControlTemplate>
</Setter.Value>
</Setter>
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 单线程的Redis速度为什么快?
· SQL Server 2025 AI相关能力初探
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
· 展开说说关于C#中ORM框架的用法!