c# - Default styles do not apply to subclasses -
i have weird scenario involving overriding default styles in wpf , having them apply subclasses. not possible? example, have following code:
<window x:class="testwpfstyling.mainwindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:testwpfstyling="clr-namespace:testwpfstyling" title="mainwindow" height="350" width="525" background="white"> <window.resources> <style targettype="testwpfstyling:somelabel"> <setter property="foreground" value="red" /> </style> </window.resources> <grid> <grid.rowdefinitions> <rowdefinition height="auto" /> <rowdefinition height="auto" /> <rowdefinition height="auto" /> </grid.rowdefinitions> <testwpfstyling:somelabel grid.row="0">this should red.</testwpfstyling:somelabel> <testwpfstyling:yellowlabel grid.row="1">this should red.</testwpfstyling:yellowlabel> <stackpanel grid.row="2"> <stackpanel.resources> <style targettype="testwpfstyling:somelabel"> <setter property="foreground" value="blue" /> </style> </stackpanel.resources> <testwpfstyling:somelabel>this should blue.</testwpfstyling:somelabel> <testwpfstyling:yellowlabel>this should blue.</testwpfstyling:yellowlabel> </stackpanel> </grid> </window> with code-behind:
namespace testwpfstyling { public partial class mainwindow : window { } public class somelabel : label { } public class yellowlabel : somelabel { } } i expect control yellowlabel in stackpanel have color of blue , 1 outside red, both of them black. there way subclass take on default style of parent?
actually how wpf works. have a blog post discusses wpf styling (primarily regards how our theme property works) underlying issue scenario 1 of problem 1 same describing. is, implicit local styles located using actual class type , have no relation defaultstylekey. defaultstylekey used when locating default style (i.e. style applied control based on current os theme , default generic.xaml control). 1 easy way address set style of derived control (e.g in ctor) dynamicresource reference base class' style. e.g.
this.setresourcereference(styleproperty, typeof(somelabel));
Comments
Post a Comment