xaml - 以编程方式更改StandardStyles

时间:2013-02-19 16:05:11

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

我正在使用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属性以编程方式更改为“黑色”?

1 个答案:

答案 0 :(得分:1)

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

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

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

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