【问题标题】:xaml - change StandardStyles programmaticallyxaml - 以编程方式更改 StandardStyles
【发布时间】:2013-02-19 16:05:10
【问题描述】:

我正在使用 xaml/c# 开发 Windows 8 应用程序。 我需要更改PageHeaderTextStyle(在 StandardStyles.xaml 中定义)。

这是我的PageHeaderTextStyle 定义:

<Style x:Key="PageHeaderTextStyle" TargetType="TextBlock" BasedOn="{StaticResource HeaderTextStyle}">
    <Setter Property="TextWrapping" Value="NoWrap"/>
    <Setter Property="VerticalAlignment" Value="Bottom"/>
    <Setter Property="Margin" Value="0,0,30,40"/>
    <Setter Property="Foreground" Value="White"/>
</Style>

如何以编程方式将Foreground 属性更改为“黑色”?

【问题讨论】:

    标签: c# xaml windows-8 windows-store-apps windows-store


    【解决方案1】:

    您确定需要以编程方式更改它吗?

    您可以通过在 XAML 中包含如下代码来正常加载样式。您可以通过更改“BasedOn”属性来扩展现有的 PageHeaderTextStyle 定义。现在“myPageHeaderTextStyle”将与 PageHeaderTextStyle 完全一样,只有您指定的更改。

    <Style x:Key="myPageHeaderTextStyle" TargetType="TextBlock" BasedOn="{StaticResource PageHeaderTextStyle}">
        <Setter Property="Foreground" Value="Black"/>
    </Style>
    

    如果您确实需要以编程方式更改它,您可以在 Style 类上使用 SetValue(这是您的“myPageHeaderTextStyle”对象)。

    【讨论】:

    • 感谢您的合格回答。
    • 稍微解释一下为什么我需要这样做:我想构建一个类似于 Bing Travel 应用程序的应用程序,该应用程序预装在 Windows 8 上。在这个应用程序中,您有一个固定的 Header 和一个大页面左侧的图像。由于标题位于图像上方,因此 headerText-color 为白色。如果用户水平向右移动,并且一旦 headerText 离开图像,headerText-color 就会改变(以确保对比度)。我想“重建”这种效果,我看到的唯一解决方案是以编程方式更改样式。如果有一个“更简单”的解决方案,我会很高兴!
    猜你喜欢
    • 1970-01-01
    • 2016-05-07
    • 1970-01-01
    • 2016-08-08
    • 1970-01-01
    • 1970-01-01
    • 2019-04-10
    • 2015-08-16
    • 2012-10-04
    相关资源
    最近更新 更多