[英]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.