wpf - Application thorws Exception when applying style for a CustomListBoxItem -


i have problem require bit of time. need add listboxes on multiple pages in windows phone 8 app, on each page normal/selected background listboxitem different(10-12 pages). 1 solution create different styles each listboxitem every page hard-coding color-codes in stylesheet(which dont want). have extended listboxitem class , added 2 dependency properties normal , selected item background listboxitem. may set these properties on each xaml page , dont have create different styles every page. here extended control

public class customlistboxitem : listboxitem {     public static readonly dependencyproperty normalbackgroundproperty =         dependencyproperty.register("normalbackground", typeof(solidcolorbrush), typeof(customlistboxitem), new propertymetadata(null));     public static readonly dependencyproperty selectedbackgroundproperty =         dependencyproperty.register("selectedbackground", typeof(solidcolorbrush), typeof(customlistboxitem), new propertymetadata(null));     public solidcolorbrush normalbackground     {         { return (solidcolorbrush)getvalue(normalbackgroundproperty); }         set { base.setvalue(normalbackgroundproperty, value); }     }     public solidcolorbrush selectedbackground     {         { return (solidcolorbrush)getvalue(selectedbackgroundproperty); }         set { base.setvalue(selectedbackgroundproperty, value); }     } } 

and here how use (cycleslist list of strings):

<listbox grid.row="1" itemssource="{binding cycleslist}" itemcontainerstyle="{staticresource filterlistboxitemstyle}" >                 <listbox.itemtemplate>                     <datatemplate>                         <controls:customlistboxitem normalbackground="transparent" content="{binding}" selectedbackground="red"/>                     </datatemplate>                 </listbox.itemtemplate>             </listbox> 

and here style applying make generic.

<style x:key="filterlistboxitemstyle" targettype="controls:customlistboxitem">     <setter property="background" value="transparent"/>     <setter property="borderthickness" value="0"/>     <setter property="padding" value="0"/>     <setter property="horizontalcontentalignment" value="left"/>     <setter property="verticalcontentalignment" value="top"/>     <setter property="template">         <setter.value>             <controltemplate targettype="listboxitem">                 <border x:name="layoutroot" borderbrush="{templatebinding borderbrush}" borderthickness="{templatebinding borderthickness}" background="{templatebinding background}" horizontalalignment="{templatebinding horizontalalignment}" verticalalignment="{templatebinding verticalalignment}">                     <visualstatemanager.visualstategroups>                         <visualstategroup x:name="commonstates">                             <visualstate x:name="normal"/>                             <visualstate x:name="mouseover"/>                             <visualstate x:name="disabled"/>                         </visualstategroup>                         <visualstategroup x:name="selectionstates">                             <visualstate x:name="unselected"/>                             <visualstate x:name="selected">                                 <storyboard>                                     <objectanimationusingkeyframes storyboard.targetproperty="background" storyboard.targetname="contentcontainer">                                         <discreteobjectkeyframe keytime="0" value="{binding relativesource={relativesource templatedparent}, path=selectedbackground}"/>                                     </objectanimationusingkeyframes>                                 </storyboard>                             </visualstate>                         </visualstategroup>                     </visualstatemanager.visualstategroups>                     <contentcontrol x:name="contentcontainer" contenttemplate="{templatebinding contenttemplate}" content="{templatebinding content}" foreground="{templatebinding foreground}" horizontalcontentalignment="{templatebinding horizontalcontentalignment}" margin="{templatebinding padding}" verticalcontentalignment="{templatebinding verticalcontentalignment}"                                     background="{binding relativesource={relativesource templatedparent}, path=normalbackground}"/>                 </border>             </controltemplate>         </setter.value>     </setter> </style> 

but application throws exception here (full stack trace):

{system.windows.markup.xamlparseexception: [line: 0 position: 0] ---> system.windows.markup.xamlparseexception: [line: 0 position: 0] ---> system.windows.markup.xamlparseexception: [line: 0 position: 0] ---> system.windows.markup.xamlparseexception: [line: 0 position: 0] ---> system.windows.markup.xamlparseexception: [line: 0 position: 0] ---> system.windows.markup.xamlparseexception: [line: 0 position: 0] ---> system.windows.markup.xamlparseexception: [line: 0 position: 0] @ ms.internal.xcpimports.checkhresult(uint32 hr) @ ms.internal.xcpimports.setvalue(imanagedpeerbase obj, dependencyproperty property, dependencyobject doh) @ system.windows.dependencyobject.setvalue(dependencyproperty property, dependencyobject doh) @ system.windows.controls.listbox.getcontainerforitemoverride() @ system.windows.controls.itemscontrol.ms.internal.controls.igeneratorhost.getcontainerforitem(object item, dependencyobject recycledcontainer) @ system.windows.controls.itemcontainergenerator.generator.generatenext(boolean stopatrealized, boolean& isnewlyrealized) @ system.windows.controls.itemcontainergenerator.system.windows.controls.primitives.iitemcontainergenerator.generatenext(boolean& isnewlyrealized) @ system.windows.controls.virtualizingstackpanel.measureoverride(size constraint) @ system.windows.frameworkelement.measureoverride(intptr nativetarget, double inwidth, double inheight, double& outwidth, double& outheight) --- end of inner exception stack trace --- @ ms.internal.xcpimports.checkhresult(uint32 hr) @ ms.internal.xcpimports.frameworkelement_measureoverride(frameworkelement element, size availablesize) @ system.windows.controls.scrollcontentpresenter.measureoverride(size constraint) @ system.windows.frameworkelement.measureoverride(intptr nativetarget, double inwidth, double inheight, double& outwidth, double& outheight) --- end of inner exception stack trace --- @ ms.internal.xcpimports.checkhresult(uint32 hr) @ ms.internal.xcpimports.uielement_measure(uielement element, size availablesize) @ system.windows.uielement.measure(size availablesize) @ system.windows.controls.scrollviewer.measureoverride(size constraint) @ system.windows.frameworkelement.measureoverride(intptr nativetarget, double inwidth, double inheight, double& outwidth, double& outheight) --- end of inner exception stack trace --- @ ms.internal.xcpimports.checkhresult(uint32 hr) @ ms.internal.xcpimports.frameworkelement_measureoverride(frameworkelement element, size availablesize) @ system.windows.frameworkelement.measureoverride(size availablesize) @ system.windows.frameworkelement.measureoverride(intptr nativetarget, double inwidth, double inheight, double& outwidth, double& outheight) --- end of inner exception stack trace --- @ ms.internal.xcpimports.checkhresult(uint32 hr) @ ms.internal.xcpimports.frameworkelement_measureoverride(frameworkelement element, size availablesize) @ system.windows.frameworkelement.measureoverride(size availablesize) @ system.windows.frameworkelement.measureoverride(intptr nativetarget, double inwidth, double inheight, double& outwidth, double& outheight) --- end of inner exception stack trace --- @ ms.internal.xcpimports.checkhresult(uint32 hr) @ ms.internal.xcpimports.frameworkelement_measureoverride(frameworkelement element, size availablesize) @ microsoft.phone.controls.phoneapplicationframe.measureoverride(size availablesize) @ system.windows.frameworkelement.measureoverride(intptr nativetarget, double inwidth, double inheight, double& outwidth, double& outheight) --- end of inner exception stack trace ---}

from stack-trace may appear if missing width/height of control, have tried set these also, nothing worked. there doing wrong? or can suggest better way of making generic customlistboxitem?

thanks in anticipation,

regards,

the resource dictionary key of customlistboxitem style filterlistboxitemstyle:

<style x:key="filterlistboxitemstyle" targettype="controls:customlistboxitem"> 

but use filterlistboxstyle refer style resource:

<listbox ... itemcontainerstyle="{staticresource filterlistboxstyle}"> 

change above declaration to

<listbox ... itemcontainerstyle="{staticresource filterlistboxitemstyle}"> 

update: having said this, there thing mention. in order have custom derived listboxitem class, won't use in datatemplate in itemtemplate. instead create custom derived listbox class overrides getcontainerforitemoverride method:

public class customlistbox : listbox {     protected override dependencyobject getcontainerforitemoverride()     {         return new customlistboxitem();     } } 

the custom listbox no longer declare itemtemplate:

<controls:customlistbox ...     itemcontainerstyle="{staticresource filterlistboxitemstyle}" /> 

you set normalbackground , selectedbackground properties in customlistboxitem style.


Comments

Popular posts from this blog

Android layout hidden on keyboard show -

google app engine - 403 Forbidden POST - Flask WTForms -

c - Why would PK11_GenerateRandom() return an error -8023? -