如果您的用户可以确定应用程序的哪个位可能响应输入,那就太好了.在某种程度上,这可以通过使按钮看起来像按钮来完成.如果某些东西看起来是可点击的,那么它可能就是.
然而,现代用户界面设计中的一个惯例是用户界面元素还应表示在鼠标移动时通过更改父母的响应意愿
例如,当鼠标移动时,内置按钮控件稍微改变其背景,暗示它是交互式的,然后在点击它时进一步更改父母看起来像它的选择.几乎所有控件都需要这样做,设计师需要一种方法来创建和编辑动画以实现它.
让我们看一下视觉状态的实例.考虑一个复选框.它可能是未选中或已选中,如果您选择,它可以支持第三个不确定状态.对于所有三种情况,控件需要看起来不同.因此,我们有三个视觉状态.
In为了证明它已经准备好响应用户输入,当鼠标移过它时,复选框会略微改变其外观,当鼠标固定在那里时它会进一步改变.如果禁用该复选框,则必须考虑第四个状态,它看起来很棒,并表示它不会响应用户输入.
因此,我们还有另外四个州.在任何给定时间,复选框的可视状态必须是正常,鼠标悬停,已检查或已禁用.同时,它必须是选中,未选中或不确定.
由于模板定义了控件的外观,因此模板需要定义每个Visual状态会发生什么.到目前为止我们查看的模板不包含此类信息.因此,无论当前状态如何,控件的外观都保持静态.
要向模板添加可视状态,首先要添加属性元素.
您可以为视觉状态处理做的最简单的事情是定义在控件进入特定状态时运行的动画.
控件会在状态改变时通知可视状态管理器类.
然后是可视状态管理器查看模板的这一部分并确定要运行的动画.
因此,当复选框输入鼠标时,此动画将运行,更改模板某些部分的颜色.
让我们看一个简单的例子,通过使用可视状态机制来制作一个自定义反映状态更改的复选框的模板.
下面给出了带有 visual s的复选框自定义模板的XAML代码tate .
<UserControl xmlns = "http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x = "http://schemas.microsoft.com/winfx/2006/xaml" x:Class = "CheckboxVisualState.Page" Width = "640" Height="480" xmlns:vsm = "clrnamespace:System.Windows;assembly = System.Windows" xmlns:d = "http://schemas.microsoft.com/expression/blend/2008" xmlns:mc = "http://schemas.openxmlformats.org/markup-compatibility/2006" mc:Ignorable = "d"> <UserControl.Resources> <ControlTemplate x:Key = "CheckBoxControlTemplate1" TargetType = "CheckBox"> <Grid> <vsm:VisualStateManager.VisualStateGroups> <vsm:VisualStateGroup x:Name = "FocusStates"> <vsm:VisualState x:Name = "ContentFocused"/> <vsm:VisualState x:Name = "Focused"/> <vsm:VisualState x:Name = "Unfocused"/> </vsm:VisualStateGroup> <vsm:VisualStateGroup x:Name = "CommonStates"> <vsm:VisualStateGroup.Transitions> <vsm:VisualTransition GeneratedDuration = "00:00:00.5000000"/> </vsm:VisualStateGroup.Transitions> <vsm:VisualState x:Name = "MouseOver"> <Storyboard> <ColorAnimationUsingKeyFrames BeginTime = "00:00:00" Duration = "00:00:00.0010000" Storyboard.TargetName = "background" Storyboard.TargetProperty = "(Shape.Fill). (SolidColorBrush.Color)"> <SplineColorKeyFrame KeyTime = "00:00:00" Value = "#FFFF0000"/> </ColorAnimationUsingKeyFrames> </Storyboard> </vsm:VisualState> <vsm:VisualState x:Name = "Pressed"> <Storyboard> <ColorAnimationUsingKeyFrames BeginTime = "00:00:00" Duration = "00:00:00.0010000" Storyboard.TargetName = "background" Storyboard.TargetProperty = "(Shape.Fill). (SolidColorBrush.Color)"> <SplineColorKeyFrame KeyTime = "00:00:00" Value = "#FFCEFF00"/> </ColorAnimationUsingKeyFrames> </Storyboard> </vsm:VisualState> <vsm:VisualState x:Name = "Disabled"/> <vsm:VisualState x:Name = "Normal"/> </vsm:VisualStateGroup> <vsm:VisualStateGroup x:Name = "CheckStates"> <vsm:VisualStateGroup.Transitions> <vsm:VisualTransition GeneratedDuration = "00:00:00.5000000"/> </vsm:VisualStateGroup.Transitions> <vsm:VisualState x:Name = "Checked"> <Storyboard> <DoubleAnimationUsingKeyFrames BeginTime = "00:00:00" Duration = "00:00:00.0010000" Storyboard.TargetName = "checkPath" Storyboard.TargetProperty = "(UIElement.Opacity)"> <SplineDoubleKeyFrame KeyTime = "00:00:00" Value = "1"/> </DoubleAnimationUsingKeyFrames> </Storyboard> </vsm:VisualState> <vsm:VisualState x:Name = "Unchecked"/> <vsm:VisualState x:Name = "Indeterminate"/> </vsm:VisualStateGroup> </vsm:VisualStateManager.VisualStateGroups> <Grid.ColumnDefinitions> <ColumnDefinition Width = "Auto"/> <ColumnDefinition Width = "3.61782296696066"/> <ColumnDefinition Width = "Auto"/> </Grid.ColumnDefinitions> <Canvas Height = "50" HorizontalAlignment = "Left" VerticalAlignment = "Top" Width = "50"> <Rectangle Height = "33.746" x:Name = "background" Width = "33.746" Canvas.Left = "8.452" Canvas.Top = "7.88" Fill = "#FFFFFFFF" Stroke = "#FF000000" RadiusX = "5.507" RadiusY = "5.507"/> <Path Height = "40.25" x:Name = "checkPath" Width = "39.75" Opacity = "0" Canvas.Left = "5.959" Canvas.Top = "7.903" Stretch = "Fill" Stroke = "#FF1F9300" StrokeThickness = "3" Data = "M1.5,1.5 C15.495283,8.7014561 27.056604,18.720875 33.75,33.75 M36,3.75 C22.004717,10.951456 10.443395,20.970875 3.7499986,36"/> </Canvas> <ContentPresenter HorizontalAlignment = "Left" Margin = "{TemplateBinding Padding}" VerticalAlignment = "{TemplateBinding VerticalContentAlignment}" Grid.Column = "2" Grid.ColumnSpan = "1" d:LayoutOverrides = "Height"/> </Grid> </ControlTemplate> </UserControl.Resources> <Grid x:Name = "LayoutRoot" Background = "White" > <CheckBox HorizontalAlignment = "Left" Margin = "52.5410003662109,53.5970001220703,0,0" VerticalAlignment = "Top" Template = "{StaticResource CheckBoxControlTemplate1}" Content = "CheckBox"/> </Grid> </UserControl>
编译并执行上述代码后,您将看到以下网页,其中包含一个复选框.
当光标进入复选框区域时,它将改变状态.
单击复选框时,您将看到以下状态.
我们建议您执行上述示例以便更好地理解.