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.
- namespace WpfApp1
- {
- ///
- /// Interaction logic for MainWindow.xaml
- ///
- public partial class MainWindow : Window
- {
- public MainWindow()
- {
- InitializeComponent();
- }
- private void Btn1_Click(object sender, RoutedEventArgs e)
- {
- MessageBox.Show("Hello World");
- }
- public void Btn2_Click(object sender, RoutedEventArgs e)
- {
- Window1 instanceOfWindow1 = new Window1();
- instanceOfWindow1.Visibility = Visibility.Visible;
- }
- }
- }
- namespace WpfApp1
- {
- ///
- /// Interaction logic for Window1.xaml
- ///
- public partial class Window1 : Window
- {
- public Window1()
- {
- InitializeComponent();
- }
- public void Btn1_Click(object sender, RoutedEventArgs e)
- {
- // code here to call Btn_Click in MainWindow
- }
- }
- }
- <Window x:Class="WpfApp1.MainWindow"
- xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
- xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
- xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
- xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
- xmlns:local="clr-namespace:WpfApp1"
- mc:Ignorable="d"
- Title="MainWindow" Height="450" Width="800">
- <Grid>
- <Button x:Name="btn1" Content="Show Hello World" HorizontalAlignment="Left" Margin="84,49,0,0" VerticalAlignment="Top" Width="125" Click="Btn1_Click"/>
- <Button x:Name="btn2" Content="Switch to Window1" HorizontalAlignment="Left" Margin="354,49,0,0" VerticalAlignment="Top" Width="130" Click="Btn2_Click"/>
- Grid>
- Window>
- <Window x:Class="WpfApp1.Window1"
- xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
- xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
- xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
- xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
- xmlns:local="clr-namespace:WpfApp1"
- mc:Ignorable="d"
- Title="Window1" Height="450" Width="800">
- <Grid>
- <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"/>
- Grid>
- Window>
Piyush PansuriyaPosted Feb 7, 2019, 6:01 AM
Tony AllanPosted Feb 7, 2019, 4:43 AM
Piyush PansuriyaPosted Feb 7, 2019, 4:34 AM