11

I have this:

var MyText = new TextBlock();
MyText.Text = "blah";
MyText.Style = /* ??? */;

In XAML, I can set a style like this:

<TextBlock Text="blah" Style="{ThemeResource ListViewItemTextBlockStyle}"/>

But how do I do that in C#?

EDIT:

Error   1   'Windows.UI.Xaml.Application' does not contain a definition for 'FindResource' and no extension method 'FindResource' accepting a first argument of type 'Windows.UI.Xaml.Application' could be found (are you missing a using directive or an assembly reference?)
Error   1   'Geodropper.HubPage' does not contain a definition for 'FindResource' and no extension method 'FindResource' accepting a first argument of type 'Geodropper.HubPage' could be found (are you missing a using directive or an assembly reference?)

When I tried (Style)this.FindResource("ListViewItemTextBlockStyle"); and (Style)App.Current.FindResource("ListViewItemTextBlockStyle") I got these errors.

Cœur
  • 37,241
  • 25
  • 195
  • 267
Zudo
  • 481
  • 3
  • 19
  • 1
    something like `MyText.Style = (Style)this.FindResource("ListViewItemTextBlockStyle");` where this is your window or the element containing the resource or `(Style)App.Current.FindResource("ListViewItemTextBlockStyle")` – Xi Sigma Apr 25 '15 at 18:56
  • http://stackoverflow.com/questions/1729368/creating-a-style-in-code-behind and http://stackoverflow.com/questions/10686917/setting-the-style-property-of-a-wpf-label-in-code – Eugene Podskal Apr 25 '15 at 18:59
  • @decoherence neither of those worked for me. – Zudo Apr 25 '15 at 19:04
  • 1
    @ZudoMC sorry thought you were using WPF, otherwise `(Style)App.Current.Resources["ListViewItemTextBlockStyle"]` or `(Style)this.Resources["ListViewItemTextBlockStyle"]` – Xi Sigma Apr 25 '15 at 19:11

1 Answers1

17

Thank you decoherence! What I needed was the following:

var MyText = new TextBlock();
MyText.Text = drop;
MyText.Style = (Style)Application.Current.Resources["ListViewItemTextBlockStyle"];
Zudo
  • 481
  • 3
  • 19