C# WPF InotifyPropertyChange Confusion -
i converting vbnet winforms project c# wpf. new both trying figure out.
i have project 2 wpf windows , viewmodel referenced project.
public class sheepviewmodel : inotifypropertychanged { private string _currenteventname; public string currenteventname { { return _currenteventname; } set { _currenteventname = value; onpropertychanged("currenteventname"); } } static sheepviewmodel _details; public static sheepviewmodel getdetails() { if (_details == null) _details = new sheepviewmodel(); return _details; } public event propertychangedeventhandler propertychanged; private void onpropertychanged(string prop) { if (propertychanged != null) propertychanged(this, new propertychangedeventargs(prop)); console.writeline(prop + " has changed"); } }
the window display things looks this.
<window xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:vm="clr-namespace:sheepviewmodel;assembly=sheepviewmodel" xmlns:shadereffectlibrary="clr-namespace:shadereffectlibrary;assembly=shadereffectlibrary" x:class="sheep_score_3._1.scorescreen" mc:ignorable="d" title="scorescreen" height="540" width="920" windowstartuplocation="centerscreen"> <window.datacontext> <vm:sheepviewmodel/> </window.datacontext> <viewbox x:name="stand1viewbox" rendertransformorigin="0.5,0.5"> <canvas x:name="standviewbox" height="112.513" margin="0,1100,2,0" verticalalignment="bottom" rendertransformorigin="0.493,0.473" background="#ff999999" width="2268"> <textblock x:name="currenteventname" canvas.left="270.9" textwrapping="wrap" width="1104.815" fontfamily="calibri" fontsize="29.333" fontstyle="italic" canvas.top="-1.649" text="{binding path=currenteventname}"/> </canvas> </viewbox>
my mainwindow control window looks this
<window x:class="sheep_score_3._1.mainwindow" xmlns:xctk="http://schemas.xceed.com/wpf/xaml/toolkit" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:vm="clr-namespace:sheepviewmodel;assembly=sheepviewmodel" mc:ignorable="d" title="mainwindow" height="433.689" width="941.194"> <window.datacontext> <vm:sheepviewmodel/> </window.datacontext> <grid margin="0,0,0,0"> <textbox x:name="currenteventname" height="23" margin="131.01,163.013,0,0" textwrapping="wrap" verticalalignment="top" horizontalalignment="left" width="327.151" text="{binding currenteventname, mode=twoway}"/> </grid>
now, if type control window textbox appears on display window in textblock, bindings working , can see notifyproperychange fire. that's working great.
however, if change property programmatically, can still see notifypropertychange fire, textbox , textblock not update new value.
sheepviewmodel.sheepviewmodel.getdetails().currenteventname = "this new value";
am missing here? guidance on welcome, i've checked calling right property name , googled couple of days try , figure out, i'm stuck.
i'm surprised text updates work you; doesn't on side. here's why:
you have following snippet in both windows:
<window.datacontext> <vm:sheepviewmodel/> </window.datacontext>
this says: create new instance of sheepviewmodel , set window's datacontext. since have in both windows, each window have separate instance of viewmodel. currenteventname
property instance property, value won't shared between instances , won't update properly.
when try programmatically update value, call sheepviewmodel.getdetails()
. creates yet instance, totally unrelated ones used controls. therefore, don't see updates.
what want of above use 1 single viewmodel instance. this, can set datacontext of windows code behind. instead of using above xaml snippet, can use following constructor in windows:
public displaywindow() { initializecomponent(); this.datacontext = sheepviewmodel.getdetails(); }
this ensures windows refer singleton instance retrieved through getdetails
method.
Comments
Post a Comment