Alan392
Alan392

Reputation: 685

Change Style from code behind

I have this style

<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>

Now, if I want to change the Setter Property Value from code behind how can I do it ?

In code behind I'd want something like this:

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

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

I must use only framework 4.

Thank you

Upvotes: 6

Views: 5285

Answers (3)

Alan392
Alan392

Reputation: 685

This is my final code.

<!-- 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>

Code behind

        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

Bye

Upvotes: 0

Mike Eason
Mike Eason

Reputation: 9713

Giangregorio has covered most of the reason why this can't be achieved directly. However, here's a solution:

You can use DynamicResource references in your style's Setters, then when you need to change the style, you simply update the resource, instead of the style. This would probably make more sense with an example:

<!-- 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>

So. As the Foreground properties reference a DynamicResource, whenever the resource changes, it'll update the Style. All you need to do in code is change the resource value.

App.Current.Resources["BlueBrush"] = new SolidColorBrush(Colors.Pink);

The DynamicResource will take care of the rest.

Upvotes: 4

Giangregorio
Giangregorio

Reputation: 1510

You can't change your style after its first use, from MSDN:

A style is sealed when another style is based on it or when it is applied for the first time.

In your case i will probably define another style in XAML and switch them at runtime.

Otherwise if you hadn't used it yet, you can do something like this (using index to make a fast example)

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

Upvotes: 1

Related Questions