1

Say I have these two User Controls. How would I pass data entered in the TextBox from ControlOneto the TextBox in ControlTwo when the Button in ControlOne is clicked?

<UserControl x:Class="Project.ControlOne"
             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>
        <StackPanel Orientation="Horizontal">
            <TextBox x:Name="MyTextBox" HorizontalAlignment="Center" VerticalAlignment="Center" Width="100" />
            <Button HorizontalAlignment="Center" VerticalAlignment="Center" Content="Send" Click="Button_Click" />
        </StackPanel>
    </Grid>
</UserControl>

...
namespace Project
{
    public partial class ControlOne : UserControl
    {

        public ControlOne()
        {
            InitializeComponent();
        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {

        }
    }
}

<UserControl x:Class="Project.ControlTwo"
             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>
        <StackPanel Orientation="Horizontal">
            <TextBox x:Name="MyTextBox" HorizontalAlignment="Center" VerticalAlignment="Center" Width="100" />
        </StackPanel>
    </Grid>
</UserControl>
KrimCard
  • 213
  • 2
  • 5
  • 12
  • [UI is not Data](http://stackoverflow.com/questions/14381402/wpf-programming-methodology/14382137#). Therefore you don't "pass data" between UI elements. You must learn MVVM. – Federico Berasategui Apr 03 '13 at 21:08
  • That is a pretty simple user control. Why not just skip the UserControl and use two Buttons and two TextBox? – paparazzo Apr 03 '13 at 21:10
  • @HighCore I looked at your link and I am having trouble with the Command class of the MVVM example. What namespace has that class? @Blam I should have said that `ControlOne` and `ControlTwo` are in different .xaml files. Unless I misunderstood. – KrimCard Apr 03 '13 at 21:28
  • @KrimCard [DelegateCommand](http://wpftutorial.net/DelegateCommand.html) – Federico Berasategui Apr 03 '13 at 21:31
  • @HighCore Sorry to ask you this, but everything seems to be going over my head. Would it be too much trouble to ask if you could illustrate how I would use MVVM in the situation I posed above? – KrimCard Apr 03 '13 at 21:56
  • @KrimCard post a screenshot of what you need and I can tell you the proper way to implement it in WPF / MVVM. – Federico Berasategui Apr 03 '13 at 21:59
  • @HighCore My main problem is my original question. However, I am trying to tackle it using what you first linked. Currently, I am trying to get that DelegateCommand to work. I typed up that example from WPFTutorial. Then I tried to use it in a view model. I must admit that I don't really understand the DelegateCommand and don't know how to use it. [ScreenShot](http://i.imgur.com/6ldgjEr.png) Edit: Changed the image. – KrimCard Apr 03 '13 at 22:11
  • You don't need `new Action()` and all that stuff. Use lambdas or directly reference the methods. – Federico Berasategui Apr 03 '13 at 22:17
  • @HighCore I see that I am a bit outside of my knowledge. I'll just go read up on MVVM and other things mentioned. Thanks for the help. – KrimCard Apr 03 '13 at 22:39
  • @KrimCard, how is controlOne related to control2? I mean where would you use each control? If I understand the structure, maybe I can help you. – Lance Apr 04 '13 at 11:23

1 Answers1

1

Your two user controls should have no knowledge of each other (unless one contains the other). That's why you can't "pass data" between them. The idea behind a user control is that you can drop it anywhere as many times as needed. If ControlOne knew about ControlTwo, what would happen if you used them separately? Or had three ControlTwos in the same place?

The layer which contains both of them should be the one to read a value from one and set it to the other. If you want it to happen on the button press, you should look into event handling so the parent can know when the control's button is pressed.

Community
  • 1
  • 1
Bobson
  • 13,498
  • 5
  • 55
  • 80