image - WPF Error: Cannot find governing FrameworkElement for target element -
i've got datagrid row has image. image bound trigger state. when state changes want change image.
the template set on headerstyle of datagridtemplatecolumn. template has bindings. first binding day shows day , state changes image trigger.
these properties set in viewmodel.
properties:
public class headeritem { public string day { get; set; } public validationstatus state { get; set; } } this.headeritems = new observablecollection<headeritem>(); (int = 1; < 15; i++) { this.headeritems.add(new headeritem() { day = i.tostring(), state = validationstatus.nieuw, }); }
datagrid:
<datagrid x:name="personeelsprestatiesdatagrid" horizontalalignment="stretch" verticalalignment="stretch" autogeneratecolumns="false" selectionmode="single" itemssource="{binding caregiverperformances}" frozencolumncount="1" > <datagridtemplatecolumn headerstyle="{staticresource headercenteralignment}" header="{binding headeritems[1]}" width="50"> <datagridtemplatecolumn.celleditingtemplate> <datatemplate> <textbox text="{ binding performances[1].duration,converter={staticresource timespanconverter},mode=twoway}"/> </datatemplate> </datagridtemplatecolumn.celleditingtemplate> <datagridtemplatecolumn.celltemplate> <datatemplate> <textblock textalignment="center" text="{ binding performances[1].duration,converter={staticresource timespanconverter}}"/> </datatemplate> </datagridtemplatecolumn.celltemplate> </datagridtemplatecolumn> </datagrid>
datagrid headerstyletemplate:
<style x:key="headercenteralignment" targettype="{x:type datagridcolumnheader}"> <setter property="horizontalcontentalignment" value="center"/> <setter property="template"> <setter.value> <controltemplate targettype="{x:type datagridcolumnheader}"> <grid> <grid.rowdefinitions> <rowdefinition /> <rowdefinition /> </grid.rowdefinitions> <textblock grid.row="0" text="{binding day}" /> <image x:name="imagevalidation" grid.row="1" width="16" height="16" source="{staticresource imgbevestigd}" /> </grid> <controltemplate.triggers> <multidatatrigger > <multidatatrigger.conditions> <condition binding="{binding state}" value="nieuw"/> </multidatatrigger.conditions> <setter targetname="imagevalidation" property="source" value="{staticresource imggeenstatus}"/> </multidatatrigger> </controltemplate.triggers> </controltemplate> </setter.value> </setter> </style>
now when startup project images doesn't show , error:
system.windows.data error: 2 : cannot find governing frameworkelement or frameworkcontentelement target element. bindingexpression:path=headeritems[0]; dataitem=null; target element 'datagridtemplatecolumn' (hashcode=26950454); target property 'header' (type 'object')
any idea why error showing? thanx in advance
sadly datagridcolumn
hosted under datagrid.columns
not part of visual
tree , therefore not connected data context of datagrid. bindings not work properties such visibility
or header
etc (although these properties valid dependency properties!).
now may wonderhow possible? isn't binding
property supposed bound data context? hack. binding not work. datagrid cells copy / clone binding object , use displaying own contents!
so solving issue, assume headeritems
property of object set datacontext
of parent view. can connect datacontext
of view datagridcolumn
via call proxyelement
.
the example below illustrates how connect logical child such contextmenu
or datagridcolumn
parent view's datacontext
<window x:class="wpfapplicationmultithreading.window5" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:vb="http://schemas.microsoft.com/wpf/2008/toolkit" title="window5" height="300" width="300" > <grid x:name="mygrid"> <grid.resources> <frameworkelement x:key="proxyelement" datacontext="{binding}"/> </grid.resources> <grid.datacontext> <textblock text="text column header" tag="tag columne header"/> </grid.datacontext> <contentcontrol visibility="collapsed" content="{staticresource proxyelement}"/> <vb:datagrid autogeneratecolumns="false" x:name="mydatagrid"> <vb:datagrid.itemssource> <x:array type="{x:type textblock}"> <textblock text="1" tag="1.1"/> <textblock text="2" tag="1.2"/> <textblock text="3" tag="2.1"/> <textblock text="4" tag="2.2"/> </x:array> </vb:datagrid.itemssource> <vb:datagrid.columns> <vb:datagridtextcolumn header="{binding datacontext.text, source={staticresource proxyelement}}" binding="{binding text}"/> <vb:datagridtextcolumn header="{binding datacontext.tag, source={staticresource proxyelement}}" binding="{binding tag}"/> </vb:datagrid.columns> </vb:datagrid> </grid> </window>
the view above encountered same binding error have found if did not have implemented proxyelement hack. proxyelement frameworkelement steals datacontext
main view , offers logical child such contextmenu
or datagridcolumn
. must hosted content
invisible contentcontrol
under same view.
i hope guides in correct direction.
Comments
Post a Comment