c# - BindingExpression path error when creating the Window (no VM bound yet) -


i trying follow mvvm pattern, i'm having hard time binding vm property dependency property of usercontrol of mine. i'm not sure whether got initialization flow wrong or missed detail.

here application startup event handler:

private void application_startup(object sender, startupeventargs e) {     mainviewmodel viewmodel = new mainviewmodel();     mainwindow window = new mainwindow();     viewmodel.bindview(window);     window.show(); } 

this mainviewmodel (viewmodelbase comes mvvm light):

public class mainviewmodel : viewmodelbase {     private project m_currentproject = new project();     public project currentproject     {         { return m_currentproject; }         private set         {             m_currentproject = value;         }     } } 

and mainwindow.xaml:

<window x:class="wst_desktop.mainwindow"     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"     xmlns:d="http://schemas.microsoft.com/expression/blend/2008"     xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"     xmlns:local="clr-namespace:wst_desktop"     xmlns:controls="clr-namespace:wst_desktop.controls"     mc:ignorable="d"     title="mainwindow" height="540" width="960">     <grid>         <grid.rowdefinitions>             <rowdefinition height="auto" />             <rowdefinition />         </grid.rowdefinitions>          <controls:projectcontrol project="{binding path=currentproject, converter={wstd:debugconverter}}" />     </grid> </window> 

i error databinding listener (lines broken readability):

bindingexpression path error: 'currentproject' property not found on 'object' ''project' (hashcode=30400195)'.  bindingexpression:path=currentproject; dataitem='project' (hashcode=30400195);  target element 'projectcontrol' (name=''); target property 'project' (type 'project') 

the error reported on line new mainwindow() startup handler. also, debugconverter attached binding never hit.

i think understand mainwindow created without datacontext (it bound in next line), , fails binding property of viewmodel (that doesn't know of yet, of course). correct? how fix this?

ps of course, projectcontrol has public property named project of correct project type.

and bindview method gets called in startup event handler:

public static void bindview(this viewmodelbase viewmodel, control i_control) {     i_control.datacontext = viewmodel; } 

which translates to

window.datacontext = viewmodel; 

more info

i tried change this

<controls:projectcontrol project="{binding path=currentproject, converter={wstd:debugconverter}}" /> 

to this

<controls:projectcontrol datacontext="{binding path=currentproject, converter={wstd:debugconverter}}" /> 

and error gone. i'm not sure 1 correct, though: should project object (which sort of "root" model object of projectcontrol) dependency property or datacontext?

let's see binding error about:

bindingexpression path error: 'currentproject' property not found on 'object' ''project' (hashcode=30400195)'.
bindingexpression:path=currentproject; dataitem='project' (hashcode=30400195);
target element 'projectcontrol' (name=''); target property 'project' (type 'project')

you can lot of info out of it.

first of all, binding engine telling there error in path property of binding, because binding engine cannot find property named currentproject on object serving current datacontext. should know, binding engine uses datacontext object source when don't explicitly specify different source (e.g. using elementname or source properties of binding).

the binding engine states also, current datacontext object of type project , not of type mainviewmodel. fact should make suspicious @ code.

the second line repeats info: datacontext (or dataitem in message) object of type project, , we're trying property currentproject (what not possible, because property doesn't exist).

the third line shows binding's target: object of type projectcontrol , property named project (of type project).

so summarizing: view model binding engine dealing not 1 you're thinking you're binding to. there 1 explanation this: somewhere, there's code or binding sets view's datacontext object of type project.

and mention in comment, have found in code.


Comments

Popular posts from this blog

angular - Ionic slides - dynamically add slides before and after -

minify - Minimizing css files -

Add a dynamic header in angular 2 http provider -