c# - Databind text to a string property -


i'm new silverlight , concept of data-binding , still fail on resolving problem. haven't manage find solution after few days of research.

here problem :

i correctly bind string property text of textblock can see below :

mainpage.xaml

<grid  background="blue"  datacontext="{staticresource wp8displayable}">         <textblock x:name="tbcanvastitle" textwrapping="wrap" text="{binding titledisplayable}" fontweight="bold" horizontalalignment="center"/>           </grid> 

wp8displayable.cs

public class wp8displayable : idisplayable, inotifypropertychanged {      public string title { get; set; }       #region inotifypropertychanged members       public string titledisplayable     {                 {             return title;         }         set         {             if (title != value)             {                 title = value;                 notifypropertychanged("titledisplayable");             }         }     }        public event propertychangedeventhandler propertychanged;      // used notify page data context property changed     private void notifypropertychanged(string propertyname)     {         if (propertychanged != null)         {             propertychanged(this, new propertychangedeventargs(propertyname));         }     }      #endregion      public void settitle(string s)     {         this.title = s;          notifypropertychanged("titledisplayable");     } } 

i have thread in mainpage.xaml.cs can instantiate 1 or more instance of wp8displayable class. when 1 of instance call settitle(string s) text in textblock not update, seems datacontext not set properly.

edit :

my thread launched in mainpage.xaml.cs in mainpage_loaded(object sender, routedeventargs e) method , :

  var instancewp8displayable = new wp8displayable();   //tbcanvastitle.datacontext = instancewp8displayable;   here on xaml   instancewp8displayable.settitle("my title"); 

edit 2 : app.xaml

<application x:class="ams.app" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:phone="clr-namespace:microsoft.phone.controls;assembly=microsoft.phone" xmlns:shell="clr-namespace:microsoft.phone.shell;assembly=microsoft.phone" xmlns:windows="clr-namespace:system.windows;assembly=system.windows" xmlns:ioc="clr-namespace:ams.controller">  <!--application resources--> <application.resources>      <windows:resourcedictionary>         <local:localizedstrings xmlns:local="clr-namespace:ams" x:key="localizedstrings"/>         <ioc:locator x:key="locator" x:name="locator" />     </windows:resourcedictionary> </application.resources> 

mainpage.xaml

    <grid x:name="layoutroot" background="white">         <grid  background="blue"  datacontext="{binding source={staticresource locator}, path=wp8displayable}">         <textblock x:name="tbcanvastitle" textwrapping="wrap" text="{binding titledisplayable}" fontweight="bold" horizontalalignment="center"/>               </grid>     <grid.datacontext>         <local:wp8displayable />     </grid.datacontext>     </grid> 

how can dynamically set datacontext in case ? , is possible link more 1 instance same object ?

if clue or feels question not clear enough don't hesitate tell me.

thank you.

i recommend check out mvvmlight eliminate lot of boiler plate code have write e.g. inotifypropertychanged. further provides ioc container (inversion of control) commonly used task trying perform. can implement simple version of 1 on own (see bellow).

you can set datacontext in xaml need class provides object, instance write class (i'm assuming add directly project , not in subfolder):

public class locator {     public wp8displayable wp8displayable     {         { return new wp8displayable(); }     } } 

next have register locator class in app.xaml can reference within view:

<application     ...     xmlns:windows="clr-namespace:system.windows;assembly=system.windows"     xmlns:ioc="clr-namespace:yourappname">   <!--application resources-->   <application.resources>            <windows:resourcedictionary>          <local:localizedstrings xmlns:local="clr-namespace:ams" x:key="localizedstrings"/>          <ioc:locator x:key="locator" x:name="locator" />      </windows:resourcedictionary>  </application.resources>            ...  </application> 

now can set datacontext in xaml:

<grid  background="blue"  datacontext="{binding source={staticresource locator}, path=wp8displayable}">         <textblock x:name="tbcanvastitle" textwrapping="wrap" text="{binding titledisplayable}" fontweight="bold" horizontalalignment="center"/>           </grid> 

hth


Comments

Popular posts from this blog

scala - 'wrong top statement declaration' when using slick in IntelliJ -

c# - DevExpress.Wpf.Grid.InfiniteGridSizeException was unhandled -

PySide and Qt Properties: Connecting signals from Python to QML -