c# - 以编程方式设置 TextBlock 的样式

标签 c# visual-studio xaml windows-phone-8.1

我有这个:

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"];

关于c# - 以编程方式设置 TextBlock 的样式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29869286/

相关文章:

c# - 在 xaml 中创建选项卡模板

c# - 无法更改 javascript 中设置的 cookie 值?

c# - 在 C# 中使用 UPDATE MySQL 命令

c# - CRM 2011 Silverlight 应用程序 - 无法将项目添加到 DataGridTemplateColumn 单元格内的组合框

c# - 在桌面和 Surface (PixelSense) 1.0 上运行的 WPF 应用程序的约定

c# - WebApi 路由标识与同一 URL 匹配的多个 Controller 类型。怎么解决?

c - 自动转换 C 代码

c# - 如何更改 CodeMaid 添加的默认访问修饰符

c# - 如何为数据绑定(bind)组合框预定义组合框项?

wpf - 为什么我的 xaml 只显示一个没有任何内容的普通窗口