禁用按钮时更改按钮的样式 ( IsEnabled=False )

2023-12-14

我有一个 UWP 问题。当按钮禁用时(IsEnabled=False),如何更改按钮的样式?


Microsoft added the VisualStateManager (known from Silverlight) to the Windows Universal Plattform. Its purpose is to handle the appearance of a control for different states. The "Disabled" state of a button is a good example. The visual states of a control are defined in its ControlTemplate. To customize these States the easiest way is to right click on your button in the designer and choose "Edit Template" then "Edit a Copy...". enter image description here

其作用是将控件的默认模板复制到所选位置。 如果控件是按钮,您将拥有如下样式资源:

<Style x:Key="MyButtonStyle" TargetType="Button">
        <Setter Property="Background" Value="{ThemeResource SystemControlBackgroundBaseLowBrush}"/>
        <Setter Property="Foreground" Value="{ThemeResource SystemControlForegroundBaseHighBrush}"/>
        <Setter Property="BorderBrush" Value="{ThemeResource SystemControlForegroundTransparentBrush}"/>
        <Setter Property="BorderThickness" Value="{ThemeResource ButtonBorderThemeThickness}"/>
        <Setter Property="Padding" Value="8,4,8,4"/>
        <Setter Property="HorizontalAlignment" Value="Left"/>
        <Setter Property="VerticalAlignment" Value="Center"/>
        <Setter Property="FontFamily" Value="{ThemeResource ContentControlThemeFontFamily}"/>
        <Setter Property="FontWeight" Value="Normal"/>
        <Setter Property="FontSize" Value="{ThemeResource ControlContentThemeFontSize}"/>
        <Setter Property="UseSystemFocusVisuals" Value="True"/>
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="Button">
                    <Grid x:Name="RootGrid" Background="{TemplateBinding Background}">
                        <VisualStateManager.VisualStateGroups>
                            <VisualStateGroup x:Name="CommonStates">
                                <VisualState x:Name="Normal"/>
                                <VisualState x:Name="PointerOver">
                                    <Storyboard>
                                        <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="BorderBrush" Storyboard.TargetName="ContentPresenter">
                                            <DiscreteObjectKeyFrame KeyTime="0" Value="{ThemeResource SystemControlHighlightBaseMediumLowBrush}"/>
                                        </ObjectAnimationUsingKeyFrames>
                                        <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="Foreground" Storyboard.TargetName="ContentPresenter">
                                            <DiscreteObjectKeyFrame KeyTime="0" Value="{ThemeResource SystemControlHighlightBaseHighBrush}"/>
                                        </ObjectAnimationUsingKeyFrames>
                                    </Storyboard>
                                </VisualState>
                                <VisualState x:Name="Pressed">
                                    <Storyboard>
                                        <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="Background" Storyboard.TargetName="RootGrid">
                                            <DiscreteObjectKeyFrame KeyTime="0" Value="{ThemeResource SystemControlBackgroundBaseMediumLowBrush}"/>
                                        </ObjectAnimationUsingKeyFrames>
                                        <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="BorderBrush" Storyboard.TargetName="ContentPresenter">
                                            <DiscreteObjectKeyFrame KeyTime="0" Value="{ThemeResource SystemControlHighlightTransparentBrush}"/>
                                        </ObjectAnimationUsingKeyFrames>
                                        <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="Foreground" Storyboard.TargetName="ContentPresenter">
                                            <DiscreteObjectKeyFrame KeyTime="0" Value="{ThemeResource SystemControlHighlightBaseHighBrush}"/>
                                        </ObjectAnimationUsingKeyFrames>
                                        <PointerDownThemeAnimation Storyboard.TargetName="RootGrid"/>
                                    </Storyboard>
                                </VisualState>
                                <VisualState x:Name="Disabled">
                                    <Storyboard>
                                        <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="Background" Storyboard.TargetName="RootGrid">
                                            <DiscreteObjectKeyFrame KeyTime="0" Value="{ThemeResource SystemControlBackgroundBaseLowBrush}"/>
                                        </ObjectAnimationUsingKeyFrames>
                                        <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="Foreground" Storyboard.TargetName="ContentPresenter">
                                            <DiscreteObjectKeyFrame KeyTime="0" Value="{ThemeResource SystemControlDisabledBaseLowBrush}"/>
                                        </ObjectAnimationUsingKeyFrames>
                                        <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="BorderBrush" Storyboard.TargetName="ContentPresenter">
                                            <DiscreteObjectKeyFrame KeyTime="0" Value="{ThemeResource SystemControlDisabledTransparentBrush}"/>
                                        </ObjectAnimationUsingKeyFrames>
                                    </Storyboard>
                                </VisualState>
                            </VisualStateGroup>
                        </VisualStateManager.VisualStateGroups>
                        <ContentPresenter x:Name="ContentPresenter" AutomationProperties.AccessibilityView="Raw" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" ContentTemplate="{TemplateBinding ContentTemplate}" ContentTransitions="{TemplateBinding ContentTransitions}" Content="{TemplateBinding Content}" HorizontalContentAlignment="{TemplateBinding HorizontalContentAlignment}" Padding="{TemplateBinding Padding}" VerticalContentAlignment="{TemplateBinding VerticalContentAlignment}"/>
                    </Grid>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>

现在您可以看到按钮的视觉状态以及与其关联的设置器/动画。您可以手动修改它们,也可以打开 Microsoft Blend 2015。它具有设计视觉状态的强大功能。右键单击按钮,然后单击 Blend 中的“编辑模板”->“编辑当前”,您可以在状态选项卡(左侧)上看到控件的所有状态。

enter image description here

当您选择“禁用”状态时,您将看到设计器窗口周围有一个红色边框。左上角的文字告诉您“禁用状态录制已开启...”。这意味着对控件树中对象的每次更改都会使控件处于“禁用”状态。例如,您可以将 Contentpresenter 的前景色更改为禁用状态。以下动画将添加到您的 XAML 中:

                                            <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="Foreground" Storyboard.TargetName="ContentPresenter">
                                            <DiscreteObjectKeyFrame KeyTime="0">
                                                <DiscreteObjectKeyFrame.Value>
                                                    <SolidColorBrush Color="#FF225EFF"/>
                                                </DiscreteObjectKeyFrame.Value>
                                            </DiscreteObjectKeyFrame>
                                        </ObjectAnimationUsingKeyFrames>

有关 VisualStateManager 以及从哪里开始的更多信息,请查看Visual Studio 2015 中的新 XAML 工具和 Channel9 上的混合视频.

希望这有帮助

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

禁用按钮时更改按钮的样式 ( IsEnabled=False ) 的相关文章

随机推荐

  • Android:FragmentTabHost - java.lang.IllegalArgumentException:您必须指定一种创建选项卡内容的方法

    我在课堂上有以下内容用于创建FragmentTabHost public class TabsActivity extends FragmentActivity private FragmentTabHost mTabHost Overri
  • 双向重复测量ANOVA python函数

    预先感谢您的任何答复 我想在 python 中进行 2 路重复测量方差分析 其中一个 IV 有 5 个级别 其他 4 个级别 有一个 DV 我尝试查看 scipy 文档和一些在线博客 但似乎找不到任何东西 您可以使用rm anovaPino
  • 我希望能够通过网络从 BIOS 提取错误日志

    我希望能够通过网络从 BIOS 提取错误日志 查看 MSDN 中的 Win32 BIOS 我没有看到任何定义错误日志的内容 很乐意在 C 中使用 WMI 来完成此操作 但我愿意接受建议 是否可以 Win32 BIOS 没有包含 BIOS 错
  • 为什么在 PHP 中使用 ORM?

    最近开始学习ORM 突然想到一个问题 PHP 应用程序主要使用 MySql 和 Sqlite 几乎所有 PHP 服务器都安装了它们 那么是否值得在 PHP 中使用 ORM 来独立于数据库呢 性能怎么样 数据库独立性并不是使用 ORM 的主要
  • Jquery 自动完成 - 无结果消息

    我希望自动完成在下拉列表中显示 无结果 如果没有找到结果 则列出 我的情况就像 JQuery 默认示例 function var availableTags ActionScript AppleScript Asp BASIC C C Cl
  • R Shiny:Vtree 图未使用 Shiny 渲染

    如何在闪亮中使用 vtree 包 尝试从服务器端渲染时 没有出现所需的绘图 我的代码 library shiny library vtree Define UI ui lt pageWithSidebar App title headerP
  • ColdFusion、MS Word 文档和希腊字符

    我正在尝试从数据库动态构建 Word 文档 我使用 CFC 进行查询处理 我的努力是成功的 但我只能复制英语文档 但是 我的应用程序使用希腊语 当我尝试构建包含希腊字符的文档时 输出如下所示 我尝试了很多事情但没有任何效果 这里奇怪的是 当
  • 以天:小时:分钟:秒格式计算 SQL 中的 DateDiff

    我目前正在使用 SQL 脚本来计算两个日期之间的差异 这将为我提供 DD HH MI SEC 格式的结果 例子 日期 1 2012 年 7 月 30 日 下午 4 00 日期 2 2012 年 5 月 4 日上午 10 31 结果应该是 8
  • 当 puppet-rspec 存在时,为什么我会收到“类不存在”?

    我设置了一个新的木偶demo模块具有以下内容Gemfile当我运行一个简单的 puppet rspec 测试时 它按预期工作 Gemfile source https rubygems org if puppetversion ENV PU
  • 如何通过纯javascript中的每次点击来选择循环中下一个特定数量的元素?

    我试图为每次单击 下一步 按钮选择接下来的 3 个项目 nextElementSiblings 项目长度为 14 直到项目 12 为止它都工作正常 在项目 12 之后 它检查接下来的 3 个项目 但循环中只剩下 2 个项目 十三个 13 十
  • C++ 中重载运算符->

    我有一个智能指针类 我想重载operator gt 提供它是为了方便 这样我就可以直接访问智能指针中包含的类的成员 我正在研究 Boost 在其中实现该运算符的方式shared ptr模板 我注意到他们添加了一个断言 在返回指针之前检查指针
  • 将绘图映射到 FacetGrid 时的图例问题

    我还在seaborn git repo 中提出了一个问题here 然而 我很可能犯了一些基本错误 而不是错误 但我还没有弄清楚 问题 相同的颜色被分配给图例中的两个不同的值 当我扩展到更多仅包含分配给 色调 的值的子集的图形时 如何防止这种
  • 从检测方法调用自己的类时出现 Java NoClassDefFoundError

    我正在开发一套简单的 Java 代理工具包 以帮助我 也希望其他人 排除 Java 应用程序的故障 我想创建的代理之一是 JComponent getToolTipText 方法 只需将鼠标光标悬停在任何 GUI 类上即可快速识别该类 您可
  • Jackson XML:如何将空/空集合序列化为空节点

    我正在使用 Jackson XML 2 8 9 不幸的是我找不到任何方法将空 空集合序列化为空节点 负责序列化为 XML 的方法 protected byte toXml final Collection
  • 将 Base64 编码的图像上传到 Node.js 服务器不起作用

    我正在使用 MEAN io 并且正在尝试上传 Base64 编码的图像 客户端 AngularJS Image we re going to send it out var base64Image files i var file imag
  • 在应用程序商店中转让 iPhone 应用程序的所有权

    我和我的团队有一个应用程序 我们很快就会将其提交到商店 但我们知道我们将在不久的将来将该应用程序出售给另一家公司 有人有将应用程序的所有权转移到另一个帐户的经验吗 具体来说 当我将应用程序出售给另一家公司时 我们如何将应用程序移至他们的帐户
  • 使用交替和分组结构

    最初我想要一个正则表达式来解析月份数字 首先我想出了以下正则表达式 1 9 1 1 012 它匹配任何正数 表示它匹配该数字的高位数字 即 1 gt 1 2 gt 2 9 gt 9 10 gt 1 19 gt 1 20 gt 2 为什么会这
  • 如何在代码中复制 android:editable="false" ?

    在布局中您可以设置EditText小部件不可通过android editable attribute 我怎样才能在代码中做到这一点 我需要做EditText小部件可根据条件进行编辑 editText setFocusable false e
  • 使用 DomDocument 将实体添加到 DOCTYPE

    我正在尝试创建一个类似于这样的 XML 文档
  • 禁用按钮时更改按钮的样式 ( IsEnabled=False )

    我有一个 UWP 问题 当按钮禁用时 IsEnabled False 如何更改按钮的样式 Microsoft added the VisualStateManager known from Silverlight to the Window