wpf - Automatically inherit control styles in only one custom window -


i wondering if possible associate styles controls custom window in wpf.

here's scenario - have created custom window, , have defined styles number of controls use in window. these contained in portable class library.

the catch only want controls use style library when used in custom window (there several different windows in application).

i understand can assign styles key, , load them portable library in application's app.xaml using pack syntax, example:

 <application.resources>     <resourcedictionary>         <resourcedictionary.mergeddictionaries>             <resourcedictionary source="pack://application:,,,/custom.application.library.controls;component/styles/checkbox.xaml"/>         </resourcedictionary.mergeddictionaries>     </resourcedictionary> </application.resources> 

and add , style control within custom window such:

<checkbox x:name="checkbox" style="{staticresource specialcheckbox}"  

but define styles in class library without key, in this:

<style targettype="{x:type checkbox}"> 

instead of this:

<style x:key="specialcheckbox" targettype="{x:type checkbox}"> 

so when checkbox used in custom window automatically inherits style. if define style this, , load app.xaml, problem checkboxes inherit style, not checkboxes used in custom window.

so, i'm trying find out if there way associate style resource explicitly custom window, can define styles without key, , have them default inherit "special" style when used in custom window, use wpf defaults in other windows of application. have experience this?

for clarity here code of custom window:

xaml:

<!-- window style --> <style targettype="{x:type controls:cctapplicationwindow}">     <setter property="windowstyle" value="none"/>     <setter property="allowstransparency" value="true"/>     <setter property="resizemode" value="canresizewithgrip"/>     <setter property="minwidth" value="500"/>     <setter property="template">         <setter.value>             <controltemplate targettype="{x:type controls:cctapplicationwindow}">                 <border borderbrush="#ff999999">                     <border.style>                         <style targettype="{x:type border}">                             <setter property="borderthickness" value="1"/>                             <style.triggers>                                 <datatrigger binding="{binding relativesource={relativesource templatedparent}, path=windowstate}" value="maximized">                                     <setter property="borderthickness" value="7"/>                                 </datatrigger>                             </style.triggers>                         </style>                     </border.style>                     <grid>                         <grid>                             <grid.rowdefinitions>                                 <rowdefinition height="29"/>                                 <rowdefinition />                             </grid.rowdefinitions>                             <controls:cctapplicationheader grid.row="0"                                                             margin="0"                                                             title="{templatebinding title}"                                                             dragmovecommand="{templatebinding dragmovecommand}"                                                             maximizecommand="{templatebinding maximizecommand}"                                                             minimizecommand="{templatebinding minimizecommand}"                                                             closecommand="{templatebinding closecommand}"/>                             <grid background="white" grid.row="1" margin="0">                                 <adornerdecorator>                                     <contentpresenter/>                                 </adornerdecorator>                             </grid>                         </grid>                     </grid>                 </border>             </controltemplate>         </setter.value>     </setter> </style> 

cs:

public partial class cctapplicationwindow : window {     public static readonly dependencyproperty maximizecommandproperty = dependencyproperty.register("maximizecommand", typeof(delegatecommand), typeof(cctapplicationwindow));     public static readonly dependencyproperty minimizecommandproperty = dependencyproperty.register("minimizecommand", typeof(delegatecommand), typeof(cctapplicationwindow));     public static readonly dependencyproperty closecommandproperty = dependencyproperty.register("closecommand", typeof(delegatecommand), typeof(cctapplicationwindow));     public static readonly dependencyproperty dragmovecommandproperty = dependencyproperty.register("dragmovecommand", typeof(delegatecommand), typeof(cctapplicationwindow));      public cctapplicationwindow()     {         maximizecommand = new delegatecommand(maximizeexecute);         minimizecommand = new delegatecommand(minimizeexecute);         closecommand = new delegatecommand(closeexecute);         dragmovecommand = new delegatecommand(dragmoveexecute);     }      static cctapplicationwindow()     {         defaultstylekeyproperty.overridemetadata(typeof(cctapplicationwindow), new frameworkpropertymetadata(typeof(cctapplicationwindow)));     }      public delegatecommand maximizecommand     {                 {             return (delegatecommand)getvalue(maximizecommandproperty);         }         set         {             setvalue(maximizecommandproperty, value);         }     }      public delegatecommand minimizecommand     {                 {             return (delegatecommand)getvalue(minimizecommandproperty);         }         set         {             setvalue(minimizecommandproperty, value);         }     }      public delegatecommand closecommand     {                 {             return (delegatecommand)getvalue(closecommandproperty);         }         set         {             setvalue(closecommandproperty, value);         }     }      public delegatecommand dragmovecommand     {                 {             return (delegatecommand)getvalue(dragmovecommandproperty);         }         set         {             setvalue(dragmovecommandproperty, value);         }     }      private void maximizeexecute(object obj)     {         if (this.windowstate != windowstate.maximized)         {             this.windowstate = windowstate.maximized;         }         else         {             systemcommands.restorewindow(this);         }     }      private void minimizeexecute(object obj)     {         systemcommands.minimizewindow(this);     }      private void closeexecute(object obj)     {         systemcommands.closewindow(this);     }      private void dragmoveexecute(object obj)     {         dragmove();     } } 

yes, can this, shouldn't! you've tagged question mvvm , yet architecture design breaks mvvm entirely. whole point of mvvm view logic contained within view model layer; view models ones should keeping track of logical hierarchy , ones should exposing properties views control appearance. put way, because xaml flexible enough , powerful enough implement such logic doesn't mean it's best place it!

to answer question though, yes, can done datatrigger binding parent objecttotypeconverter. here's example of setting textblock background cornflowerblue, unless immediate parent grid in case should set palegoldenrod:

<stackpanel orientation="vertical">      <stackpanel.resources>         <converters:objecttotypeconverter x:key="objecttotypeconverter" />         <style targettype="{x:type textblock}">             <setter property="background" value="cornflowerblue" />             <style.triggers>                 <datatrigger binding="{binding path=parent, relativesource={relativesource mode=self}, converter={staticresource objecttotypeconverter}}" value="{x:type grid}">                     <setter property="background" value="palegoldenrod" />                 </datatrigger>             </style.triggers>         </style>     </stackpanel.resources>      <grid width="100" height="32" horizontalalignment="left">         <textblock text="textbox a" /> <!-- gets palegoldenrod background -->     </grid>     <canvas width="100" height="32" horizontalalignment="left">         <textblock text="textbox b" /> <!-- gets cornflowerblue background -->     </canvas>  </stackpanel> 

and here's converter code. it's worth pointing out if you're happy check parent of given type exists somewhere in hierarchy (as opposed immediate parent) don't need this, can attempt bind relativesource ancestortype set relevant parent type.

// based on http://stackoverflow.com/questions/8244658/binding-to-the-object-gettype [valueconversion(typeof(object), typeof(type))] public class objecttotypeconverter : ivalueconverter {     public object convert(object value, type targettype, object parameter, system.globalization.cultureinfo culture)     {         return value == null ? null : value.gettype();     }      public object convertback(object value, type targettype, object parameter, system.globalization.cultureinfo culture)     {         throw new invalidoperationexception();     } } 

but again, implore you, if want adhere mvvm not this! kind of problem "proper" mvvm designed solve.


Comments

Popular posts from this blog

sublimetext3 - what keyboard shortcut is to comment/uncomment for this script tag in sublime -

java - No use of nillable="0" in SOAP Webservice -

ubuntu - Laravel 5.2 quickstart guide gives Not Found Error -