wpf - 向初学者解释如何使用 VisualStateManager

标签 wpf xaml windows-8 microsoft-metro

我对 Xaml/WPF 知之甚少。我正在尝试设计一个自定义 Style在 Windows 8 Metro 应用程序中用于 Button它具有洋红色背景,按下时具有蓝色背景。我知道我需要使用 VisualStateManager但我在网上找不到任何对这个主题知之甚少的人有意义的东西。有很多假设的知识。这是我到目前为止所得到的:

<Style x:Name="test" TargetType="Button">
        <Setter Property="Background" Value="Magenta"/>
        <Setter Property="Content" Value="Test style" />
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="Button">
                    <Grid>                          
                        <VisualStateManager.VisualStateGroups>
                            <VisualStateGroup x:Name="CommonStates">
                                <VisualState x:Name="Pressed">
                                    <Storyboard>
                                        <turn the background blue>
                                    </Storyboard>
                                </VisualState>
                            </VisualStateGroup>
                        </VisualStateManager.VisualStateGroups>
                    </Grid>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>

这段代码可能非常错误,但正如我所说,我一直试图将一些我不理解的信息拼凑在一起以产生结果。

感谢您的时间

最佳答案

作为初学者,甚至不要费心尝试手工编写它。 WPF(触发器)的原始机制很容易手工编写,但旨在在较新的 XAML 平台上替代它的 VSM 更加冗长,并且专为在设计工具中使用而设计。

为了省去很多麻烦,只需在 Blend 中进行编辑,然后您可以查看它生成的 XAML,如果您想了解更多细节以便将来手动编辑。 Blend 体验非常简单 - 在“状态”面板中选择要编辑的状态,您将看到它的预览。您在选择该状态时所做的任何更改都将作为该状态上的 Storyboard应用。

关于wpf - 向初学者解释如何使用 VisualStateManager,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23657581/

相关文章:

c# - ComboBox 打开时 KeyDown 事件不起作用?

WPF 无法加载文件或程序集或其依赖项之一

C# - 如何覆盖文本框的 "Up arrow"和 "Down arrow"操作?

wpf - 关闭抗锯齿以在 WPF 中绘制位图

c# - 如何将图像放置在其中心而不是左上角?

c# - WPF 的快捷键

c# - Windows RT 中的自定义触摸键盘

JavaScript 运行时错误 : Unable to add dynamic content

c# - 当应用程序在 Windows 8 XAML/C# 应用程序中有超过 1 个页面时出现 SuspensionManager 错误

wpf - UI 未调用 INotifyDataErrorInfo.GetErrors()