簡體   English   中英

如何在文件(xaml.cs)后面的代碼之外訪問樣式?

[英]How to access a Style outside the code behind file (xaml.cs)?

在后面的代碼( filename.xaml.cs文件)中,我可以成功訪問靜態資源,如下所示:

TextBlock elm = new TextBlock();
elm.Style = (Style)this.Resources["myStyle"];

Styles.xaml添加到filename.xaml方式如下:

<Page.Resources>
    <ResourceDictionary Source="resources/Styles.xaml" />
</Page.Resources>

但是, this.Resources["myStyle"]在未與任何.xaml文件關聯的.cs文件中不起作用。 在這種情況下如何訪問Style.xaml?

您應該使用FindResource

this用作FrameworkElement

elm.Style = (Style)this.FindResource("myStyle");

或在Application

elm.Style = (Style)Application.Current.FindResource("myStyle");

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM