How to style a dynamic resource in code?

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

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

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

 TextBlock tb = new TextBlock(); tb.Text = "Title:"; tb.Width = FormLabelColumnWidth; tb.Style = ??? 
+44
c # styles wpf xaml code-behind
Nov 18 '09 at 9:04
source share
3 answers

You can try:

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

Enjoy it!

+28
Nov 18 '09 at 9:10
source share
— -

You should use FrameworkElement.SetResourceReference if you need the true DynamicResource behavior - that is, updating the target element when the resource changes.

 tb.SetResourceReference(Control.StyleProperty, "FormLabelStyle") 
+137
Nov 18 '09 at 9:13
source share

This should work:

 tb.SetValue(Control.StyleProperty, "FormLabelStyle"); 
+4
Nov 18 '09 at 9:08
source share



All Articles