Reputation: 11
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
Upvotes: 0
Views: 2085
Reputation: 773
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 !!!
Upvotes: 2
Reputation: 773
You can do these by two ways.
Through Databinding as Ron Thompson told.
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();
}
}
Upvotes: 0
Reputation: 1096
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.
Upvotes: 0