75

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 = ???
Dave Clemmer
  • 3,741
  • 12
  • 49
  • 72
Edward Tanguay
  • 189,012
  • 314
  • 712
  • 1,047
  • 9
    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. – Mark A. Donohoe Sep 27 '15 at 08:02

5 Answers5

220

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")
John Cummings
  • 1,949
  • 3
  • 22
  • 38
Samuel Jack
  • 32,712
  • 16
  • 118
  • 155
38

You can try:

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

Enjoy!

Alastair Pitts
  • 19,423
  • 9
  • 68
  • 97
  • 34
    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. – Daniel Albuschat Jul 05 '13 at 07:23
  • 2
    This is better if you are sure the resource will not change after this assignment. – Huseyin Yagli Mar 26 '18 at 08:42
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.

user9220597
  • 157
  • 1
  • 2
3

This should work:

tb.SetValue(Control.StyleProperty, "FormLabelStyle");
robert.oh.
  • 644
  • 2
  • 5
  • 13
  • 4
    What's the difference between this method and `SetResourceReference`? – SepehrM Apr 30 '14 at 15:22
  • This is equivalent to `tb.Style = "FormLabelStyle"`. This is what the CLR wrapper does internally. – Palec Dec 11 '16 at 11:42
  • 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). – Palec Dec 11 '16 at 12:37
2

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

sharkbait
  • 2,980
  • 16
  • 51
  • 89
Abdul Gani
  • 679
  • 3
  • 7