c# - MenuItem style when ItemsSource is used using setter Property with ResourceDictionaries or MergedDictionaries -


i've got context menu, , styled correctly resourcedictionary loaded xamlreader.load(). style key i'm using dynamicresource i've called stylebanner.

in context menu have 1 menu item called skins, styled correctly save above dynamic resource stylebanner. menu-item has it's sub-menu-items data-bound itemssource in data context view model, , working correctly.

my trouble child menu items not styled correctly.

here working, not getting styled @ all:

<window.contextmenu>     <contextmenu datacontext="timershostviewmodel" name="timershostcontextmenu" style="{dynamicresource stylebanner}">         <menuitem name="skins" header="skins" itemssource="{binding source={staticresource timershostviewmodel}, path=skins}" style="{dynamicresource stylebanner}">             <menuitem.itemcontainerstyle>                 <style targettype="menuitem">                     <setter property="header" value="{binding path=skinname}"/>                     <setter property="command" value="{binding source={staticresource timershostviewmodel}, path=timershostcontextmenuclickcommand}"/>                     <setter property="commandparameter" value="{binding path=skinname}"/>                 </style>             </menuitem.itemcontainerstyle>         </menuitem>     </contextmenu> </window.contextmenu> 

so here 1 thing i've tried, tried adding following line:

<setter property="style" value={dynamicresource stylebanner}"/> 

like this:

<style targettype="menuitem">     <setter property="header" value="{binding path=skinname}"/>     <setter property="style" value="{dynamicresource stylebanner}"/>     <setter property="command" value="{binding source={staticresource timershostviewmodel}, path=timershostcontextmenuclickcommand}"/>     <setter property="commandparameter" value="{binding path=skinname}"/> </style> 

i exception when try that: system.argumentexception {"style object not allowed affect style property of object applies."}

so tried change added line above follows:

<setter property="template" value="{dynamicresource stylebanner}"/> 

then different exception: system.invalidcastexception {"unable cast object of type 'system.windows.style' type 'system.windows.frameworktemplate'."}

so correct way this? i've searched on internet , stackoverflow without clue.


edit: ok see using following works getting style set on child menu items:

<setter property="itemcontainerstyle" value="{dynamicresource stylebanner}"/> 

but reason stylebanner background not used children menu items.

i should have posted stylebanner resourcedictionary here is:

<!-- banner style --> <style x:key="stylebanner">   <setter property="stackpanel.background">     <setter.value>       <lineargradientbrush startpoint="0.5,0" endpoint="0.5,1">         <gradientstop color="darkgray" offset="0.1" />         <gradientstop color="black" offset="1" />       </lineargradientbrush>     </setter.value>   </setter>   <setter property="textblock.foreground" value="white" />   <setter property="textblock.fontfamily" value="tr2n" /> </style> 

here have context menu now:

<window.contextmenu>     <contextmenu datacontext="timershostviewmodel" name="timershostcontextmenu" style="{dynamicresource stylebanner}">         <menuitem name="skins" header="skins" itemssource="{binding source={staticresource timershostviewmodel}, path=skins}" style="{dynamicresource stylebanner}">             <menuitem.itemcontainerstyle>                 <style targettype="menuitem">                     <setter property="header" value="{binding path=skinname}"/>                     <setter property="command" value="{binding source={staticresource timershostviewmodel}, path=timershostcontextmenuclickcommand}"/>                     <setter property="commandparameter" value="{binding path=skinname}"/>                     <setter property="itemcontainerstyle" value="{dynamicresource stylebanner}"/>                 </style>             </menuitem.itemcontainerstyle>         </menuitem>     </contextmenu> </window.contextmenu> 

here image showing difference in styles i'm getting: difference in styles applied different sections levels of context menu.

so can see font & font color applied, stylebanner background gradient brush defined in resourcedictionary not.

actually if played little trick, stylebanner resourcedictionary above posted blackskin.xaml resourcedictionary , has gradient background should dark grey, screenshot shows blue background, because i've got blueskin.xaml resourcedictionary, similar , have same behavior, here stylebanner blueskin.xaml resourcedictionary, completeness:

<!-- banner style --> <style x:key="stylebanner">   <setter property="stackpanel.background">     <setter.value>       <lineargradientbrush startpoint="0,0.25" endpoint="1,0.5">         <gradientstop color="#cc0088dd" offset="0.3" />         <gradientstop color="#3300ffff" offset="0.85" />       </lineargradientbrush>     </setter.value>   </setter>   <setter property="textblock.foreground" value="yellow" />   <setter property="textblock.fontfamily" value="comic sans ms" /> </style> 

you cannot set style property within style.

set stylebanner itemcontainerstyle within style e.g. contextmenustyle

e.g. lets name style name contextmenustyle , set itemcontainer style stylebanner

<style x:key="contextmenustyle" targettype="menuitem">     <setter property="header" value="{binding path=skinname}"/>     <setter property="command" value="{binding source={staticresource timershostviewmodel}, path=timershostcontextmenuclickcommand}"/>     <setter property="commandparameter" value="{binding path=skinname}"/>     <setter property="itemcontainerstyle" value="{dynamicresource stylebanner}"/> </style> 

now context menu can assign style:

<contextmenu.style>     <staticresource resourcekey="contextmenustyle"></staticresource> </contextmenu.style> 

hope idea... decide belongs style , belongs contextmenustyle , set style control , can set contextmenustyle within style. can have nested contextmenustyle within stylebanner inturn apply sub menus

edit:

you right, background not carried on child menu items. in case option left define controltemplate background, try :

<setter property="template">     <setter.value>         <controltemplate>             <border >                 <border.background>                     <lineargradientbrush startpoint="0,0.25" endpoint="1,0.5">                         <gradientstop color="#cc0088dd" offset="0.3" />                         <gradientstop color="#3300ffff" offset="0.85" />                     </lineargradientbrush>                 </border.background>                 <button content="{binding path=skinname}" command="{binding source={staticresource timershostviewmodel}, path=timershostcontextmenuclickcommand}"                          commandparameter="{binding path=skinname}"                         foreground="yellow" fontfamily="comic sans ms"></button>             </border>         </controltemplate>     </setter.value> </setter> 

Comments

Popular posts from this blog

python - Subclassed QStyledItemDelegate ignores Stylesheet -

java - HttpClient 3.1 Connection pooling vs HttpClient 4.3.2 -

SQL: Divide the sum of values in one table with the count of rows in another -