Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

WPF: Style based on another one in a separate assembly

Tags:

Assembly A - ResourceDictionary contains StyleA style.
Assembly B - ResourceDictionary.MergedDictionaries to merge resources from Assembly A into B.

I would like to create a style in Assembly B "based on" StyleA. Is it possible?

I am trying to create this style:

<Style x:Key="StyleB" BasedOn="{StaticResource StyleA}">
   <Setter Property="Button.Foreground" Value="Khaki"/>
</Style>

But I get a XamlParseException exception at run-time, if I use StyleB:

Cannot convert the value in attribute 'Style' to object of type 'System.Windows.Style'. Can only base on a Style with target type that is base type 'IFrameworkInputElement'. Error at object 'System.Windows.Controls.Button' in markup file 'SamSeekApp;component/mainwindow.xaml'

like image 750
Gus Cavalcanti Avatar asked Mar 31 '09 21:03

Gus Cavalcanti


People also ask

What is the use of styles in WPF?

WPF Styles consist of Setters and Triggers that are supposed to change the Style and Behavior of a Control. From a technical point of view the purpose of Styles is to set Dependency Properties on a Control. Some Use Cases of Styles: Change Colors of a Control

How to use stylerefextension in XAML designer?

Create class in styles assembly which will extend abstract StyleRefExtension: You can use this class in any project and XAML designer will show you your styles. For example, I will create a WinForms project and will add WPF user control, and for example will use DynamicResource with referenced assembly: Yes, not working as expected...

How do I declare a style in XAML?

The most common way to declare a style is as a resource in the Resources section in a XAML file. Because styles are resources, they obey the same scoping rules that apply to all resources. Put simply, where you declare a style affects where the style can be applied.

What are WPF styles and triggers?

In this article I will introduce you into the concept of WPF Styles and Triggers. WPF Styles consist of Setters and Triggers that are supposed to change the Style and Behavior of a Control. From a technical point of view the purpose of Styles is to set Dependency Properties on a Control.


1 Answers

Try adding TargetType="{x:Type Button}" to your 'StyleB'.

like image 73
Richard Davis Avatar answered Nov 02 '22 00:11

Richard Davis