Tony Allan

Tony Allan

  • NA
  • 71
  • 10.1k

Call button from another class.

Feb 6 2019 11:46 PM
Hello,
 
I have 2 classes MainWindow and Window1. MainWindow has 2 buttons , btn1 which calls a message box and btn2 that opens an instance of Window1 (instanceOfWindow1).
Window1 has a button btn1 which I would like to call the btn1 from MainWindow and display the message.
 
Question- how do I activate btn1 in MainWindow from btn1 in Window1.
Thank you.
  1. namespace WpfApp1  
  2. {  
  3. /// <summary>  
  4. /// Interaction logic for MainWindow.xaml  
  5. /// </summary>  
  6. public partial class MainWindow : Window  
  7. {  
  8. public MainWindow()  
  9. {  
  10. InitializeComponent();  
  11. }  
  12.   
  13. private void Btn1_Click(object sender, RoutedEventArgs e)  
  14. {  
  15. MessageBox.Show("Hello World");  
  16. }  
  17.   
  18. public void Btn2_Click(object sender, RoutedEventArgs e)  
  19. {  
  20. Window1 instanceOfWindow1 = new Window1();  
  21. instanceOfWindow1.Visibility = Visibility.Visible;  
  22. }  
  23. }  
  24. }  
  25. namespace WpfApp1  
  26. {  
  27. /// <summary>  
  28. /// Interaction logic for Window1.xaml  
  29. /// </summary>  
  30. public partial class Window1 : Window  
  31. {  
  32. public Window1()  
  33. {  
  34. InitializeComponent();  
  35. }  
  36.   
  37. public void Btn1_Click(object sender, RoutedEventArgs e)  
  38. {  
  39. // code here to call Btn_Click in MainWindow  
  40. }  
  41. }  
  42. }  
  1. <Window x:Class="WpfApp1.MainWindow"  
  2. xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"  
  3. xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"  
  4. xmlns:d="http://schemas.microsoft.com/expression/blend/2008"  
  5. xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"  
  6. xmlns:local="clr-namespace:WpfApp1"  
  7. mc:Ignorable="d"  
  8. Title="MainWindow" Height="450" Width="800">  
  9. <Grid>  
  10. <Button x:Name="btn1" Content="Show Hello World" HorizontalAlignment="Left" Margin="84,49,0,0" VerticalAlignment="Top" Width="125" Click="Btn1_Click"/>  
  11. <Button x:Name="btn2" Content="Switch to Window1" HorizontalAlignment="Left" Margin="354,49,0,0" VerticalAlignment="Top" Width="130" Click="Btn2_Click"/>  
  12.   
  13. </Grid>  
  14. </Window>  
  15. <Window x:Class="WpfApp1.Window1"  
  16. xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"  
  17. xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"  
  18. xmlns:d="http://schemas.microsoft.com/expression/blend/2008"  
  19. xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"  
  20. xmlns:local="clr-namespace:WpfApp1"  
  21. mc:Ignorable="d"  
  22. Title="Window1" Height="450" Width="800">  
  23. <Grid>  
  24. <Button x:Name="btn1" Content="Button to call button in MainWindow that shows message." HorizontalAlignment="Left" Margin="79,65,0,0" VerticalAlignment="Top" Width="341" Click="Btn1_Click"/>  
  25.   
  26. </Grid>  
  27. </Window>  

Answers (3)