wpf - OnApplyTemplate never called in Custom Control -
i tested codes custom command onapplytemplate
never called. i'm sure have right static methods , assemblyinfo.cs setup; whole version inclued. https://www.dropbox.com/sh/n4uusow5z6ncd9c/aadmri9jlr-qss7o2qyag-5aa?dl=0
public override void onapplytemplate() { base.onapplytemplate(); console.writeline("begin"); //get part controls part_mastergrid = gettemplatechild("part_mastergrid") grid; part_rightcntl = gettemplatechild("part_rightcntl") stackpanel; part_leftcntl = gettemplatechild("part_leftcntl") stackpanel; part_bottomcntl = gettemplatechild("part_bottomcntl") stackpanel; part_parentpanel = gettemplatechild("part_parentpanel") dockpanel; //verify master grid exist if (part_mastergrid == null) return; //setup parent grid var parentgrid = new grid(); setupparentgrid(parentgrid); //set layers var layer0 = layers.firstordefault(x => x.level == 0); if (layer0 == null) return; var columnlayers = layers.select(x => x).where(x => x.level > 0 && x.orientation == layer.layerorientation.column).orderby( x => x.level); var rowlayers = layers.select(x => x).where(x => x.level > 0 && x.orientation == layer.layerorientation.row).orderby(x => x.level); var item = setuplayer0(layer0, columnlayers, rowlayers.count()); parentgrid.children.add(item); grid.setrow(item, 0); //setup column grid layers if (columnlayers.any()) { foreach (var layer in columnlayers) { setupcolumnlayers(parentgrid, layer, columnlayers.count()); } } //setup row grid layers if (rowlayers.any()) { foreach (var layer in rowlayers) { setuprowlayers(item, layer, rowlayers.count()); } } //add parent grid master grid part_mastergrid.children.add(parentgrid); grid.setrow(parentgrid, 0); }
update: had next layeredgrid.xaml
, had generic.xaml
include layeredgrid.xaml
<style targettype="{x:type common:layeredgrid}"> <setter property="template"> <setter.value> <controltemplate> <dockpanel verticalalignment="stretch" horizontalalignment="stretch" lastchildfill="true" name="part_parentpanel"> <stackpanel name="part_bottomcnt1" orientation="horizontal" dockpanel.dock="bottom" background="aliceblue"></stackpanel> <stackpanel name="part_leftcnt1" orientation="horizontal" dockpanel.dock="left" background="aliceblue"> <stackpanel.layouttransform> <rotatetransform angle="90"/> </stackpanel.layouttransform> </stackpanel> <stackpanel name="part_rightcnt1" orientation="horizontal" dockpanel.dock="right" background="aliceblue"> <stackpanel.layouttransform> <rotatetransform angle="90"/> </stackpanel.layouttransform> </stackpanel> <grid name="part_mastergrid" issharedsizescope="true" background="aliceblue"></grid> </dockpanel> </controltemplate> </setter.value> </setter> </style>
update2: [update2 has nil above code version has themes root folder] in mainwindow.xaml
gives compiled error cannot locate resource 'layeredgrid.xaml'.
<dockpanel> <stackpanel name="downstatusbar" dockpanel.dock="bottom" background="aliceblue"> <label></label> </stackpanel> <testnest3:layeredgrid> <testnest3:layeredgrid.layers> <testnest3:layer level="0"> <testnest3:layer.content> <grid> ... </grid> </testnest3:layer.content> </testnest3:layer> </testnest3:layeredgrid.layers> </testnest3:layeredgrid> </dockpanel>
there few things need check create sure default style gets applied:
make sure have assembly-level themeinfo
attribute , pass in resourcedictionarylocation.sourceassembly
sec argument (genericdictionarylocation
):
[assembly: themeinfo( resourcedictionarylocation.none, resourcedictionarylocation.sourceassembly)]
make sure have themes\generic.xaml
resource dictionary in same assembly custom control, build action of "page". note themes
must top-level folder in project.
make sure override default style key custom command in static constructor:
static layergrid() { defaultstylekeyproperty.overridemetadata( typeof(layergrid), new frameworkpropertymetadata(typeof(layergrid))); }
make sure generic.xaml
includes (either straight or through dictionary merging) style
targettype
matching custom control. should not have explicit x:key
, , should set template
property. if pull style in through mergeddictionaries
, create sure utilize assembly-qualified uris when merging in other dictionaries, e.g.:
<resourcedictionary source="/test_nest3;component\themes/layeredgrid.xaml" />
if you've verified above , still having problems, check output window create sure there isn't sort of error happening might prevent style beingness applied. also, check obvious: command getting loaded visual tree somewhere?
edit: able open project on phone, , seems themes
folder in wrong location: must straight under project, have nested under common
folder. location supposed relative assembly root, not folder containing file command defined.
wpf custom-controls
No comments:
Post a Comment