【问题标题】:Set style of TextBlock programmatically以编程方式设置 TextBlock 的样式
【发布时间】:2015-07-04 08:16:06
【问题描述】:

我有这个:

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") 时,我得到了这些错误。

【问题讨论】:

  • 类似MyText.Style = (Style)this.FindResource("ListViewItemTextBlockStyle");,这是您的窗口或包含资源的元素或(Style)App.Current.FindResource("ListViewItemTextBlockStyle")
  • @decoherence 这些都不适合我。
  • @ZudoMC 抱歉以为您使用的是 WPF,否则 (Style)App.Current.Resources["ListViewItemTextBlockStyle"](Style)this.Resources["ListViewItemTextBlockStyle"]
  • @decoherence 谢谢,成功了!

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


【解决方案1】:

感谢退相干!我需要的是以下内容:

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

【讨论】:

    猜你喜欢
    • 2012-09-25
    • 2017-03-11
    • 2012-04-12
    • 2011-03-09
    • 2020-10-06
    • 2014-09-20
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多