user3745735
user3745735

Reputation:

Set style of TextBlock programmatically

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.

Upvotes: 11

Views: 5837

Answers (1)

user3745735
user3745735

Reputation:

Thank you decoherence! What I needed was the following:

var MyText = new TextBlock();
MyText.Text = drop;
MyText.Style = (Style)Application.Current.Resources["ListViewItemTextBlockStyle"];

Upvotes: 17

Related Questions