假设想实现ComboBoxItem的Foreground样式,可以在PointerOver或Pressed的状态下呈现不同的效果,那么可以这样实现:
<Style x:Name="DelayClockComboBoxStyle" TargetType="ComboBox">
<Setter Property="Height" Value="48"/>
<Setter Property="Width" Value="180"/>
<Setter Property="TabNavigation" Value="Once" />
<Setter Property="ScrollViewer.VerticalScrollBarVisibility" Value="Hidden" />
<Setter Property="ScrollViewer.HorizontalScrollMode" Value="Disabled" />
<Setter Property="ScrollViewer.VerticalScrollMode" Value="Enabled" />
<Setter Property="ScrollViewer.IsDeferredScrollingEnabled" Value="False" />
<Setter Property="ScrollViewer.BringIntoViewOnFocusChange" Value="True" />
<Setter Property="HorizontalContentAlignment" Value="Center" />
<Setter Property="FontFamily" Value="{StaticResource ContentControlThemeFontFamily}" />
<Setter Property="ComboBox.ItemTemplate">
<Setter.Value>
<DataTemplate>
<Border Width="180" Height="48" Background="Transparent" Margin="0"
BorderThickness="2" BorderBrush="White">
<!--Foreground直接绑定本身所在的容器ContentPresenter-->
<TextBlock Text="{Binding}" FontSize="26" Foreground="{Binding Foreground, ElementName=ContentPresenter}"
HorizontalAlignment="Center" VerticalAlignment="Center"/>
</Border>
</DataTemplate>
</Setter.Value>
</Setter>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="ComboBox">
<Grid>
<VisualStateManager.VisualStateGroups>
<VisualStateGroup x:Name="CommonStates">
<VisualState x:Name="Normal" />
<VisualState x:Name="PointerOver"/>
<VisualState x:Name="Pressed">
<Storyboard>
<!--Pressed时改变ContentPresendter的Foreground,那么item的foreground也跟着改变-->
<ObjectAnimationUsingKeyFrames Storyboard.TargetName="ContentPresenter"
Storyboard.TargetProperty="Foreground">
<DiscreteObjectKeyFrame KeyTime="0" Value="{StaticResource AppAccentBrush}" />
</ObjectAnimationUsingKeyFrames>
<ObjectAnimationUsingKeyFrames Storyboard.TargetName="Background"
Storyboard.TargetProperty="Background">
<DiscreteObjectKeyFrame KeyTime="0" Value="White" />
</ObjectAnimationUsingKeyFrames>
</Storyboard>
</VisualState>
<VisualState x:Name="Disabled"/>
</VisualStateGroup>
<VisualStateGroup x:Name="FocusStates">
<VisualState x:Name="Focused"/>
<VisualState x:Name="FocusedPressed"/>
<VisualState x:Name="Unfocused" />
<VisualState x:Name="PointerFocused" />
<VisualState x:Name="FocusedDropDown">
<Storyboard>
<ObjectAnimationUsingKeyFrames Storyboard.TargetName="PopupBorder"
Storyboard.TargetProperty="Visibility"
Duration="0">
<DiscreteObjectKeyFrame KeyTime="0">
<DiscreteObjectKeyFrame.Value>
<Visibility>Visible</Visibility>
</DiscreteObjectKeyFrame.Value>
</DiscreteObjectKeyFrame>
</ObjectAnimationUsingKeyFrames>
</Storyboard>
</VisualState>
</VisualStateGroup>
<VisualStateGroup x:Name="DropDownStates">
<VisualState x:Name="Opened">
<Storyboard>
<SplitOpenThemeAnimation
OpenedTargetName="PopupBorder"
ContentTargetName="ScrollViewer"
ClosedTargetName="ContentPresenter"
ContentTranslationOffset="0"
OffsetFromCenter="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=TemplateSettings.DropDownOffset}"
OpenedLength="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=TemplateSettings.DropDownOpenedHeight}"
ClosedLength="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=TemplateSettings.DropDownClosedHeight}" />
</Storyboard>
</VisualState>
<VisualState x:Name="Closed">
<Storyboard>
<SplitCloseThemeAnimation
OpenedTargetName="PopupBorder"
ContentTargetName="ScrollViewer"
ClosedTargetName="ContentPresenter"
ContentTranslationOffset="40"
OffsetFromCenter="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=TemplateSettings.DropDownOffset}"
ContentTranslationDirection="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=TemplateSettings.SelectedItemDirection}"
OpenedLength="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=TemplateSettings.DropDownOpenedHeight}"
ClosedLength="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=TemplateSettings.DropDownClosedHeight}" />
</Storyboard>
</VisualState>
</VisualStateGroup>
</VisualStateManager.VisualStateGroups>
<Border x:Name="Background"
Background="Transparent"
BorderThickness="0"/>
<!--此处的ContentPresendter对应着ItemTemplate中的每一个Item-->
<ContentPresenter x:Name="ContentPresenter"
Margin="{TemplateBinding Padding}"
Foreground="White"
HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}"
VerticalAlignment="{TemplateBinding VerticalContentAlignment}" />
<Popup x:Name="Popup">
<Border x:Name="PopupBorder"
Background="Transparent"
BorderThickness="3"
BorderBrush="White"
HorizontalAlignment="Stretch">
<ScrollViewer x:Name="ScrollViewer"
Foreground="{StaticResource ComboBoxPopupForegroundThemeBrush}"
VerticalSnapPointsType="OptionalSingle"
VerticalSnapPointsAlignment="Far"
HorizontalScrollMode="{TemplateBinding ScrollViewer.HorizontalScrollMode}"
HorizontalScrollBarVisibility="{TemplateBinding ScrollViewer.HorizontalScrollBarVisibility}"
VerticalScrollMode="{TemplateBinding ScrollViewer.VerticalScrollMode}"
VerticalScrollBarVisibility="{TemplateBinding ScrollViewer.VerticalScrollBarVisibility}"
IsHorizontalRailEnabled="{TemplateBinding ScrollViewer.IsHorizontalRailEnabled}"
IsVerticalRailEnabled="{TemplateBinding ScrollViewer.IsVerticalRailEnabled}"
IsDeferredScrollingEnabled="{TemplateBinding ScrollViewer.IsDeferredScrollingEnabled}"
BringIntoViewOnFocusChange="{TemplateBinding ScrollViewer.BringIntoViewOnFocusChange}"
ZoomMode="Disabled">
<ItemsPresenter/>
</ScrollViewer>
</Border>
</Popup>
</Grid>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
VisualStateGroup中的每一个VisualState与先后顺序有关的,有可能后一个State会覆盖了前一个State的效果。
同时这样的一个样式动不动就会很长,所以有一些效果没什么必要的话就应该省略从简。
分享到:
相关推荐
Become a leading Windows 8 app developer by using Windows 8 App Projects - XAML and C# Edition to learn techniques, tools, and ideas to create successful, 5-star apps. Windows 8 App Projects - XAML ...
spinkit-xaml, Spinkit CSS微调器 Spinkit.Xaml由 Windows Tobias Ahlin的CSS SpinKit插件启发的用于 8进度环控件的样式集的端口。 在 Youtube上演示视频: Spinkit Xaml演示程序。用法安装nuget软件包:
开发人员不受今天在RNW和支持Windows的社区模块中提供的控件的限制。 最重要的是,今天这个解决方案可用! 类型/属性/事件的集合是XAML的,但是JS库可以包装那些类型/属性/事件,以与平台无关的方式公开Windows...
SurfaceImageSource - XAML UI 中的 DirectX 岛
Windows 8 应用程序开发 - XAML版 (英文PDF) This book is intended for developers who have learned the basics of the C# programming language and the Microsoft .NET platform and are ready to expand ...
作为Windows中的本机UI层,它体现了,使每个Windows应用程序都具有客户期望的优美感觉。 WinUI 2是一个控件库,为Windows 提供了官方的本机Microsoft UI控件和功能。 WinUI 2可以在任何Windows 10 UWP XAML应用中...
资源名称:Windows 8开发基础系列视频课程资源目录:【】1-Windows8的使用【】10-布局的应用:给Button指定复杂内容【】2-Windows8开发环境搭建【】3-大一统的XAML技术【】4-WindowsStore是怎么回事?【】5-所有从...
Windows 8高级编程——使用C#和XAML开发应用.rar
HLSL for windows shaders programming
win8-xaml-sdk 适用于 Windows 8 XAML SDK 的 Telerik UI是一个易于使用的基础设施,其中包含套件的所有控件的以开发人员为中心的示例。 每个控件都有一个包含多个项目的解决方案,这些项目是实际示例。 每个示例都...
Windows 8高级编程——使用C#和XAML开发应用 书中示例的源代码
Windows8高级编程:使用C#和XAML开发应用 .pdf
[Windows.8中XAML的初级读本(入门)]Windows.8.XAML.Primer.Jesse.Liberty.影印版
Programming Windows, Sixth Edition focuses on creating Windows 8 apps accessing the Windows Runtime with XAML and C#. The book also provides C++ code samples. The Sixth Edition is organized in two ...
Windows8应用程序揭秘,使用XAML和C#编程语言
PhotoHub - Windows Phone 8 XAML LongListSelector
WindowsPhone开发,包含聊天室案例开始,阅读器、天气预报、SignalR
windows8开发者训练营实例源码以及英文开发文档