Change code style

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 value of the Setter property value from the code behind, how can I do this?

In the code behind, I would like something like this:

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

I should use only framework 4.

thanks

+5
source share
3 answers

Giangregorio has covered most of the reason this cannot be achieved directly. However, here is the solution:

You can use DynamicResource links in your Setters style, and then when you need to change the style, you just update the resource , not the style. This probably makes 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. Because Foreground properties reference DynamicResource , whenever a resource changes, it updates the Style value. All you have to do in the code is change the value of the resource.

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

DynamicResource takes care of the rest.

+2
source

You cannot 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 haven't used it yet, you can do something like this (using the index to make a quick 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; 
0
source

This is my last 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 for

  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

0
source

Source: https://habr.com/ru/post/1237741/


All Articles