WPF : ListBox的几种Template属性

属性名属性的类名功能示例
TemplateControlTemplate定义控件自身的外观.

其子元素的布局可以自定义,也可以由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/>代替.
ItemsPanelItemsPanelTemplate定义子元素的布局, 其内容为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>
ItemTemplateDataTemplate定义每个子元素的外观.

这个值会拷贝给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>

 WPF_Template

一起使用:

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

转载于:https://www.cnblogs.com/sjqq/p/7846360.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值