c# - Binding to DataGrid of TabItem not Working using MVVM -


i have small application myself learn wpf , mvvm etc. have been using example josh smith found here construct own application. have got application adding tabitems, embedded datagrid bound observablecollection<resourceviewmodel> not showing data, see image below:

example

the datagrid section surrounded in blue. usercontrol seems showing in tab reason, not problem asking here. usercontrol contains datagrid bound follows

<datagrid itemssource="{binding resources}"            dataaccess:datagridtextsearch.searchvalue="{binding elementname=searchbox,                path=text, updatesourcetrigger=propertychanged}"            alternatingrowbackground="gainsboro"            alternationcount="2"            horizontalalignment="stretch"           verticalalignment="stretch"> ...</datagrid> 

the resources property defined in viewmodels namespace

internal class resourcedataviewmodel : workspaceviewmodel {     readonly resourcedatarepository resourcerepository;     public observablecollection<resourceviewmodel> resources { get; private set; }     ... } 

where resourceviewmodel holds information each row of datagrid. can confirm resource property populated. when use same model outside of mvvm , populate resource in same way works. can provide me , idea of why happening?

i have attempted set explicit path binding

itemssource="{binding path=(viewmodels:resources)}"  

but not work. time.


edit. address comments. set datacontext in app.xaml.cs file by

protected override void onstartup(startupeventargs e) {     base.onstartup(e);     mainwindow window = new mainwindow();      // create viewmodel      // main window binds.     mainwindowviewmodel mainwindowviewmodel = new mainwindowviewmodel();      // when viewmodel asks closed,      // close window.     eventhandler handler = null;     handler = delegate     {         mainwindowviewmodel.requestclose -= handler;         window.close();     };     mainwindowviewmodel.requestclose += handler;      // allow controls in window      // bind viewmodel setting      // datacontext, propagates down      // element tree.     window.datacontext = mainwindowviewmodel;     window.show(); } 

the xaml of mainwindow:

<window x:class="resourcestudio.views.mainwindow"         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"         xmlns:viewmodels="clr-namespace:resourcestudio.viewmodels"         xmlns:views="clr-namespace:resourcestudio.views"         title="mainwindow" height="629.4" width="814.4">    <window.resources>       <resourcedictionary source="mainwindowresources.xaml" />    </window.resources>    <grid>       <grid.columndefinitions>          <columndefinition width="284*"/>          <columndefinition width="567*"/>       </grid.columndefinitions>       <grid.rowdefinitions>          <rowdefinition height="48"/>          <rowdefinition height="*"/>          <rowdefinition height="24"/>       </grid.rowdefinitions>       <dockpanel keyboardnavigation.tabnavigation="none"                       background="#ffbec8d8"                   grid.columnspan="2"                   margin="0,0,0.4,0">          <menu dockpanel.dock="top"                     background="#fff9f9f9"                     borderbrush="black"                     keyboardnavigation.tabnavigation="cycle">             <menuitem header="_file">                <menuitem header="load _resource..."                           height="auto"                           command="{binding loadresourcecommand}"/>                <menuitem header="_add language..."                           height="auto"/>                <separator/>                <menuitem header="close _workspace"                           height="auto"                          command="{binding closecommand}"/>                <menuitem header="e_xit"                           height="auto" command="{binding closecommand}" />             </menuitem>             <menuitem header="_edit">             </menuitem>          </menu>          <toolbartray dockpanel.dock="top" maxheight="24" background="#fff9f9f9">             <toolbar background="#fff9f9f9">                <button toolbar.overflowmode="never">one</button>                <button>two</button>                <button>three</button>             </toolbar>          </toolbartray>       </dockpanel>       <grid grid.row="1" grid.columnspan="2" margin="0,0,0.4,23.6" grid.rowspan="2">          <grid.columndefinitions>             <columndefinition width="auto"/>             <columndefinition width="auto"/>             <columndefinition width="*"/>          </grid.columndefinitions>          <tabcontrol itemssource="{binding path=workspaces}"                       grid.column="2"                       horizontalalignment="stretch"                       verticalalignment="stretch"                       tabstripplacement="top"                       height="auto"                       width="auto">          </tabcontrol>       </grid>       <statusbar grid.row="2" grid.columnspan="2" margin="0,0.4,0.4,-0.4">          <statusbaritem dockpanel.dock="left" background="#ff007acc" margin="0,2,0,0">             <textblock text="ready" margin="5,0,0,0"/>          </statusbaritem>       </statusbar>    </grid> </window> 

where mainwindowresources.xaml is:

<resourcedictionary     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"     xmlns:viewmodels="clr-namespace:resourcestudio.viewmodels"    xmlns:views="clr-namespace:resourcestudio.views"    >    <!--this template applies resourcecontrol view instance of     resourcedataviewmodel class shown in main window.-->    <datatemplate datatype="{x:type viewmodels:resourcedataviewmodel}">       <views:resourcecontrol/>    </datatemplate>     <!--this template explains how render 'workspace'     content area in main window.-->    <datatemplate x:key="workspacestemplate">       <tabcontrol        issynchronizedwithcurrentitem="true"        itemssource="{binding}"        margin="4"/>    </datatemplate> </resourcedictionary> 

the full code resourcecontrol.xaml is:

<usercontrol x:class="resourcestudio.views.resourcecontrol"              xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"              xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"              xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"               xmlns:d="http://schemas.microsoft.com/expression/blend/2008"               xmlns:viewmodels="clr-namespace:resourcestudio.viewmodels"               xmlns:dataaccess="clr-namespace:resourcestudio.dataaccess"               mc:ignorable="d" d:designheight="300" d:designwidth="300" name="control">    <dockpanel datacontext="{binding elementname=control}" horizontalalignment="stretch" verticalalignment="stretch">       <textbox text="m"  dockpanel.dock="top" name="searchbox" />       <grid dockpanel.dock="top">          <border borderbrush="#ff007acc" borderthickness="2" horizontalalignment="stretch"                   verticalalignment="stretch">             <datagrid itemssource="{binding path=(viewmodels:resources)}"                        dataaccess:datagridtextsearch.searchvalue="{binding elementname=searchbox, path=text, updatesourcetrigger=propertychanged}"                        alternatingrowbackground="gainsboro" alternationcount="2" horizontalalignment="stretch"                       verticalalignment="stretch">                <datagrid.resources>                   <dataaccess:searchvalueconverter x:key="searchvalueconverter"/>                   <style targettype="{x:type datagridcell}">                      <setter property="dataaccess:datagridtextsearch.istextmatch">                         <setter.value>                            <multibinding converter="{staticresource searchvalueconverter}">                               <binding relativesource="{relativesource self}" path="content.text" />                               <binding relativesource="{relativesource self}" path="(dataaccess:datagridtextsearch.searchvalue)" />                            </multibinding>                         </setter.value>                      </setter>                      <style.triggers>                         <trigger property="dataaccess:datagridtextsearch.istextmatch" value="true">                            <setter property="background" value="orange" />                         </trigger>                      </style.triggers>                   </style>                </datagrid.resources>                <datagrid.cellstyle>                   <style targettype="datagridcell" basedon="{staticresource {x:type datagridcell}}">                      <style.triggers>                         <trigger property="isselected" value="true">                            <setter property="background" value="#ff007acc"/>                            <setter property="foreground" value="white"/>                         </trigger>                      </style.triggers>                   </style>                </datagrid.cellstyle>             </datagrid>          </border>       </grid>    </dockpanel> </usercontrol> 

the textbox bound datagrid. when user types textbox datagrid filters , highlights cells contains required text. however, not problem , code works, binding datagrid interested in. again tour time.

edit2: according @dkozl's comments have removed datacontext="{binding elementname=control}" dockpanel declaration, have

<dockpanel horizontalalignment="stretch"             verticalalignment="stretch"> ... 

and in mainwindowresource.xaml have

<resourcedictionary     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"     xmlns:viewmodels="clr-namespace:resourcestudio.viewmodels"    xmlns:views="clr-namespace:resourcestudio.views"    >    <!--this template applies resourcecontrol view instance of     resourcedataviewmodel class shown in main window.-->    <datatemplate datatype="{x:type viewmodels:resourcedataviewmodel}">       <views:resourcecontrol datacontext="{binding}"/>    </datatemplate>     <!--this template explains how render 'workspace'     content area in main window.-->    <datatemplate x:key="workspacestemplate">       <tabcontrol        issynchronizedwithcurrentitem="true"        itemssource="{binding}"        margin="4"/>    </datatemplate> </resourcedictionary> 

this has not worked. datagrid in resourcecontrol not being populated. again time appreciated...

your usercontrol dockpanel.datacontext bound resourcecontrol control , not resourcedataviewmodel class. need instead bind datacontext of resourcecontrol in datatemplate. achive first remove datacontext="{binding elementname=control}" resourcecontrol.dockpanel , bind resourcecontrol.datacontext object <views:resourcecontrol datacontext={binding}"/>. need change datagrid items binding itemssource="{binding path=(viewmodels:resources)}" itemssource="{binding path=resources}".

not part of original question same template applies tab header , tab content because datatemplate type specific , in case tab header content , tab content same thing. solve issue remove datatemplate viewmodels:resourcedataviewmodel type , put directly main tabcontrol:

<tabcontrol.contenttemplate>    <datatemplate>       <views:resourcecontrol datacontext={binding}"/>    </datatemplat‌​e> </tabcontrol.contenttemplate> 

Comments

Popular posts from this blog

SPSS keyboard combination alters encoding -

Add new record to the table by click on the button in Microsoft Access -

CSS3 Transition to highlight new elements created in JQuery -