以编程方式设置 TextBlock 的样式
Set style of TextBlock programmatically
我有这个:
var MyText = new TextBlock();
MyText.Text = "blah";
MyText.Style = /* ??? */;
在XAML中,我可以这样设置样式:
<TextBlock Text="blah" Style="{ThemeResource ListViewItemTextBlockStyle}"/>
但是我如何在 C# 中做到这一点?
编辑:
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?)
当我尝试 (Style)this.FindResource("ListViewItemTextBlockStyle");
和 (Style)App.Current.FindResource("ListViewItemTextBlockStyle")
时,我遇到了这些错误。
谢谢解相干!我需要的是以下内容:
var MyText = new TextBlock();
MyText.Text = drop;
MyText.Style = (Style)Application.Current.Resources["ListViewItemTextBlockStyle"];
我有这个:
var MyText = new TextBlock();
MyText.Text = "blah";
MyText.Style = /* ??? */;
在XAML中,我可以这样设置样式:
<TextBlock Text="blah" Style="{ThemeResource ListViewItemTextBlockStyle}"/>
但是我如何在 C# 中做到这一点?
编辑:
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?)
当我尝试 (Style)this.FindResource("ListViewItemTextBlockStyle");
和 (Style)App.Current.FindResource("ListViewItemTextBlockStyle")
时,我遇到了这些错误。
谢谢解相干!我需要的是以下内容:
var MyText = new TextBlock();
MyText.Text = drop;
MyText.Style = (Style)Application.Current.Resources["ListViewItemTextBlockStyle"];