wpf - Need assistance with making a button in one UserControl update the GUI in another UserControl using Properties in C# -


i'm trying code program if click on either listbox item or button in 1 usercontrol, update textbox in usercontrol. can't seem figure out how working dependencyproperties.

listbox.xaml

<usercontrol x:class="testdp3.listboxusercontrol"          xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"          xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"          xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"           xmlns:local="clr-namespace:testdp3"> <usercontrol.datacontext>     <local:myviewmodel/> </usercontrol.datacontext> <grid>     <listbox x:name="lstbox" horizontalalignment="left" height="144" margin="21,23,0,0" verticalalignment="top" width="149" selectionchanged="selector_onselectionchanged"/>      <button content="button" horizontalalignment="left" margin="105,231,0,0" verticalalignment="top" width="75" command="{binding testcommand}"/>     <textblock horizontalalignment="left" margin="52,272,0,0" textwrapping="wrap" text="{binding mydp.result}" verticalalignment="top"/>  </grid> 

vninfo.xaml

<usercontrol x:class="testdp3.vninfousercontrol"              xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"              xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"              xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"               xmlns:d="http://schemas.microsoft.com/expression/blend/2008"               mc:ignorable="d"               d:designheight="300" d:designwidth="300"              xmlns:local="clr-namespace:testdp3">     <usercontrol.datacontext>         <local:myviewmodel/>     </usercontrol.datacontext>     <grid>         <textblock horizontalalignment="left" margin="10,102,0,0" textwrapping="wrap" text="dependencyproperty is: " verticalalignment="top"/>             <textblock horizontalalignment="left" margin="159,102,0,0" textwrapping="wrap" text="{binding mydp.result, mode=twoway}" verticalalignment="top"/>     </grid> </usercontrol> 

myviewmodel.cs

public class myviewmodel : inotifypropertychanged     {         private mydp _mydp;         public event propertychangedeventhandler propertychanged;         public myviewmodel()         {             _mydp = new mydp();              testcommand = new mycommand(samplemethod);          }          public mydp mydp         {             { return _mydp; }             set             {                 _mydp = value;                 raisepropertychanged("mydp");                           }         }          private void raisepropertychanged(string propertyname)         {             propertychangedeventhandler handler = this.propertychanged;             if (handler != null)             {                 handler(this, new propertychangedeventargs(propertyname));             }         }          public icommand testcommand { get; set; }          private int counter = 0;         private int samplemethod()         {             mydp.result = counter++;             return mydp.result;           }     } 

mydp.cs

public class mydp : dependencyobject     {         public int result         {             { return (int)getvalue(resultproperty); }             set { setvalue(resultproperty, value); }         }         public static readonly dependencyproperty resultproperty =             dependencyproperty.register("result", typeof(int), typeof(mydp) ,new propertymetadata(0)              );             } 

here link visual studio project: https://dl.dropboxusercontent.com/u/22398345/testdp.zip

both views must access same object (viewmodel). suspect calls to

<local:myviewmodel/> 

in each of xaml files creating separate objects. you'll need create single myviewmodel object in code-behind somewhere:

myviewmodel vm = new myviewmodel(); 

and pass reference of each of views, either through constructor:

listboxusercontrol(myviewmodel vm) { ... } 

or property:

listboxusercontrol.myviewmodel = vm; 

then can set datacontext in code-behind:

this.datacontext = vm; 

Comments

Popular posts from this blog

how to insert data php javascript mysql with multiple array session 2 -

multithreading - Exception in Application constructor -

windows - CertCreateCertificateContext returns CRYPT_E_ASN1_BADTAG / 8009310b -