0

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.

enter image description here

enter image description here

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;
        }
    }
}
HelloWorld1
  • 13,688
  • 28
  • 82
  • 145
  • Answer to similar question and link to Git example project here http://stackoverflow.com/questions/24221068/wpf-navigation-and-rotating-backgrounds/24334887#24334887 – Mikko Viitala Jul 30 '14 at 19:53

3 Answers3

0

As your question is not exactly like the How to call functions in a main view model from other view models? question, I did not close it as a duplicate. However, I believe that the answer to your question lies in my answer to the linked question and the one that it links to and that is to use delegates.

The difference to those questions is that they were using view models, while it appears that you'll be using code behind. Either way, it makes little difference, because you can use delegates in the same way in code behind as long as you have a common parent. MainWindow.xaml.cs could be your common parent.


UPDATE >>>

The link that you have provided does not fit to my purpose. In my case it is between two user controls that are located inside of mainwindows.xaml

That's exactly what I've mentioned. Define your delegate in the usercontrol_menu.xaml.cs file and the handler in the usercontrol_number1.xaml.cs file and connect them in MainWindow.xaml.cs. Extending my example from the other linked question (Passing parameters between viewmodels), you could do something like this:

In MainWindow.xaml.cs:

usercontrol_menu.ParameterChange += usercontrol_number1.OnUserControl1ParameterChange;

Read the linked answers again and the linked pages on MSDN and you'll get it.

Community
  • 1
  • 1
Sheridan
  • 68,826
  • 24
  • 143
  • 183
  • I have made some improvement int he explaination. Is it possible that you can provide a concrete example based on the code I have provided. – HelloWorld1 Jun 27 '14 at 15:46
  • I am confused as to why you're asking me to do your job for you. It was surprising enough when you left your first comment, after I specifically mentioned the fact that the linked answer would also fit your problem in my original answer. Also, thanks for deleting your previous comment, so that my UPDATE no longer makes sense. I have provided links to all the resources that you need to address your problem, even if you've never used `delegate`s before. All you need to do is follow the provided examples and let's face it... as a developer, if you can't do that, you may as well give up your job. – Sheridan Jun 27 '14 at 16:06
  • When you mean Handler do you mean "EventHandler"? – HelloWorld1 Jun 27 '14 at 23:43
  • In this case, I'm talking about the method that 'handles', or is attached to, the property of the type of `delegate` that you declare... the `usercontrol_number1.OnUserControl1ParameterChange` method in the above example. – Sheridan Jun 27 '14 at 23:51
  • I have faced some problem. 1.I don't know how to make a binding with xaml code in relation to the "btn_send" after you have clicked on the button? 2. How should I enable to make a invocation in the usercontrol_menu when the sourcecode is established in the mainwindows? I have added some new source code with c# code to understand better. – HelloWorld1 Jun 28 '14 at 09:34
  • 3. What part am I missing in order to achieve the goal? – HelloWorld1 Jun 28 '14 at 09:52
  • One `UserControl` declares a `delegate` and a related method of that `delegate` type. The other `UserControl` declare a 'handler' method that matches the signature of that `delegate`. They get hooked up in `MainWindow.xaml.cs` as shown in my bottom code example. When the `Button` is clicked, that `UserControl` calls the `delegate` method passing in whatever parameters you set up in your `delegate`. If the handler method from the other `UserControl` has been attached correctly, it will get called with the relevant data. I can't say it any clearer than that. You don't need MVVM Light to do this. – Sheridan Jun 29 '14 at 15:51
  • There is a error that I tried solving but I failed. Please take a look at the new content to understand the situation. – HelloWorld1 Jun 29 '14 at 19:55
  • Why did you add and accept an answer if your problem is not solved? If this is a separate problem, then ask a new question. – Sheridan Jun 29 '14 at 20:45
0

Use EventAggregator of Prism. A good explanation is given here. Just download the sample and run. It is exactly what you are expecting.

Mayur Dhingra
  • 1,527
  • 10
  • 27
0

1.Download and install the package for all usercontrol. https://www.nuget.org/packages/MvvmLight/

2.Define a message class that be accessable for all usercontrol. It doesn't matter where it will be located inside of solution

public class MyMessage
{
   public string Text { get; set; }
}

3.In usercontrol_menu, create an instance of this class and pass it to the Send method of the messenger in the user control.:

using GalaSoft.MvvmLight.Messaging;
...
private void btn_send_Click(object sender, RoutedEventArgs e)
{
    var msg = new MyMessage2() { Text = txt_input.Text };
    Messenger.Default.Send<MyMessage2>(msg);
}

4.Register for the given message type in the constructor of usercontrol_number1:

Messenger.Default.Register<MyMessage>
( 
     this, 
     ( action ) => { lbl_text.Content = action.Text; }
);

In order to use Messenger you need a reference to it in each project and a Using in each class.

The way messenger works is the publisher/sender creates a message of a particular Type ( as in of that class ). The receiver subscribes to messages of that Type. So the Type ( of the object ) defines what the message is. And of course can also contain data.

HelloWorld1
  • 13,688
  • 28
  • 82
  • 145