Goal:
The input data that is located inside of the textbox(usercontrol_menu.xaml) shall be transferred and then to be used and displayed in the label inside of usercontrol_number1.xaml. The transportation of the input data will be executed when you have pressed the button "enter" or clicked on the button "Send" inside of usercontrol_menu.xaml.
Problem:
Do you have a concrete example combined on my source code in order to achieve the goal?
Information:
- MainWindows.xaml is located in a 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 for 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 for 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 for UserControl1.xaml
/// </summary>
public partial class UserControl1 : UserControl
{
public UserControl1()
{
InitializeComponent();
}
public void OnUserControl1ParameterChange(string pData)
{
lbl_text.Content = pData;
}
}
}