mvvm - Get data from a user control in wpf to an user control? -
basically i'm new wpf. have user control - a. inside a, have user control b. when button on b pressed, value passed a. i`m trying wpf mvvm. kindly me.
//-------------------mainwindow------------------// public partial class mainwindow : window { public delegate void valuepassdelegate(int valuetoget); public event valuepassdelegate valuepassevent; public usercontrol1 usercontrol1obj = new usercontrol1(); public usercontrol2 usercontrol2obj = new usercontrol2(); public mainwindow() { initializecomponent(); valuepassevent += new valuepassdelegate(method1); usercontrol1obj.del = valuepassevent; } public void method1(int valuetoget) { usercontrol2obj.txtname.text = valuetoget.tostring(); } } //---------------------usercontrol1------------------// public partial class usercontrol1 : usercontrol { public delegate del; public int valuetopass = 0; public usercontrol1() { initializecomponent(); } public void method1() { del.dynamicinvoke(valuetopass); } private void button_click(object sender, routedeventargs e) { method1(); } } //-----------------usercontrol2--------------------// public partial class usercontrol2 : usercontrol { public int valuetoget; public usercontrol2() { initializecomponent(); } }
these codes of usercontrol1, usercontrol2, , mainwindow. kindly let me know error in this
mainwindow.xaml
<window x:class="testmultipleusercontrol.mainwindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:controls="clr-namespace:testmultipleusercontrol" title="mainwindow" height="350" width="525"> <grid> <controls:usercontrol1 name="usercontrol1obj" margin="10,-5,38,148"/> <controls:usercontrol2 name="usercontrol2obj" margin="153,171,38,10"/> </grid> </window>
mainwindow.xaml.cs
public partial class mainwindow : window { public delegate void valuepassdelegate(int valuetoget); public event valuepassdelegate valuepassevent; public mainwindow() { initializecomponent(); valuepassevent += new valuepassdelegate(method1); usercontrol1obj.del = valuepassevent; } public void method1(int valuetoget) { usercontrol2obj.txtname.text = valuetoget.tostring(); } }
usercontrol1.xaml
<usercontrol x:class="testmultipleusercontrol.usercontrol1" 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"> <grid> <button content="button" horizontalalignment="left" margin="77,121,0,0" verticalalignment="top" width="166" height="44" click="button_click"/> </grid> </usercontrol>
usercontrol1.xaml.cs
public partial class usercontrol1 : usercontrol { public delegate del; public int valuetopass = 0; public usercontrol1() { initializecomponent(); } public void method1() { del.dynamicinvoke(valuetopass); } private void button_click(object sender, routedeventargs e) { method1(); } }
usercontrol2.xaml
<usercontrol x:class="testmultipleusercontrol.usercontrol2" 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:designwidth="300" height="174.286"> <grid> <textbox x:name="txtname" horizontalalignment="left" height="46" margin="51,55,0,0" textwrapping="wrap" text="" verticalalignment="top" width="211"/> </grid> </usercontrol>
usercontrol2.xaml.cs
public partial class usercontrol2 : usercontrol { public usercontrol2() { initializecomponent(); } }
it work perfect !!!
Comments
Post a Comment