为 Win 8 Metro 控件编写自定义控件模板 (XAML) 时,我们需要使用 VisualStateManager 根据 VisualState 转换更新控件。 我在 MSDN 上看到下面的示例,但我找不到 VisualStateGroup "CommonStates" 的记录位置,以及除了"PointerOver"和"Normal"之外还定义了哪些其他 VisualState? 您是否必须深入研究SDK才能找到按钮的默认控件模板? 如果是,在哪里?
<ControlTemplate TargetType="Button">
<Grid >
<VisualStateManager.VisualStateGroups>
<VisualStateGroup x:Name="CommonStates">
<VisualStateGroup.Transitions>
<!--Take one half second to transition to the PointerOver state.-->
<VisualTransition To="PointerOver"
GeneratedDuration="0:0:0.5"/>
</VisualStateGroup.Transitions>
<VisualState x:Name="Normal" />
<!--Change the SolidColorBrush, ButtonBrush, to red when the
Pointer is over the button.-->
<VisualState x:Name="PointerOver">
<Storyboard>
<ColorAnimation Storyboard.TargetName="ButtonBrush"
Storyboard.TargetProperty="Color" To="Red" />
</Storyboard>
</VisualState>
</VisualStateGroup>
</VisualStateManager.VisualStateGroups>
<Grid.Background>
<SolidColorBrush x:Name="ButtonBrush" Color="Green"/>
</Grid.Background>
</Grid>
</ControlTemplate>
您可以转到 xaml 文件的设计视图,并在选中按钮控件的情况下 - 右键单击/编辑模板/编辑当前 - 将提取默认模板。通常,控件应使用指示应在模板中使用哪些视觉状态的属性进行批注,如下所示,但是当我导航到像 Button 这样的控件的定义时,我看不到它们。
[TemplateVisualState(GroupName="CommonStates", Name="Normal")]
[TemplateVisualState(GroupName="CommonStates", Name="PointerOver")]