Style based on previously defined StaticResource not found at runtime

I am using Telerik RadControls for WPF with implicit style. The following style is defined in Themes/Windows8/Telerik.Windows.Controls.RibbonView.xaml :

 <Style TargetType="telerikRibbonView:RadRibbonView" x:Key="RadRibbonViewStyle"> ... </Style> 

My own styles and default values ​​for Telerik are combined into the Lib.Windows.Controls assembly in the Themes folder:

 <ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"> <ResourceDictionary.MergedDictionaries> <ResourceDictionary Source="Windows8/Telerik.Windows.Controls.RibbonView.xaml" /> <ResourceDictionary Source="MyTheme/TelerikCustomizations.xaml" /> <ResourceDictionary> <!-- avoid optimization --> <Style TargetType="{x:Type Rectangle}" /> </ResourceDictionary> </ResourceDictionary.MergedDictionaries> </ResourceDictionary> 

And in TelerikCustomizations.xaml I define the following (empty, for testing purposes) style:

 <Style x:Key="MyThemeRadRibbonViewStyle" TargetType="{x:Type telerik:RadRibbonView}" BasedOn="{StaticResource ResourceKey=RadRibbonViewStyle}" /> 

This raises the following exception at runtime:

'Provide value on 'System.Windows.Markup.StaticResourceHolder' threw an exception.' Line number '4' and line position '42'. {"Cannot find resource named 'RadRibbonViewStyle'. Resource names are case sensitive."}

This led me to the following debugging operations in MyView.xaml.cs:

 public ShellView() { var baseStyle = FindResource("RadRibbonViewStyle"); var inherited = FindResource("MyThemeRadRibbonViewStyle"); InitializeComponent(); } 

Now everything: an exception is thrown in the second FindResource call. With the same message. However, RadRibbonViewStyle explicitly found in the first line of the constructor.

If that matters, the merged dictionary actually merges into App.xaml a second time.

I am sure that I am missing something obvious, but I cannot understand that.

App.xaml

 <Application x:Class="TestClient.App" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" StartupUri="Views/ShellView.xaml"> <Application.Resources> <ResourceDictionary> <ResourceDictionary.MergedDictionaries> <ResourceDictionary Source="pack://application:,,,/Lib.Windows.Controls;component/Themes/MyTheme.xaml" /> <ResourceDictionary> <!-- added to avoid optimization --> <Style TargetType="{x:Type Rectangle}" /> </ResourceDictionary> </ResourceDictionary.MergedDictionaries> </ResourceDictionary> </Application.Resources> </Application> 

App.xaml.cs does not overwrite the constructor. This actually does nothing.

Update

If I combined Telerik dictionaries in TelerikCustomizations.xaml instead of merging them into another dictionary ( MyTheme.xaml ), the exception will disappear.

However, I would still like to know why this is happening.

+6
source share
1 answer

You need to combine Windows8/Telerik.Windows.Controls.RibbonView.xaml into MyTheme/TelerikCustomizations.xaml

 <ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"> <ResourceDictionary.MergedDictionaries> <ResourceDictionary Source="Windows8/Telerik.Windows.Controls.RibbonView.xaml" /> <ResourceDictionary> <Style x:Key="MyThemeRadRibbonViewStyle" TargetType="{x:Type telerik:RadRibbonView}" BasedOn="{StaticResource ResourceKey=RadRibbonViewStyle}" /> </ResourceDictionary> </ResourceDictionary.MergedDictionaries> </ResourceDictionary> 

And now you can use / combine this dictionary wherever you want.

You need to do this because StaticResource does not work between the MergedDictionaries sister, so you cannot refer to a resource that has been merged at the same level because StaticResource only looks back at direct parents:

From MSDN :

References to XAML resources in a specific resource dictionary must reference a resource that is already defined using the key, and this resource should appear lexically before the link to the resource. Direct links cannot be resolved by reference to the XAML resource

But when using MergedDictionaries :

In the resource search sequence, the MergedDictionaries dictionary is checked only after checking all the key ResourceDictionary resources declared by the MergedDictionaries.

+6
source

Source: https://habr.com/ru/post/952113/


All Articles