c# - Duplicate Model instantiated from InitializeComponent() -


i'm working on mvvm wpf application , have run dead end trying solve this. %)

have model class devicemodel instantiated app.xaml.cs. model implements inotifypropertychanged interface.

public partial class app : application {     public devicemodel devicemodelinstance { get; set; }      public app()     {         devicemodelinstance = new devicemodel();     } } 


in app.xaml:

<application.resources>     <vm:viewmodelbase x:key="viewmodelbaseapp"/>     <m:devicemodel x:key="devicemodelapp"/> </application.resources> 

then mainwindow.xaml instantiate viewmodelbase:

    <window.datacontext>          <binding source="{staticresource viewmodelbaseapp}"/>     </window.datacontext> 

then bind buttons in ui (mainwindow.xaml) commands in viewmodelbase, implement icommand interface:

    command="{binding inputphantomcommand, converter={staticresource inputphantomconverter}, source={staticresource viewmodelbaseapp}}" 

this works great! push button, command in viewmodelbase gets called , changes property of devicemodelinstance (instantiated in app.xaml.cs), in turn triggers propertychanged event, propagates other classes listening event. icommand bound buttons in ui work way.

however, when bind sliders properties in devicemodel class, i.e.:

    value="{binding inputchannel2.gain, converter={staticresource inputgainconverter}, source={staticresource devicemodelapp}}" 

all of sliders bound duplicate devicemodel class, gets instantiated @ initializecomponent() method in constructor of mainwindow.xaml.cs. when move of sliders, duplicate model reacts fine changes. of labels bound same properties sliders dynamically updated. yet, of events listened on original devicemodelinstance object (from app.xaml.cs) , no events occur, obviously.

i cannot figure out why of buttons interacting devicemodelinstance created in app.xaml.cs, while of sliders , corresponding labels interacting duplicate devicemodel() gets created @ initializecomponent() of mainwindow...

i suspect it's issue of referencing original devicemodelinstance correctly in xaml, don't know how this. thank you! )

so, in addition clues given in comments , clemens, somehow answer cleared me more - multiple instances of viewmodel

specifically, line:

to access in code-behind, grab adminviewmodel (adminviewmodel)this.datacontext.

in case, after relocating devicemodelinstance app class viewmodelbase class, setting public property, instantiating viewmodelbase instance via xaml:

    <window.datacontext>          <vm:viewmodelbase/>     </window.datacontext> 

and referencing specific viewmodelbase instance:

public partial class mainwindow : window {     public viewmodelbase vm;      public mainwindow()     {         vm = (viewmodelbase)this.datacontext;         initializecomponent();     } 

this works me intended because 1 instance of viewmodelbase (and subsequently 1 instance of devicemodel) gets instantiated via xaml , gets referenced in code-behind. however, still don't understand how same other way around, i.e. instantiating model objects in code , referencing them in xaml, not creating new instances.

thanks!


Comments

Popular posts from this blog

python Tkinter Capturing keyboard events save as one single string -

android - InAppBilling registering BroadcastReceiver in AndroidManifest -

javascript - Z-index in d3.js -