c# - Simple databinding not working -


i'm new wpf , i'm trying make simple app, stopwatch. works fine if i'm not doing data binding. here's xaml.

<window x:class="stopwatch.mainwindow"     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"     xmlns:s="clr-namespace:stopwatch"     title="mainwindow" height="318" width="233"> <window.resources>     <s:stopwatchviewmodel x:key="swviewmodel" x:name="swviewmodel"></s:stopwatchviewmodel> </window.resources> <grid datacontext="{staticresource swviewmodel}">      <grid.rowdefinitions>         <rowdefinition height="auto"/>         <rowdefinition height="auto"/>         <rowdefinition height="128*" />     </grid.rowdefinitions>     <textblock grid.row="1" height="49" horizontalalignment="left" margin="42,50,0,0" name="txttime" text="{binding path=message}" verticalalignment="top" width="147" fontsize="20" textalignment="center" />     <button content="start" grid.row="2" height="23" horizontalalignment="left" margin="12,15,0,0" name="startbtn" verticalalignment="top" width="58" click="startbtn_click" />     <button content="stop" grid.row="2" height="23" horizontalalignment="left" margin="76,15,0,0" name="stopbtn" verticalalignment="top" width="58" click="stopbtn_click" />     <button content="reset" grid.row="2" height="23" horizontalalignment="left" margin="140,15,0,0" name="resetbtn" verticalalignment="top" width="59"/> </grid> 

and here code in mainwindow

public partial class mainwindow : window {     private stopwatchviewmodel stopwatch;      public mainwindow()     {         initializecomponent();         stopwatch = new stopwatchviewmodel();     }      private void startbtn_click(object sender, routedeventargs e)     {         stopwatch.start();     }      private void stopbtn_click(object sender, routedeventargs e)     {         stopwatch.stop();     } } 

and here's code in stopwatchviewmodel.cs

class stopwatchviewmodel : inotifypropertychanged {     private dispatchertimer timer;     private stopwatch stopwatch;     private string message;      public event propertychangedeventhandler propertychanged;      public string message     {                 {             return message;         }         set         {             if (message != value)             {                 message = value;                 onpropertychanged("message");             }         }     }      public stopwatchviewmodel()     {         timer = new dispatchertimer();         stopwatch = new stopwatch();         timer.tick += new eventhandler(timer_tick);         timer.start();         stopwatch.reset();     }      public void start()     {         stopwatch.start();     }      public void stop()     {         stopwatch.stop();     }      private void timer_tick(object sender, eventargs e)     {         message = stopwatch.elapsed.tostring(); // doesn't work.         // message = "hello"; not work too!     }      private void onpropertychanged(string propertyname)     {         if (propertychanged != null)         {             propertychanged(this, new propertychangedeventargs(propertyname));         }     }  } 

i don't know got wrong.

edit: got working. here's working code anyone's reference.

xaml, change original this

<window x:class="stopwatch.mainwindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:s="clr-namespace:stopwatch" title="mainwindow" height="318" width="233"> <grid> // partial code 

and in behind code, change constructor based on erno's suggestion.

public mainwindow()     {         initializecomponent();         viewmodel = new stopwatchviewmodel();         this.datacontext = viewmodel;     } 

thanks guys!

just replace message property , work:

 public string message  {      { return (string)getvalue(messageproperty); }      set { setvalue(messageproperty, value); }  }   public static readonly dependencyproperty messageproperty =      dependencyproperty.register("message", typeof(string),       typeof(mainwindow), new uipropertymetadata(string.empty)); 

edit after posting solution noticed changed code viewmodel solution... feel free ignore or go first set of code.

in new code creating 2 instances of viewmodel, 1 in code , 1 in resources. not manipulating 1 in code , binding 1 in resources(xaml)

edit: change constructor this:

public mainwindow() {     initializecomponent();     stopwatch = new stopwatchviewmodel();     this.datacontext = stopwatch; } 

that's all


Comments

Popular posts from this blog

java - SNMP4J General Variable Binding Error -

windows - Python Service Installation - "Could not find PythonClass entry" -

Determine if a XmlNode is empty or null in C#? -