properties - How can I add Dependency Property to a DataGridRow WPF -
i'm working custom datagrid , want add property datagridrow. don't know how can that. think crate custom datagridrow can't use in custom datagrid. error message:
a style intended type 'datagridrowex' cannot applied type 'datagridrow
this's xaml code custom datagridrow:
<resourcedictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:local="clr-namespace:mydatagridrow"> <style x:key="{x:type local:datagridrowex}" targettype="{x:type local:datagridrowex}"> <setter property="template"> <setter.value> <controltemplate targettype="{x:type local:datagridrowex}"> <border x:name="dgr_border" borderbrush="{templatebinding borderbrush}" borderthickness="{templatebinding borderthickness}" snapstodevicepixels="true"> <border.background> <lineargradientbrush startpoint="0.5,0" endpoint="0.5,1"> <gradientstop offset="0" color="#aaffffaa" /> <gradientstop offset="1" color="#aaffd455" /> </lineargradientbrush> </border.background> <selectivescrollinggrid> <selectivescrollinggrid.columndefinitions> <columndefinition width="auto" /> <columndefinition width="*" /> </selectivescrollinggrid.columndefinitions> <selectivescrollinggrid.rowdefinitions> <rowdefinition height="*" /> <rowdefinition height="auto" /> </selectivescrollinggrid.rowdefinitions> <datagridcellspresenter grid.column="1" itemspanel="{templatebinding itemspanel}" snapstodevicepixels="{templatebinding snapstodevicepixels}" /> <datagriddetailspresenter grid.row="1" grid.column="1" selectivescrollinggrid.selectivescrollingorientation="{binding arerowdetailsfrozen, converterparameter={x:static selectivescrollingorientation.vertical}, converter={x:static datagrid.rowdetailsscrollingconverter}, relativesource={relativesource ancestortype={x:type datagrid}}}" visibility="{templatebinding detailsvisibility}" /> <datagridrowheader grid.rowspan="2" selectivescrollinggrid.selectivescrollingorientation="vertical" visibility="{binding headersvisibility, converterparameter={x:static datagridheadersvisibility.row}, converter={x:static datagrid.headersvisibilityconverter}, relativesource={relativesource ancestortype={x:type datagrid}}}" /> </selectivescrollinggrid> </border> </controltemplate> </setter.value> </setter> </style> </resourcedictionary>
and xaml code custom datagrid
<resourcedictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:local="clr-namespace:mydatagrid" xmlns:mydatagridrow="clr-namespace:mydatagridrow;assembly=mydatagridrow"> <lineargradientbrush x:key="headerbackgroundbrush" endpoint="1,.6" startpoint="1,0"> <gradientstop color="#8fabb5" offset="0" /> <gradientstop color="#9fbec9" offset="0.7" /> <gradientstop color="#cdeffb" offset="1" /> </lineargradientbrush> <lineargradientbrush x:key="headerbackgroundopacitymask" startpoint="0,0" endpoint="0,1"> <gradientstop color="#ffffffff" offset="0"/> <gradientstop color="#ffffffff" offset="0.667"/> <gradientstop color="#00ffffff" offset="1"/> </lineargradientbrush> <lineargradientbrush x:key="rowbackgroundselectedbrush" startpoint="0,0" endpoint="0,1"> <gradientstop color="#4f8cc7" offset="0" /> <gradientstop color="#1c4b7c" offset="0.7" /> <gradientstop color="#042d5b" offset="1" /> </lineargradientbrush> <solidcolorbrush x:key="datagridhorizontallinesbrush" color="black" /> <solidcolorbrush x:key="foreigncolorselecttextbrush" color="whitesmoke" /> <style x:key="columnheadergripperstyle" targettype="{x:type thumb}"> <setter property="width" value="2"/> <setter property="background" value="transparent"/> <setter property="cursor" value="sizewe"/> <setter property="template"> <setter.value> <controltemplate targettype="{x:type thumb}"> <border padding="{templatebinding padding}" background="{templatebinding background}"/> </controltemplate> </setter.value> </setter> </style> <style x:key="datagridcellstyle" targettype="{x:type datagridcell}"> <setter property="background" value="transparent" /> <setter property="borderbrush" value="transparent" /> <setter property="borderthickness" value="1" /> <setter property="template"> <setter.value> <controltemplate targettype="{x:type datagridcell}"> <border background="transparent" borderbrush="{templatebinding borderbrush}" borderthickness="0" snapstodevicepixels="true"> <contentpresenter snapstodevicepixels="{templatebinding snapstodevicepixels}" margin="3"/> </border> </controltemplate> </setter.value> </setter> </style> <style x:key="datagridcolumnheadercustomtemplatestyle" targettype="{x:type datagridcolumnheader}"> <setter property="snapstodevicepixels" value="true" /> <setter property="minwidth" value="0" /> <setter property="minheight" value="30" /> <setter property="foreground" value="navy" /> <setter property="cursor" value="hand" /> <setter property="template"> <setter.value> <controltemplate targettype="datagridcolumnheader"> <grid name="root"> <grid.columndefinitions> <columndefinition width="*" /> <columndefinition width="auto" /> </grid.columndefinitions> <border x:name="backgroundborder" borderthickness="0,0,1,1" background="{staticresource headerbackgroundbrush}" borderbrush="{x:static systemcolors.activeborderbrush}" grid.columnspan="2" /> <contentpresenter margin="6,3,6,3" horizontalalignment="center" verticalalignment="center" /> <path x:name="sortarrow" visibility="collapsed" data="m0,0 l1,0 0.5,1 z" stretch="fill" grid.columnspan="2" width="8" height="6" fill="olive" margin="0,0,0,2" verticalalignment="bottom" rendertransformorigin="0.5,0.4" /> <rectangle width="1" fill="black" horizontalalignment="right" grid.columnspan="2" /> <thumb x:name="part_leftheadergripper" horizontalalignment="left" style="{staticresource columnheadergripperstyle}"/> <thumb x:name="part_rightheadergripper" horizontalalignment="right" style="{staticresource columnheadergripperstyle}"/> </grid> <controltemplate.triggers> <trigger property="ismouseover" value="true"> <setter targetname="backgroundborder" property="background" value="#cdeffb" /> </trigger> <trigger property="ispressed" value="true"> <setter property="background" targetname="backgroundborder" value="#4079b2" /> </trigger> <trigger property="sortdirection" value="ascending"> <setter targetname="sortarrow" property="visibility" value="visible"/> <setter targetname="sortarrow" property="rendertransform"> <setter.value> <rotatetransform angle="180"/> </setter.value> </setter> </trigger> <trigger property="sortdirection" value="descending"> <setter targetname="sortarrow" property="visibility" value="visible"/> </trigger> </controltemplate.triggers> </controltemplate> </setter.value> </setter> </style> <style x:key="datagridrowstyle" targettype="{x:type mydatagridrow:datagridrowex}"> <style.resources> <solidcolorbrush x:key="{x:static systemcolors.highlightbrushkey}" color="transparent"/> <solidcolorbrush x:key="{x:static systemcolors.controlbrushkey}" color="transparent"/> <solidcolorbrush x:key="{x:static systemcolors.controltextbrushkey}" color="whitesmoke"/> </style.resources> <setter property="minheight" value="25"/> <setter property="allowdrop" value="true"/> <setter property="background" value="transparent"/> <style.triggers> <trigger property="ismouseover" value="true"> <setter property="background" value="linen"/> </trigger> <multitrigger> <multitrigger.conditions> <condition property="isselected" value="true"/> <condition property="isfocused" value="true"/> </multitrigger.conditions> <setter property="background" value="{staticresource rowbackgroundselectedbrush}"/> <setter property="foreground" value="{staticresource foreigncolorselecttextbrush}"/> </multitrigger> <multitrigger> <multitrigger.conditions> <condition property="isselected" value="true"/> <condition property="isfocused" value="false"/> </multitrigger.conditions> <setter property="background" value="#ad855c"/> <setter property="foreground" value="{staticresource foreigncolorselecttextbrush}"/> </multitrigger> </style.triggers> </style> <style x:key="{x:type local:datagridex}" targettype="{x:type local:datagridex}"> <setter property="headersvisibility" value="all" /> <setter property="rowstyle" value="{staticresource datagridrowstyle}" /> <setter property="cellstyle" value="{staticresource datagridcellstyle}" /> <setter property="columnheaderstyle" value="{staticresource datagridcolumnheadercustomtemplatestyle}" /> <setter property="autogeneratecolumns" value="false"/> <setter property="template"> <setter.value> <controltemplate targettype="{x:type local:datagridex}"> <border x:name="dg_fullborder" snapstodevicepixels="true" background="{templatebinding background}" borderbrush="{templatebinding borderbrush}" borderthickness="{templatebinding borderthickness}" padding="{templatebinding padding}"> <scrollviewer x:name="dg_scrollviewer" focusable="false"> <scrollviewer.template> <controltemplate targettype="{x:type scrollviewer}"> <grid> <grid.rowdefinitions> <rowdefinition height="auto"/> <rowdefinition height="*"/> <rowdefinition height="auto"/> </grid.rowdefinitions> <grid.columndefinitions> <columndefinition width="auto"/> <columndefinition width="*"/> <columndefinition width="auto"/> </grid.columndefinitions> <button width="{binding path=cellspanelhorizontaloffset, relativesource={relativesource findancestor, ancestorlevel=1, ancestortype={x:type datagrid}}}" focusable="false" command="{x:static datagrid.selectallcommand}"> <button.visibility> <binding path="headersvisibility" relativesource="{relativesource findancestor, ancestorlevel=1, ancestortype={x:type datagrid}}"> <binding.converterparameter> <datagridheadersvisibility>all</datagridheadersvisibility> </binding.converterparameter> </binding> </button.visibility> <button.template> <controltemplate targettype="{x:type button}"> <grid> <rectangle fill="{dynamicresource {x:static systemcolors.controlbrushkey}}" x:name="border" snapstodevicepixels="true"/> <polygon fill="#ff000000" stretch="uniform" horizontalalignment="right" margin="8,8,3,3" x:name="arrow" verticalalignment="bottom" opacity="0.15" points="0,10 10,10 10,0"/> </grid> <controltemplate.triggers> <trigger property="ismouseover" value="true"> <setter property="stroke" targetname="border" value="{dynamicresource {x:static systemcolors.controldarkbrushkey}}"/> </trigger> <trigger property="ispressed" value="true"> <setter property="fill" targetname="border" value="{dynamicresource {x:static systemcolors.controldarkbrushkey}}"/> </trigger> <trigger property="isenabled" value="false"> <setter property="visibility" targetname="arrow" value="collapsed"/> </trigger> </controltemplate.triggers> </controltemplate> </button.template> </button> <datagridcolumnheaderspresenter x:name="part_columnheaderspresenter" grid.column="1"> <datagridcolumnheaderspresenter.visibility> <binding path="headersvisibility" relativesource="{relativesource findancestor, ancestorlevel=1, ancestortype={x:type datagrid}}"> <binding.converterparameter> <datagridheadersvisibility>column</datagridheadersvisibility> </binding.converterparameter> </binding> </datagridcolumnheaderspresenter.visibility> </datagridcolumnheaderspresenter> <scrollcontentpresenter x:name="part_scrollcontentpresenter" grid.columnspan="2" grid.row="1" content="{templatebinding content}" contentstringformat="{templatebinding contentstringformat}" contenttemplate="{templatebinding contenttemplate}" cancontentscroll="{templatebinding cancontentscroll}" canhorizontallyscroll="false" canverticallyscroll="false"/> <scrollbar x:name="part_verticalscrollbar" visibility="{templatebinding computedverticalscrollbarvisibility}" grid.column="2" grid.row="1" orientation="vertical" viewportsize="{templatebinding viewportheight}" maximum="{templatebinding scrollableheight}" value="{binding path=verticaloffset, mode=oneway, relativesource={relativesource templatedparent}}"/> <grid grid.column="1" grid.row="2"> <grid.columndefinitions> <columndefinition width="{binding path=nonfrozencolumnsviewporthorizontaloffset, relativesource={relativesource findancestor, ancestorlevel=1, ancestortype={x:type datagrid}}}"/> <columndefinition width="*"/> </grid.columndefinitions> <scrollbar x:name="part_horizontalscrollbar" visibility="{templatebinding computedhorizontalscrollbarvisibility}" grid.column="1" orientation="horizontal" viewportsize="{templatebinding viewportwidth}" maximum="{templatebinding scrollablewidth}" value="{binding path=horizontaloffset, mode=oneway, relativesource={relativesource templatedparent}}"/> </grid> </grid> </controltemplate> </scrollviewer.template> <border x:name="dg_contentborder" snapstodevicepixels="true" background="{templatebinding background}" borderbrush="{templatebinding borderbrush}" borderthickness="{templatebinding borderthickness}" padding="{templatebinding padding}"> <itemspresenter snapstodevicepixels="{templatebinding snapstodevicepixels}"/> </border> </scrollviewer> </border> </controltemplate> </setter.value> </setter> </style> </resourcedictionary>
thanks much!
i suspect still using system.windows.controls.datagridrow type datagridex's rows. use instances of own datagridrowex type, must override getcontainerforitemoverride() method in datagridex class:
public class datagridrowex : datagridrow { // can add custom dependency property here } public class datagridex : datagrid { //... protected override dependencyobject getcontainerforitemoverride() { return new datagridrowex(); } }
Comments
Post a Comment