从代码更改样式(在ResourceDictionary中)

阿兰392

我有这个ResourceDictionary

<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">

<Style x:Key="MainMenuLabelStyle" TargetType="{x:Type TextBlock}">

    <Style.Triggers>
        <Trigger Property ="IsMouseOver" Value="True">
            <Setter Property= "Foreground" Value="White"/>
            <Setter Property= "FontSize" Value="18"/>
            <Setter Property= "FontFamily" Value="Arial"/>
        </Trigger>
    </Style.Triggers>

</Style>

如果我想更改字体大小或颜色,该怎么办?此代码无效。

 Application.Current.Resources("MainMenuLabelStyle") = 25

这是xaml

  <TextBlock Text="Uscita" Grid.Row="1" Grid.Column="1"  TextAlignment="Left"  Margin="4" TextWrapping="Wrap" Style="{DynamicResource MainMenuLabelStyle}">
维克

在WPF应用程序中首次使用样式之前,出于性能原因将其密封,无法再对其进行修改。您可以在MSDN上阅读它

因此,如果要更改样式,则必须选择。第一个(最简单的方法)是声明所需的样式,并将它们放入您的中ResourceDictionary

第二种解决方案是考虑aSetter是a DependencyObject,因此可以绑定其依赖项属性。在这种情况下,您的样式将变为:

<Style x:Key="MainMenuLabelStyle" TargetType="{x:Type TextBlock}">
    <Style.Triggers>
        <Trigger Property="IsMouseOver" Value="True">
            <Setter Property="Foreground" Value="{Binding RelativeSource={RelativeSource Mode=Self}, Path=Tag.Foreground, TargetNullValue=Red, FallbackValue=Red}" />
            <Setter Property="FontSize" Value="{Binding RelativeSource={RelativeSource Mode=Self}, Path=Tag.FontSize, TargetNullValue=18, FallbackValue=18}" />
            <Setter Property="FontFamily" Value="{Binding RelativeSource={RelativeSource Mode=Self}, Path=Tag.FontFamily, TargetNullValue=Arial, FallbackValue=Arial}" />
        </Trigger>
    </Style.Triggers>
</Style>

现在,您只需设置Tag每个TextBlock控件属性即可更改样式

<StackPanel>
    <TextBlock Text="Uscita" TextAlignment="Left" Margin="4" TextWrapping="Wrap" Style="{DynamicResource MainMenuLabelStyle}" />
    <TextBlock Text="Uscita" TextAlignment="Left"  Margin="4" TextWrapping="Wrap" Style="{DynamicResource MainMenuLabelStyle}">
        <TextBlock.Tag>
            <local:StyleConfig FontSize="50" FontFamily="Tahoma" Foreground="Orange" />
        </TextBlock.Tag>
    </TextBlock>
</StackPanel>

如您所见,第一个TextBlock将使用声明的样式。另一方面,第二个TextBlock将使用原始样式的修改版本。

当然,为了使此选项正常工作,您必须创建一个类(StyleConfig在我的示例中),该类可能是这样的:

public class StyleConfig
{
    public string Foreground { get; set; }
    public string FontSize { get; set; }
    public string FontFamily { get; set; }
}

希望对您有所帮助。

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

从代码更改样式(在ResourceDictionary中)

来自分类Dev

从代码后面更改样式

来自分类Dev

在Word中更改样式

来自分类Dev

在ModelView中更改样式(MVVM + WPF)

来自分类Dev

从javascript更改样式

来自分类Dev

根据Angular 8中的RouterURL更改样式

来自分类Dev

如果在cshtml中更改样式

来自分类Dev

我可以在react js包中更改样式吗?

来自分类Dev

如何在NuxtJS中更改样式的加载顺序?

来自分类Dev

无法使用 VBA 在 Word 中更改样式

来自分类Dev

以编程方式更改样式

来自分类Dev

javascript onclick更改样式

来自分类Dev

如何在后台代码中向ResourceDictionary添加样式

来自分类Dev

WPF:由于只读状态,无法通过隐藏的代码更改样式 SolidColorBrush

来自分类Dev

使用JavaScript更改样式表中没有的类样式(不是单个元素)

来自分类Dev

在WPF中更改样式时,情节提要板不会更改

来自分类Dev

AngularJs:单击div时更改样式

来自分类Dev

如何使用AngularJS更改样式?

来自分类Dev

Notepad ++更改样式控制台

来自分类Dev

ACF Google Map-更改样式?

来自分类Dev

mytoolkit:FixedHtmlBlock更改样式,xaml

来自分类Dev

用:after用jquery更改样式

来自分类Dev

angularjs:动态更改样式失败

来自分类Dev

在滚动上反应NavBar的更改样式

来自分类Dev

如何防止Safari更改样式?

来自分类Dev

JavaScript click事件未更改样式

来自分类Dev

更改样式表的paintEvent颜色?

来自分类Dev

元素的动态更改样式(DomIcon)

来自分类Dev

根据暗或亮模式更改样式