TECHNOLOGIES
FORUMS
JOBS
BOOKS
EVENTS
INTERVIEWS
Live
MORE
LEARN
Training
CAREER
MEMBERS
VIDEOS
NEWS
BLOGS
Sign Up
Login
No unread comment.
View All Comments
No unread message.
View All Messages
No unread notification.
View All Notifications
Answers
Post
An Article
A Blog
A News
A Video
An EBook
An Interview Question
Ask Question
Forums
Monthly Leaders
Forum guidelines
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.
namespace
WpfApp1
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
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
{
/// <summary>
/// Interaction logic for Window1.xaml
/// </summary>
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
>
Reply
Answers (
3
)
Want to open remote desktop
How to show read more and hide in jquery data table