xaml - DataBinding and EventToCommand -


i'm attempting use eventtocommand initialize viewmodel, command isn't firing. suspect it's because triggers section not within databound container, how can in example? i'm trying stick straight xaml if possible.

<window x:class="mvvmsample.home"         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:mvvmsample.viewmodels"         xmlns:i="clr-namespace:system.windows.interactivity;assembly=system.windows.interactivity"         xmlns:cmd="clr-namespace:galasoft.mvvmlight.command;assembly=galasoft.mvvmlight.extras.wpf4"         d:datacontext="{d:designinstance type=viewmodels:homeviewmodel, isdesigntimecreatable=true}"         mc:ignorable="d"         title="mainwindow" height="350" width="525">     <window.resources>         <viewmodels:homeviewmodel x:key="viewmodel" x:name="viewmodel" />     </window.resources>     <i:interaction.triggers>         <i:eventtrigger eventname="loaded">             <cmd:eventtocommand command="{binding loadedcommand}" />         </i:eventtrigger>     </i:interaction.triggers>     <grid datacontext="{staticresource viewmodel}">         <textblock text="{binding personcount}" />     </grid> </window>  

you right, datacontext part of problem,but solve using mvvm-light designed.

if using mvvm_light should use view model locator. main backbone of framework. used mvvm light learn mvvm principle. liked lot because simple , allowed me learn quick learning curve possible.

in mvvm-light declare viewmodellocator in app.xaml

<application.resources>     <resourcedictionary>         <!--global view model locator-->         <vm:viewmodellocator x:key="locator" d:isdatasource="true" />     </resourcedictionary>                </application.resources>  

then in view (be usercontrol or window etc) "attach" viewmodel view follows: notice datacontext declaration.

<usercontrol x:class="ftc.view.trackinglistview"             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:i="clr-namespace:system.windows.interactivity;assembly=system.windows.interactivity"             xmlns:cmd="http://www.galasoft.ch/mvvmlight"             mc:ignorable="d"              datacontext="{binding yourviewmodel, source={staticresource locator}}"             d:designheight="700" d:designwidth="1000"> 

this way view model locator mvvm light can either create singleton of viewmodel or unique instance need. can use ioc inject services constructor of viewmodel.

so example, if have viewmodel deals people objects datamodel, create people service performs crud operation , reference in viewmodel constructor parameters. allows me use either fake design time data or real data model. keep concerns decoupled, purpose of mvvm.

i recommend reading more mvvm-light framwork , building 1 of samples thier site galasoft.

see video

hope helps


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 -

javascript - jQuery .height() return 0 when visible but non-0 when hidden -