WPF三大模板简介(Z)

WPF三大模板简介

 

WPF支持以下类型的模板:

(1) 控件模板。控件模板可以将自定义模板应用到某一特定类型的所有控件,或是控件的某一实例。决定控件外观的是ControlTemplate,它决定了控件“长成什么样子”,因此控件模板由ControlTemplate类表示。控件模板实际在资源集合或资源字典中定义。例子详见:通过设计ControlTemplate,制作圆角文本框与圆角按钮(http://www.cnblogs.com/zhouhb/p/3284780.html)。

(2) 数据模板。在WPF中,决定数据外观的是DataTemplate,即DataTemplate是数据内容的表现形式,一条数据显示成什么样子,是简单的文本还是直观的图形,就是由DataTemplate决定的。例子详见:DataTemplate应用(http://www.cnblogs.com/zhouhb/p/3284827.html)。

(3)ItemsPanelTemplate模板。ItemsPanelTemplate 指定用于项的布局的面板。如下面的例子通过设置ListBox的ItemsPanelTemplate,使得每一项从左至右按顺序显示。

因为WrapPanel从左至右按顺序位置定位子元素,在包含框的边缘处将内容断开至下一行。 后续排序按照从上至下或从右至左的顺序进行,具体取决于 Orientation 属性的值(默认值为 Horizontal)。

<ListBox AllowDrop="True" Margin="0,1,262,0">
<ListBox.ItemsPanel>
<ItemsPanelTemplate>
<WrapPanel/>
</ItemsPanelTemplate>
</ListBox.ItemsPanel>
<Label Name="label1" Content="E" />
<Label Name="label2" Content="A" />
<Label Name="label3" Content="I" />
<Label Name="label4" Content="M" />
</ListBox>

 

运行效果:

关于WPF三大模版的学习,可以进一步参照:WPF详解模板(http://www.cnblogs.com/dingli/archive/2011/07/20/2112150.html)。

转载于:https://www.cnblogs.com/tranw/p/6002998.html

您可以使用自定义模板来修改ComboBox的下拉箭头颜色。您可以使用以下代码创建自定义模板: ```xml <ComboBox> <ComboBox.Resources> <Style TargetType="{x:Type ToggleButton}"> <Setter Property="OverridesDefaultStyle" Value="True" /> <Setter Property="Foreground" Value="Black" /> <Setter Property="Template"> <Setter.Value> <ControlTemplate TargetType="ToggleButton"> <Grid> <VisualStateManager.VisualStateGroups> <VisualStateGroup x:Name="CheckStates"> <VisualState x:Name="Checked"> <Storyboard> <ColorAnimationUsingKeyFrames Storyboard.TargetProperty="(Shape.Fill).(SolidColorBrush.Color)" Storyboard.TargetName="Arrow"> <EasingColorKeyFrame KeyTime="0" Value="#FF8B8B8B"/> </ColorAnimationUsingKeyFrames> </Storyboard> </VisualState> <VisualState x:Name="Unchecked"/> <VisualState x:Name="Indeterminate"/> </VisualStateGroup> </VisualStateManager.VisualStateGroups> <Grid> <Grid.ColumnDefinitions> <ColumnDefinition Width="*"/> <ColumnDefinition Width="18"/> </Grid.ColumnDefinitions> <Border Grid.Column="0" BorderThickness="{TemplateBinding BorderThickness}" Padding="2"> <ContentPresenter /> </Border> <Path x:Name="Arrow" Grid.Column="1" Fill="#FF707070" HorizontalAlignment="Center" VerticalAlignment="Center" Data="M 0 0 L 4 4 L 8 0 Z"/> </Grid> </Grid> </ControlTemplate> </Setter.Value> </Setter> </Style> </ComboBox.Resources> </ComboBox> ``` 在这个模板中,我们重写了ToggleButton的模板,并使用Path来绘制箭头。我们在箭头路径的Data属性中定义一个三角形形状。 我们使用VisualStateManager来定义箭头在ToggleButton被选中时的颜色。在<VisualState x:Name="Checked">中,我们更新了箭头填充颜色的SolidColorBrush颜色。此代码显示灰色的箭头: ``` <ColorAnimationUsingKeyFrames Storyboard.TargetProperty="(Shape.Fill).(SolidColorBrush.Color)" Storyboard.TargetName="Arrow"> <EasingColorKeyFrame KeyTime="0" Value="#FF8B8B8B"/> </ColorAnimationUsingKeyFrames> ``` 我们将其中的“Value”属性更改为您想要的箭头颜色,然后使用此模板更新ComboBox的样式。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值