wpf - UserControl in DataTemplate doesn't applied style for font -


i have user control in datatemplate, style of textblock doesn't change fontsize changes background.

attached samples:

  1. create wpf window.

  2. create user control, usercontrol1

  3. inside window paste below code:

    <window.resources>   <style targettype="{x:type textblock}"          x:key="textblockstylefontandbackgound">       <setter property="fontsize"               value="20" />       <setter property="background"               value="blue" />   </style>   <datatemplate x:key="contenttemplate">       <stackpanel>             <m:usercontrol1 />       </stackpanel>   </datatemplate> </window.resources> <grid>   <contentcontrol fontsize="10">       <stackpanel x:name="stackpanel">               <button click="button_click" />               <contentcontrol  contenttemplate="{staticresource contenttemplate}" />               <!--<m:usercontrol1 />-->       </stackpanel>   </contentcontrol> </grid> 
  4. in user control paste following code:

    <usercontrol.resources>   <datatemplate x:key="contenttemplateinsideusercontrol">       <textblock name="textblockinresourse" text="textblockinsideusercontrolresource"                  style="{dynamicresource textblockstylefontandbackgound}"/>   </datatemplate> </usercontrol.resources> <grid>     <stackpanel>     <contentcontrol contenttemplate="{staticresource contenttemplateinsideusercontrol}" />         <button content="st" click="button_click" />         <textblock name="textblockincontrol" text="textblockinsideusercontrol"                    style="{dynamicresource textblockstylefontandbackgound}" />     </stackpanel> </grid> 

we have 2 text blocks same background color, blue, different font sizes.

textblockinresourse fontsize = 20, taken style textblockstylefontandbackgound

textblockincontrol fontsize = 10, inherited value, why happen?

i have added handle in user control:

   private void button_click(object sender, routedeventargs e)     {         style style = findresource("textblockstylefontandbackgound") style;         textblockincontrol.style = null;         textblockincontrol.style = style;     } 

and font set style textblockstylefontandbackgound, , it's size 20

why fontsize taken style textblockstylefontandbackgound.

thanks, barak

that's peculiar problem have found there. i'm not sure why fontsize not affected when not in datatemplate... looking @ 2 property descriptions , remarks on msdn, difference between them textblock.fontsize attachedproperty, can't see how affect anything.

i can offer solution problem if you're still interested. try declaring style in app.xaml file:

<application.resources>     <style targettype="{x:type textblock}" x:key="textblockstylefontandbackgound">         <setter property="fontsize" value="20" />         <setter property="background" value="blue" />     </style> </application.resources> 

then declare textblock in usercontrol using staticresource so:

<textblock text="text" style="{staticresource textblockstylefontandbackgound}" /> 

Comments

Popular posts from this blog

apache - Add omitted ? to URLs -

redirect - bbPress Forum - rewrite to wwww.mysite prohibits login -

php - How can I stop spam on my custom forum/blog? -