How to assign a dynamic resource style in code?
Asked Answered
V

5

77

I want to produce in code the equivalent of this in XAML:

<TextBlock
Text="Title:"
Width="{Binding FormLabelColumnWidth}"
Style="{DynamicResource FormLabelStyle}"/>

I can do the text and the width, but how do I assign the dynamic resource to the style:

TextBlock tb = new TextBlock();
            tb.Text = "Title:";
            tb.Width = FormLabelColumnWidth;
            tb.Style = ???
Vulcanize answered 18/11, 2009 at 9:4 Comment(1)
as others here have pointed out, the answer you marked as correct doesn't actually do what the question poses. There is another answer here that is. You should consider changing your answer accordingly because some people read the marked answer and nothing else and they will miss the correct info, which is what SO is all about.Gipson
Q
40

You can try:

tb.Style = (Style)FindResource("FormLabelStyle");

Enjoy!

Quacksalver answered 18/11, 2009 at 9:10 Comment(2)
The answer von Samuel is better, since it is the equivalent of {DynamicResource} in XAML, while your answer changes tb.Style to the current value of the resource "FormLabelStyle". tb.Style does not change when the resource "FormLabelStyle" changes, though.Isopleth
This is better if you are sure the resource will not change after this assignment.Delapaz
B
222

You should use FrameworkElement.SetResourceReference if you want true DynamicResource behaviour - ie updating of the target element when the resource changes.

tb.SetResourceReference(Control.StyleProperty, "FormLabelStyle")
Bravissimo answered 18/11, 2009 at 9:13 Comment(1)
If you're dealing with nested dictionaries this is definitely the way to go!Benitobenjamen
Q
40

You can try:

tb.Style = (Style)FindResource("FormLabelStyle");

Enjoy!

Quacksalver answered 18/11, 2009 at 9:10 Comment(2)
The answer von Samuel is better, since it is the equivalent of {DynamicResource} in XAML, while your answer changes tb.Style to the current value of the resource "FormLabelStyle". tb.Style does not change when the resource "FormLabelStyle" changes, though.Isopleth
This is better if you are sure the resource will not change after this assignment.Delapaz
E
13

The original question was how to make it Dynamic, which means if the resource changes the control will update. The best answer above used SetResourceReference. For the Xamarin framework this is not available but SetDynamicResource is and it does exactly what the original poster was asking. Simple example

        Label title = new Label();
        title.Text = "Title";
        title.SetDynamicResource(Label.TextColorProperty, "textColor");
        title.SetDynamicResource(Label.BackgroundColorProperty, "backgroundColor");

Now calling:

        App.Current.Resources["textColor"] = Color.AliceBlue;
        App.Current.Resources["backgroundColor"] = Color.BlueViolet;

Causes the properties to change for all controls using the resource this way. This should work for any property.

Eberly answered 24/11, 2018 at 23:36 Comment(0)
P
3

This should work:

tb.SetValue(Control.StyleProperty, "FormLabelStyle");
Plumcot answered 18/11, 2009 at 9:8 Comment(3)
What's the difference between this method and SetResourceReference?Glycosuria
This is equivalent to tb.Style = "FormLabelStyle". This is what the CLR wrapper does internally.Energy
This throws an ArgumentException: 'FormLabelStyle' is not a valid value for property 'Style'. If you use the equivalent tb.Style = "FormLabelStyle";, the semantics are the same (the CLR wrapper property calls the same code internally), but it fails in compile time because of strict type checking (which is good).Energy
C
2

Application.Current.Resources.TryGetValue("ResourceKey", out var value)

Clift answered 16/1, 2020 at 5:27 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.