首页 > Windows开发 > 详细

WPF : ListBox的几种Template属性

时间:2019-04-16 00:39:40      阅读:171      评论:0      收藏:0      [点我收藏+]
原文:WPF : ListBox的几种Template属性

属性名 属性的类名 功能 示例
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>

</Style>

WPF : ListBox的几种Template属性

原文:https://www.cnblogs.com/lonelyxmas/p/10714293.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!