I have a custom look derived from the Control class. Its template is defined in the Generic.xaml file. Now I want to add some elements of the user interface to it (mostly brushes) and I want to do this in a separate resource dictionary, and then access these materials from the feil-code (* .xaml.cs). See below: Generic.xaml (snippet):
<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>...
Part (fragment) on the server side:
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:
This does not work. In principle, the TryFindResource method always returns null. Any ideas how to make these things work together? Thank!
UPD: If I add the following line to the constructor of the control, it will work:
this.Resources = Application.LoadComponent(new Uri("PTE.Controls;Component/Resources/CategoriesColors.xaml", UriKind.Relative)) as ResourceDictionary;
-, ( ) . -, XAML.