0

Basically I'm new to WPF. I have a user Control - A. Inside A, I have another user Control B. When a button on B is pressed, a value is to be passed to A. I`m trying WPF MVVM. Kindly help 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 are the codes of UserControl1, UserControl2, and mainwindow. Now Kindly let me know the error in this

B.K.N
  • 11
  • 1
  • 6
  • possible duplicate of [Data Binding in WPF User Controls](http://stackoverflow.com/questions/11226843/data-binding-in-wpf-user-controls) – Fragment Mar 21 '15 at 06:38

3 Answers3

1

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 will work perfect !!!

sriman reddy
  • 763
  • 8
  • 22
0

You need to use data binding.

Last time (and only time) I did this, I updated a property in the code behind from the xaml when control A event fired; then, on the property changed event handler, update the property that's bound to B, and B will update automagically.

Just go look at data binding on MSDN.

Edit: just in case I wasn't clear, in my case I had some logic to implement. If your case is that you literally just want to replicate text on another control, then you could just bind the text fields directly to the same property, and they'd all update together.

No matter what the final solution you find looks like, I'd wager fair amounts of money it's going to involve databinding and one or more event handlers.

Ron Thompson
  • 1,086
  • 6
  • 12
  • In my case, I have to use User Control B in many other user controls. For Example: Like a header/Footer. So code behind may not be good? – B.K.N Mar 21 '15 at 04:15
  • How many controls are we talking about here? A dozen? If it's small enough, it's still the same thing. Just update all of the properties involved in the same event handler. If it's enough to make you shudder, there's probably a more elegant solution. But in that case, you should reword the question to make that part clear. – Ron Thompson Mar 21 '15 at 04:23
0

You can do these by two ways.

  1. Through Databinding as Ron Thompson told.

  2. Another way is using delegates and events

Pass values from UserControl1 to ParentControl through delegates and events and then get values from Parent Control to UserControl2

Sample Code:

public class UserControl1
{
   public Delegate del;  
   public int ValueToPass = 0;
   public UserControl1() 
   {
       InitializeComponent();   
   }
   public void method1() 
   {
       del.DynamicInvoke(ValueToPass);
   }
}

public class ParentControl
{
   public delegate void ValuePassDelegate(int ValueToGet);
   public event ValuePassDelegate ValuePassEvent;                 

   public UserControl1 UserControl1Obj = new UserControl1();
   public UserControl2 UserControl2Obj = new UserControl2();     

   public ParentControl() 
   {
       InitializeComponent();   
       ValuePassEvent += new ValuePassDelegate(method1);
       UserControl1Obj.del = ValuePassEvent;    
   }
   public void method1(int ValueToGet) 
   {
       UserControl2Obj.ValueToGet =  ValueToGet;
   }
}

public class UserControl2
{   
   public int ValueToGet;
   public UserControl2()
   {
       InitializeComponent();
   }       
}
sriman reddy
  • 763
  • 8
  • 22
  • I am getting an error at delegate & ValuePassDelegate. – B.K.N Mar 21 '15 at 05:37
  • Edit: misordered returntype & delegate keyword public delegate void ValuePassDelegate(int ValueToGet); – sriman reddy Mar 21 '15 at 05:57
  • @simran: k, good. Now in userControl1, i have a button. in userControl2, I have a textbox. When i click button, a text should be seen in textbox. For that, What can i do here? – B.K.N Mar 21 '15 at 06:05
  • @B.K.N I am not simran :-) I'm sriman .. In Usercontrol1, call method1() in button1 click event In method1() of ParentControl, Set the value of UserControl2 Textbox. `UserControl2Obj.TextBox1.Text = ValueToGet` – sriman reddy Mar 21 '15 at 06:17
  • cool Sriman..... I got an error "An unhandled exception of type 'System.NullReferenceException' occurred" On the line del.DynamicInvoke(ValueToPass); – B.K.N Mar 21 '15 at 06:26
  • @B.K.N Initialize ValueToPass variable to some value. `public int ValueToPass = 0;` – sriman reddy Mar 21 '15 at 06:34
  • I have already set value for ValueToPass. But still getting that error :( – B.K.N Mar 21 '15 at 06:39
  • @B.K.N Check whether you have pointed the event to UC1 Delegate Wrapper class obj in the constructor of ParentControl. `UserControl1Obj.del = ValuePassEvent;` – sriman reddy Mar 21 '15 at 06:44
  • @B.K.N Or else Paste your ParentWindow.xaml.cs , UserControl1.xaml.cs, UserControl2.xaml.cs code to let me check it. – sriman reddy Mar 21 '15 at 06:58
  • @B.K.N Code behind is perfect ! Paste the xaml code of ParentWindow.xaml , UserControl1.xaml, UserControl2.xaml. – sriman reddy Mar 21 '15 at 07:13