Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 284
Zudo Avatar asked Apr 25 '15 18:04

Zudo


1 Answers

Thank you decoherence! What I needed was the following:

var MyText = new TextBlock();
MyText.Text = drop;
MyText.Style = (Style)Application.Current.Resources["ListViewItemTextBlockStyle"];
like image 99
Zudo Avatar answered Oct 06 '22 17:10

Zudo