wpf - Require reusable DataCell ControlTemplate for different binding sources -
(visual studio 2010) web examples, have overridden controltemplate
of datagridtextcolumn
include additional image binded viewmodel.
<datagridtextcolumn.cellstyle> <style targettype="datagridcell"> <setter property="template"> <setter.value> <controltemplate targettype="datagridcell"> <grid name="root" background="{templatebinding background}"> <visualstatemanager.visualstategroups> <visualstategroup x:name="currentstates"> <visualstate x:name="regular" /> <visualstate x:name="current"> <storyboard> <doubleanimation storyboard.targetname="focusvisual" storyboard.targetproperty="opacity" to="1" duration="0" /> </storyboard> </visualstate> </visualstategroup> <visualstategroup x:name="validationstates"> <visualstate x:name="valid"/> <visualstate x:name="invalid"> <storyboard> <doubleanimation storyboard.targetname="invalidvisualelement" storyboard.targetproperty="opacity" duration="0" to="1"/> <coloranimation storyboard.targetname="focusvisual" storyboard.targetproperty="(fill).color" duration="0" to="#ffffffff"/> </storyboard> </visualstate> </visualstategroup> </visualstatemanager.visualstategroups> <grid.columndefinitions> <columndefinition/> <columndefinition width="auto"/> </grid.columndefinitions> <rectangle name="focusvisual" stroke="#ff6dbdd1" strokethickness="1" fill="#66ffffff" horizontalalignment="stretch" verticalalignment="stretch" ishittestvisible="false" opacity="0" /> <contentpresenter contentsource="content" /> <image source="{binding someproperty}" grid.column="1"/> <rectangle x:name="invalidvisualelement" ishittestvisible="false" strokethickness="1" stroke="#ffdc000c" horizontalalignment="stretch" verticalalignment="stretch" opacity="0"/> <rectangle name="rightgridline" grid.column="1" verticalalignment="stretch" width="1" /> </grid> </controltemplate> </setter.value> </setter> </style> </datagridtextcolumn.cellstyle>
the line of interest near bottom: <image source="{binding someproperty}" grid.column="1"/>
. want able reuse controltemplate
in several places within application. difference binding paths of column
, added image
. ideally declare textcolumn
, declare style , direct bindings minimal additional code:
<datagridtextcolumn binding="{binding typicalproperty}" cellstyle="mystyle" [this part can't -->] imagesourcebinding="{binding anypropertyinviewmodel}" >
i've come across multiple posts of users asking how create reusable controltemplate
, or templatecolumn
, or style
, columns of datagrid
using different binding paths, , no far no 1 seems have provided answer on how it. common suggestion try attachedproperty
binding doesn't work right.
surely there has better approach copy/paste above code each individual column, change binding path? mind utterly ridiculous, , flies in face of i've ever learned concept of coding redundancy... if column count dozens, or hundreds? stands severe limitation of customability of datagrid
... must missing here.
if understood correctly, need add in data class property, example - anothertextvalue
. example, have class myobject
:
public class myobject { public string basevalue { get; set; } // value textblock in datagridcell public string anothertextvalue { get; set; } }
observablecollection
of class:
public observablecollection<myobject> objects { get; set; }
style of datagridcell
desirable place resource of windows (window.resources), or in app.xaml
:
<window.resources> <style x:key="mycellstyle" targettype="{x:type datagridcell}"> ... </style> </window.resources>
textblock
in datagridcell
style this:
... <contentpresenter contentsource="content" /> <textblock text="{binding anothertextvalue}" grid.column="1" />
define datagridtextcolumn
:
<datagridtextcolumn cellstyle="{staticresource mycellstyle}" binding="{binding basevalue}" header="my super header" />
Comments
Post a Comment