简体   繁体   中英

How to use XAML resource of SolidColorBrush in C#?

I am defining resources in my App.XAML file:

 <SolidColorBrush x:Key="ActivePanelBackgBrush" Color="#FF77FF83"/>

 <SolidColorBrush x:Key="NonActivePanelBackgBrush" Color="#FFFF7777"/>

In my C# code I would like to set the background of a Grid to that color. How do I do that?

Thx

You can get objects from Resources in App.xaml like that:

 var brush = Application.Current.Resources["NonActivePanelBackgBrush"] as SolidColorBrush;

And use it where you want:

 Grid1.Background = brush;

Alternatively you can use FindResource . However, WinRT seemed to be missing the FindResource function which is familiar from WPF. You can use this extension method.( sadly I have not tested it yet)

Grid1.Background = FindResource("NonActivePanelBackgBrush") as SolidColorBrush;

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