2015-04-25 13 views
5

mam to:Ustaw styl TextBlock programowo

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

w XAML, mogę ustawić styl takiego:

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

Ale jak mam to zrobić w 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?) 

Kiedy próbowałem (Style)this.FindResource("ListViewItemTextBlockStyle"); i (Style)App.Current.FindResource("ListViewItemTextBlockStyle") mam te błędy.

+1

coś takiego jak 'MyText.Style = (Style) this.FindResource ("ListViewItemTextBlockStyle");' gdzie jest to Twoja okno lub element zawierający zasób lub '(styl) App .Current.FindResource ("ListViewItemTextBlockStyle") ' –

+0

http://stackoverflow.com/questions/1729368/creating-a-style-in-code-behind i http://stackoverflow.com/questions/10686917/setting-the -style-property-of-a-wpf-label-in-code –

+0

@decoherence żadne z tych nie zadziałało dla mnie. – Zudo

Odpowiedz

10

Dziękuję kserokopii! Co potrzebne było następujące:

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