wpf - Control created in code behind does not have it's default Style -


i trying instantiate control in window's code behind (for frame navigation). unfortunately, it's not getting default style applied it.

i expect when control first rendered, renderer set control's style/template either (the explicitly supplied 1 -> default 1 supplied in resources -> null).

is mental model of how default styles applied wrong? how

class whatever {     int value = 5; } 

is syntactic sugar for

class whatever {     public whatever()     {         this.value = 5;     }      int value; } 

and style being set @ compile time?

could problem stemming how i'm accessing styles , templates control (unlikely can make control of it's type on main window , has default style)?

mainwindow.xaml

<window>     <window.resources>         <resourcedictionary>             </resourcedictionary.mergeddictionaries>                 <resourcedictionary source="datalogpage/themes.xaml" />             </resourcedictionary.mergeddictionaries>         </resourcedictionary>     </window.resources>     <!-- data here... --> </window> 

datalogpage/themes.xaml

<resourcedictionary>     <resourcedictionary.mergeddictionaries>         <resourcedictionary source="themes/styles/defaultstyle.xaml" />     </resourcedictionary.mergeddictionaries> </resourcedictionary> 

datalogpage/themes/styles/defaultstyle.xaml

<resourcedictionary>     <resourcedictionary.mergeddictionaries>         <resourcedictionary source="../templates/defaulttemplate.xaml" />     </resourcedictionary.mergeddictionaries>     <style targettype="this:datalog">         <setter property="template"                 value="{staticresource defaulttemplatefordatalog}" />     </style> </resourcedictionary> 

datalogpage/themes/templates/defaulttemplate.xaml

<resourcedictionary>     <controltemplate x:key="defaulttemplatefordatalog"                      targettype="this:datalog">         <!-- data here... -->     </controltemplate> </resourcedictionary> 

mainwindow.xaml.cs (where i'm creating control in code behind)

private void currentcontext_contextchangerequested() {     //other bits of logic context switching      //user wants go data log page     this.mainframe.navigate(new datalogpage.datalog());     return; } 

to reiterate:

problem: control created in code behind not have it's default style.

possible ideas on why may be:

1)my user model how default styles applied wrong , must set explicitly.
2)i may referencing style incorrectly.

if have explicitly set style/template, there best-of-both-worlds can make in mainwindow's xaml can programmatically reference so: new datalogpage.datalog(){style = this.datalogstyle};?

app.xaml

if want stuff shared may inject them shared application resources, or may merge them in app.xaml

<application x:class="blabla"          xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"          xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"          startupuri="mainwindow.xaml">    <application.resources>      <resourcedictionary>        <resourcedictionary.mergeddictionaries>           <resourcedictionary source="a.xaml"/>           <resourcedictionary source="b.xaml"/>        </resourcedictionary.mergeddictionaries>     </resourcedictionary>   </application.resources> </application> 

using global resourcedictionary in usercontrol library

wpf - global style?

templates , cust controls

applies customcontrols (**)

first off never start merging or using dictionaries way in window(that's if go cc way, rather merge these dictionaries in generic files described below, note must in scope though). have folder called generic, must have file called themes.xaml present. merge alot of dictionaries here , "manual labor". typically call cust control foo's theme themename.generic.xaml, that's preference. :).

a customcontrol should derrive control , must have following static constructor, having it's template applied.

public class whatever : control // scratch or want extend    static whatever()    {        defaultstylekeyproperty.overridemetadata(typeof (whatever), new frameworkpropertymetadata(typeof (whatever)));    } } 

<style targettype="{x:type local:whatever}">     <!-- whatever here ;) -->     <setter property="template">         <setter.value>             <controltemplate targettype="{x:type local:whatever}">                 <border background="{templatebinding background}"                         borderbrush="{templatebinding borderbrush}"                         borderthickness="{templatebinding borderthickness}">                 </border>             </controltemplate>         </setter.value>     </setter> </style> 

override onapplytemplate see if template applied. (*)

styles

applies styling.

to override existing controls template following in themes.xaml. make button controls have style default.

 <style targettype="{x:type button}">     <setter property="background" value="green"/>   </style> 

i put styles directly in ie usercontrol's resources, data,itemptemplates etc if don't belong in cust control, anyways how that:

<usercontrol.resources>     <!-- usuallly  datatemplates, itemtemplates etc.. -->     <style x:key="somestyle" targettype="{x:type button}">         <!--whatever -->         <setter property="background" value="black"/>     </style> </usercontrol.resources> <button style="{staticresource somestyle}"></button> 

btw: may load things dynamicly , other assemblies. resources, templates works. that's subject :)

hope helped some,

cheers

stian


Comments

Popular posts from this blog

javascript - Jquery show_hide, what to add in order to make the page scroll to the bottom of the hidden field once button is clicked -

javascript - Highcharts multi-color line -

javascript - Enter key does not work in search box -