c# - MVVM Distribute Objects -


maybe small step solution can't far.

i did wpf tutorials datacontext , binding, can't how share context and/or binding between (e.g.) 2 pages.

for example when @ one: https://msdn.microsoft.com/en-us/library/ms754356%28v=vs.110%29.aspx

<label>enter name:</label> <textbox>     <textbox.text>         <binding source="{staticresource mydatasource}" path="name" updatesourcetrigger="propertychanged"/>     </textbox.text> </textbox>  <label>the name entered:</label> <textblock text="{binding source={staticresource mydatasource}, path=name}"/> 

this result:

enter image description here

it's easy example , there no problem running , understanding this, want is:

  1. fill textbox , label code behind. tried name textbox tb , call tb.text = "some text" - works. tried assign datacontext both textbox , label, create object , fill datacontext object - worked.
  2. placing label on page.

problem 2 1 hard me, in combination problem 1. example: when create object in page 1 constructor , assign datacontext (ofcourse) textbox on page 1 contain value.

i don't know how share 1 object declared in page 1 page 2 set datacontext.

maybe didn't find perfect tutorial or explanation me understand how datacontext , binding works , how can share objects between pages , windows.

can guys me out?

if need more informations, feel free ask ;)

public class myshareddatacontext : inotifypropertychanged {     private string _name;      public string name     {         { return _name; }         set         {             if (value == _name) return;             _name = value;             onpropertychanged();         }     } } 

in app.xaml create shared resource:

<application x:class="wpfapplication1.app"              xmlns:my="clr-namespace:wpfapplication1">     <application.resources>          <my:myshareddatacontext x:key="myshareddatacontext" /> 

now can use shared resource in both pages:

<textbox text="{binding name, source={staticresource myshareddatacontext}" /> 

if want sent name value, better don't access textbox directly, set in myshareddatacontext class:

var datacontext = (myshareddatacontext)findresource("myshareddatacontext"); datacontext.name = "john smith"; 

Comments

Popular posts from this blog

android - MPAndroidChart - How to add Annotations or images to the chart -

javascript - Add class to another page attribute using URL id - Jquery -

firefox - Where is 'webgl.osmesalib' parameter? -