Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Change Style from code behind

Tags:

wpf

xaml

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

like image 841
Alan392 Avatar asked Dec 09 '15 10:12

Alan392


2 Answers

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.

like image 113
Mike Eason Avatar answered Nov 15 '22 06:11

Mike Eason


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;
like image 35
Giangregorio Avatar answered Nov 15 '22 06:11

Giangregorio