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;
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top