1

Topic: How WPF decides what style to apply?

Tried under the book "Pro WPF 4.5 in CSharp 4th edition" to create lookless user control. The idea like is simple: we Create style about value TargetType of demanded type that it was automatically applied to all objects of the specified type. We set its property Template.//LooklessColorPicker.xaml <ResourceDictionary xmlns = "http://schemas.microsoft.com/winfx/2006 … esentation" xmlns:x = "http://schemas.microsoft.com/winfx/2006/xaml" xmlns:local = "clr-namespace:CustomControls"> <Style TargetType = "{x:Type local:LooklessColorPicker}"> <Setter Property = "Template"> <Setter. Value> <ControlTemplate TargetType = "{x:Type local:LooklessColorPicker}"> <Grid>... </Grid> </ControlTemplate> </Setter. Value> </Setter> </Style> </ResourceDictionary> Further the author writes that it is necessary to create folder Themes in which to place a file generic.xaml with following contents://Themes/generic.xaml <ResourceDictionary xmlns = "http://schemas.microsoft.com/winfx/2006 … esentation" xmlns:x = "http://schemas.microsoft.com/winfx/2006/xaml"> <ResourceDictionary. MergedDictionaries> <ResourceDictionary Source = "/CustomControls;component/themes/LooklessColorPicker.xaml"/> </ResourceDictionary. MergedDictionaries> </ResourceDictionary> Well in the static designer it is necessary to tell WPF what style by default to use: public class LooklessColorPicker: Control {static LooklessColorPicker () {DefaultStyleKeyProperty. OverrideMetadata (typeof (LooklessColorPicker), new FrameworkPropertyMetadata (typeof (LooklessColorPicker)));... And all is good, only here does not work (is not displayed at layout in shape) In this connection two questions more precisely: 1) Why can not work? 2) whether it is necessary  presence of folder Themes and a file generic.xaml in it now? The author in a footnote writes that it is type a rudiment since Windows XP. Where generally to esteem as WPF searches  for subjects?

2

Re: How WPF decides what style to apply?

Probably too turbidly asked a question, I reformulate: I Try to make lookless control in "a minimum configuration" not to distract from an essence. Made the standard project in a Visual Studio with a title ' WpfApplication3 ', in it created folder Themes and allocated there two files generic.xaml and LooklessControlStyle.xaml with such contents: generic.xaml - Build action-> Page <ResourceDictionary xmlns = "http://schemas.microsoft.com/winfx/2006 … esentation" xmlns:x = "http://schemas.microsoft.com/winfx/2006/xaml" xmlns:local = "clr-namespace:WpfApplication3"> <ResourceDictionary. MergedDictionaries> <! - Cannot locate resource ' themes/looklesscontrolstyle.xaml '->//the First variant of an error in MainWindow.xalm <ResourceDictionary Source = "/Themes/LooklessControlStyle.xaml"/>//<- here what errors are not present, the resource is found <! - Invalid URI: The format of the URI could not be determined->//the Second variant of an error in MainWindow.xalm <! - <ResourceDictionary Source = "pack://application:,,,/Themes/LooklessControlStyle.xaml"/>-> </ResourceDictionary. MergedDictionaries> </ResourceDictionary> LooklessControlStyle.xaml - Build action-> Page <ResourceDictionary xmlns = "http://schemas.microsoft.com/winfx/2006 … esentation" xmlns:x = "http://schemas.microsoft.com/winfx/2006/xaml" xmlns:local = "clr-namespace:WpfApplication3"> <Style TargetType = "local:LooklessControl"> <Setter Property = "Template"> <Setter. Value> <ControlTemplate TargetType = "local:LooklessControl"> <Button Width = "100" Height = "25"> Template1 </Button> </ControlTemplate> </Setter. Value> </Setter> </Style> </ResourceDictionary> Class LooklessControl looks so: namespace WpfApplication3 {public class LooklessControl: Control {static LooklessControl () {DefaultStyleKeyProperty. OverrideMetadata (typeof (LooklessControl), new FrameworkPropertyMetadata (typeof (LooklessControl)));} }} Well and MainWindow.xalm: <Window x:Class = "WpfApplication3.MainWindow" xmlns = "http://schemas.microsoft.com/winfx/2006 … esentation" xmlns:x = "http://schemas.microsoft.com/winfx/2006/xaml" xmlns:d = "http://schemas.microsoft.com/expression/blend/2008" xmlns:mc = "http://schemas.openxmlformats.org/marku … ility/2006" xmlns:local = "clr-namespace:WpfApplication3" mc:Ignorable = "d" Title = "LooklessControl Test" Height = "300" Width = "300"> <! - <Window. Resources> <ResourceDictionary> <ResourceDictionary. MergedDictionaries> <ResourceDictionary Source = "/Themes/LooklessControlStyle.xaml"/> </ResourceDictionary. MergedDictionaries> </ResourceDictionary> </Window. Resources>-> <Grid> <local:LooklessControl/>//Errors jump out here. Errors are described in generic.xaml </Grid> </Window> If  <Window. Resources> in MainWindow.xalm and to remove DefaultStyleKeyProperty. OverrideMetadata from LooklessControl.cs all works. If all to leave as there is that in MainWindow.xalm jump out two variants of errors (see generic.xaml) depending on that as the resource in generic.xaml is registered. Questions: 1) Why so? How to treat? 2) and here nevertheless folder Themes and a file generic.xaml?

3

Re: How WPF decides what style to apply?

Hello, Cynic, you wrote: a C> Questions: a C> 1) Why so? How to treat? Most likely because the author of the book offered you one variant URL, and you for some reason used another. Connect XAML a template through component: <ResourceDictionary Source = "/<assembly-name>;component/Themes/LooklessControlStyle.xaml"/> a C> 2) And here nevertheless folder Themes and a file generic.xaml? In such folders WPF searches XAML for various subjects of the Windows which names are added by a suffix. Such subjects were in XP, and, since "seven", are already irrelevant.

4

Re: How WPF decides what style to apply?

Hello, MxMsk, you wrote: MM> Most likely because the author of the book offered you one variant URL, and you for some reason used another. Connect XAML a template through component: MM> MM> <ResourceDictionary Source = "/<assembly-name>;component/Themes/LooklessControlStyle.xaml"/> MM> Yes in it business. Only it is not clear only why I should specify Source in such format, after all at me all is in one assembly, and at the author in two different? And it is yet clear, that then WPF in generic.xaml on Source does not swear, and in MainWindow.xalm there are errors MM> In such folders WPF searches XAML for various subjects of the Windows which names are added by a suffix. Such subjects were in XP, and, since "seven", are already irrelevant. Well I would not tell that it is not actual: it is necessary to rename generic.xaml or to move from folder Themes as LooklessControl it ceases to be displayed both in the designer, and at execution. So probably all the same there is an agreement concerning that where to search ResourceDictionary, after all setting ResourceDictionary in generix.xaml we not merg' it with App or Window ResourceDictionary!

5

Re: How WPF decides what style to apply?

Hello, Cynic, you wrote: the C> Well I would not tell that it is not actual: it is necessary to rename generic.xaml or to move from folder Themes as LooklessControl it ceases to be displayed both in the designer, and at execution. So probably all the same there is an agreement concerning that where to search ResourceDictionary, after all setting ResourceDictionary in generix.xaml we not merg' it with App or Window ResourceDictionary!  not in generic.xaml, and in support by that Windows XP as here. Though, in some applications it still is required, yes.

6

Re: How WPF decides what style to apply?

Hello, MxMsk, you wrote: MM> Neaktualnost not in generic.xaml, and in support by that Windows XP as here. Though, in some applications it still is required, yes. It appeared that the author told about all but attribute ThemeInfo in AssemblyInfo which looks so://Specifies the location in which theme dictionaries are stored for types in an assembly. [assembly: ThemeInfo (//Specifies the location of system theme-specific resource dictionaries for this project.//The default setting in this project is "None" since this default project does not//include these user-defined theme files://Themes\Aero. NormalColor.xaml//Themes\Classic.xaml//Themes\Luna. Homestead.xaml//Themes\Luna. Metallic.xaml//Themes\Luna. NormalColor.xaml//Themes\Royale. NormalColor.xaml ResourceDictionaryLocation.SourceAssembly,//Specifies the location of the system non-theme specific resource dictionary: //Themes\generic.xaml ResourceDictionaryLocation.SourceAssembly)] By default this attribute is added in the WPF-project (type WindowsApplication) at creation in such type: [assembly: ThemeInfo (ResourceDictionaryLocation. None,//where theme specific resource dictionaries are located//(used if a resource is not found in the page,//or application resource dictionaries) ResourceDictionaryLocation.SourceAssembly//where the generic resource dictionary is located//(used if a resource is not found in the page,//app, or any theme specific resource dictionaries))] but it is not added in projects of type ClassLibrary. Therefore in WPF-project WPF will search for a file generic.xalm in folder Themes, and in the ClassLibrary-project is not present. It is necessary to understand as WPF selects a subject at missing attribute ThemeInfo