I have a custom look-less control derived from Control class. It's template is defined in Generic.xaml file. Now I want to add some UI stuff to it (mostly brushes) and want to do that in separate resource dictionary, and then access those stuff from the code-behind feil (*.xaml.cs). See bellow: Generic.xaml (fragment):
<ResourceDictionary
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:PTE.Controls" xmlns:sys="clr-namespace:System;assembly=mscorlib">
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="PTE.Controls;component/Resources/CategoriesColors.xaml"/>
</ResourceDictionary.MergedDictionaries>
<CornerRadius x:Key="elementCornerBorder" BottomLeft="2" BottomRight="2" TopLeft="2" TopRight="2"/>
<Style TargetType="{x:Type local:Element}">
<Setter Property="Canvas.ZIndex" Value="50"/>
<Setter Property="MinWidth" Value="60"/>
<Setter Property="Template">...
CategoriesColors.xaml (fragment):
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<LinearGradientBrush StartPoint="0,1" EndPoint="1,0" x:Key="categoryNobleGas">
<GradientStop Color="White" Offset="0"/>
<GradientStop Color="RoyalBlue" Offset="0.5"/>
<GradientStop Color="SteelBlue" Offset="1"/>
</LinearGradientBrush>...
Server side part (fragment):
private static void OnCategoryNameChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
var categoryName = (CategoryName)e.NewValue;
var element = (Element) d;
switch (categoryName)
{
case CategoryName.NobleGas:
element.Background = (Brush)element.TryFindResource("categoryNobleGas");
break;
case CategoryName.Halogen:
element.Background = (Brush)element.TryFindResource("categoryHalogen");
break;
case CategoryName.OtherNonmetal:
element.Background = (Brush)element.TryFindResource("categoryOtherNonmetal");
break;
case CategoryName.Metalloid:
It doesn't work. Basically TryFindResource method always returns null. Any ideas how to make these stuff work together? Thank you!
UPD: If I add following line in the constructor of the control it works:
this.Resources = Application.LoadComponent(new Uri("PTE.Controls;Component/Resources/CategoriesColors.xaml", UriKind.Relative)) as ResourceDictionary;
But first, it will duplicate dictionary (load new one every time) and consume considerable more memory. Second, I really want to do it in XAML.