c# - Transfer data Between two Usercontrol inside of Mainwindows -
goal: input info located within of textbox(usercontrol_menu.xaml) shall transferred , used , displayed in label within of usercontrol_number1.xaml. transportation of input info executed when have pressed button "enter" or clicked on button "send" within of usercontrol_menu.xaml.
problem: have concrete illustration combined on source code in order accomplish goal?
information: - mainwindows.xaml located in separated project.
xaml -- mainwindow.xaml <window xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:usercontrol_menu="clr-namespace:usercontrol_menu;assembly=usercontrol_menu" xmlns:usercontrol_number1="clr-namespace:usercontrol_number1;assembly=usercontrol_number1" x:class="mainwindows.mainwindow" title="mainwindow" height="350" width="525"> <grid> <usercontrol_menu:usercontrol1 horizontalalignment="left" verticalalignment="top" height="124" width="434" margin="24,28,0,0"/> <usercontrol_number1:usercontrol1 horizontalalignment="left" margin="24,176,0,0" verticalalignment="top" height="115" width="434"/> </grid> </window> -- usercontrol_menu usercontrol1.xaml <usercontrol x:class="usercontrol_menu.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 background="#fff3cbcb"> <button x:name="btn_send" content="send" horizontalalignment="left" margin="99,100,0,0" verticalalignment="top" width="75"/> <textbox x:name="txt_input" horizontalalignment="left" height="23" margin="54,57,0,0" textwrapping="wrap" text="" verticalalignment="top" width="120"/> </grid> </usercontrol> -- usercontrol_number1 usercontrol1.xaml <usercontrol x:class="usercontrol_number1.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 background="#ffff9d9d"> <label x:name="lbl_text" content="text" horizontalalignment="left" margin="31,44,0,0" verticalalignment="top"/> </grid> </usercontrol> c# code below:
namespace mainwindows { /// <summary> /// interaction logic mainwindow.xaml /// </summary> public partial class mainwindow : window { public mainwindow() { initializecomponent(); } private void test() { usercontrol_menu.usercontrol1 aaa = new usercontrol_menu.usercontrol1(); usercontrol_number1.usercontrol1 bbb = new usercontrol_number1.usercontrol1(); aaa.onparameterchange += bbb.onusercontrol1parameterchange; } } } ---------------- namespace usercontrol_menu { /// <summary> /// interaction logic usercontrol1.xaml /// </summary> public partial class usercontrol1 : usercontrol { public usercontrol1() { initializecomponent(); } public delegate void parameterchange(string parameter); public parameterchange onparameterchange { get; set; } private void btn_send_click(object sender, routedeventargs e) { parameterchange myparameterchange = new parameterchange(onparameterchange); onparameterchange("test"); } } } ----------------- namespace usercontrol_number1 { /// <summary> /// interaction logic usercontrol1.xaml /// </summary> public partial class usercontrol1 : usercontrol { public usercontrol1() { initializecomponent(); } public void onusercontrol1parameterchange(string pdata) { lbl_text.content = pdata; } } }
as question not how phone call functions in main view model other view models? question, did not close duplicate. however, believe reply question lies in reply linked question , 1 links , utilize delegates.
the difference questions using view models, while appears you'll using code behind. either way, makes little difference, because can utilize delegates in same way in code behind long have mutual parent. mainwindow.xaml.cs mutual parent.
update >>>
the link have provided not fit purpose. in case between 2 user controls located within of mainwindows.xaml
that's i've mentioned. define delegate in usercontrol_menu.xaml.cs file , handler in usercontrol_number1.xaml.cs file , connect them in mainwindow.xaml.cs. extending illustration other linked question (passing parameters between viewmodels), this:
in mainwindow.xaml.cs:
usercontrol_menu.parameterchange += usercontrol_number1.onusercontrol1parameterchange; read linked answers 1 time again , linked pages on msdn , you'll it.
c# wpf xaml
No comments:
Post a Comment