简体   繁体   中英

WPF: Accessing a style of ResourceDictionary from inside of usercontrol

Normally, to set style using C#, I write,

btn.Style = (Style)FindResource(_styleName);

But what to do, if style is defined inside main 'ResourceDictionary', and button is inside some 'UserControl'. Also, I need to write script inside 'UserControl' only.

To make it more clear - There is a button inside UserControl which should take style defined in main ResourceDictionary. It should happen on some event of usercontrol, so I need to write CS in usercontrol.xaml.cs. It is quite easy if style is defined in usercontrol.xaml itself.

if it is inside the Application resources

SolidColorBrush res = (SolidColorBrush) Application.Current.FindResource("YellowSolidBrush");

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM