简体   繁体   中英

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.

Thank you decoherence! What I needed was the following:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM