从代码背后改变样式

时间:2015-12-09 10:21:53

标签: wpf xaml

我有这种风格

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

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

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

        <Trigger Property="IsMouseOver"  Value="False">
            <Setter Property= "Foreground" Value="Black" />
            <Setter Property= "FontSize" Value="14"/>
            <Setter Property= "FontFamily" Value="Verdana"/>
        </Trigger>

    </Style.Triggers>

</Style>

现在,如果我想从代码中更改 Setter属性值,我该怎么办?

在背后的代码中,我想要这样的东西:

MainMenuStyle.IsMouseOver(True).Foreground = "Red"
MainMenuStyle.IsMouseOver(True).FontSize = 10

MainMenuStyle.IsMouseOver(False).Foreground = "Green"
MainMenuStyle.IsMouseOver(False).FontSize = 100

我必须只使用框架4.

谢谢

3 个答案:

答案 0 :(得分:2)

Giangregorio已经涵盖了直接无法实现这一目标的大部分原因。但是,这是一个解决方案:

您可以在样式message中使用DynamicResource引用,然后当您需要更改样式时,只需更新资源,而不是样式。通过一个例子,这可能更有意义:

Setters

因此。由于<!-- Colour Resources --> <SolidColorBrush x:Key="BlueBrush" Color="Blue"/> <SolidColorBrush x:Key="RedBrush" Color="Red"/> <!-- TextBlock Style (References the colour resources) --> <Style x:Key="MainMenuStyle" TargetType="{x:Type TextBlock}"> <Style.Triggers> <Trigger Property="IsMouseOver" Value="True"> <Setter Property= "Foreground" Value="{DynamicResource BlueBrush}"/> ... </Trigger> <Trigger Property="IsMouseOver" Value="False"> <Setter Property= "Foreground" Value="{DynamicResource RedBrush}" /> ... </Trigger> </Style.Triggers> </Style> 属性引用了Foreground,每当资源发生变化时,它都会更新DynamicResource。您需要在代码中执行的操作是更改资源值。

Style

App.Current.Resources["BlueBrush"] = new SolidColorBrush(Colors.Pink); 会照顾其余部分。

答案 1 :(得分:0)

首次使用后,您无法通过MSDN更改样式:

  

当另一种风格基于它或何时风格被密封   第一次申请。

在你的情况下,我可能会在XAML中定义另一种样式并在运行时切换它们。

否则,如果你还没有使用它,你可以做这样的事情(使用索引做一个快速的例子)

  Style style = this.Resources["MainMenuStyle"] as Style;
  ((Trigger)style.Triggers[0]).Setters[0] = new Setter(TextBlock.ForegroundProperty, new SolidColorBrush(Colors.Green));
  yourControl.Style = style;

答案 2 :(得分:0)

这是我的最终代码。

<!-- Colour Resources -->
<!-- Default values -->

<SolidColorBrush x:Key="MenuItem_Select_Color" Color="Blue"/>
<FontFamily x:Key="MenuItem_Select_Font">Calibri</FontFamily>
<sys:Double x:Key="MenuItem_Select_Font_Size">13</sys:Double>

<SolidColorBrush x:Key="MenuItem_UnSelect_Color" Color="Green"/>
<FontFamily x:Key="MenuItem_UnSelect_Font">Arial Black</FontFamily>
<sys:Double x:Key="MenuItem_UnSelect_Font_Size">12</sys:Double>



<!-- TextBlock Style (References the colour resources) -->
<Style x:Key="MainMenuStyle" TargetType="{x:Type TextBlock}">

    <Style.Triggers>
        <Trigger Property="IsMouseOver"  Value="True">
            <Setter Property= "Foreground" Value="{DynamicResource MenuItem_Select_Color}"/>
            <Setter Property= "FontFamily" Value="{DynamicResource MenuItem_Select_Font}"/>
            <Setter Property= "FontSize" Value="{DynamicResource MenuItem_Select_Font_Size}"/>
        </Trigger>

        <Trigger Property="IsMouseOver"  Value="False">
            <Setter Property= "Foreground" Value="{DynamicResource MenuItem_UnSelect_Color}"/>
            <Setter Property= "FontFamily" Value="{DynamicResource MenuItem_UnSelect_Font}"/>
            <Setter Property= "FontSize" Value="{DynamicResource MenuItem_UnSelect_Font_Size}"/>                
        </Trigger>

    </Style.Triggers>

</Style>

背后的代码

        Application.Current.Resources("MenuItem_Select_Color") = New SolidColorBrush(Colors.DarkBlue)
        Application.Current.Resources("MenuItem_UnSelect_Color") = New SolidColorBrush(Colors.Gold)

        Application.Current.Resources("MenuItem_Select_Font") = New FontFamily("Broadway")
        Application.Current.Resources("MenuItem_UnSelect_Font") = New FontFamily("Lucida Console")

        Application.Current.Resources("MenuItem_Select_Font_Size") = 20
        Application.Current.Resources("MenuItem_UnSelect_Font_Size") = 30

再见

相关问题