Question How to make this work if the button and label are in different window

SAE

New member
Joined
Mar 19, 2022
Messages
4
Programming Experience
Beginner
The Idea is that when I start the application 2 windows pop up. On mainwindow I have button and on window1 I have label and when I press the button the label color should change.

This is what Far have tried so far but it didn't work. What am I doing wrong ? and how to make it work ?
Note: in the wpf app am not using Model View ViewModel.

public partial class MainWindow : Window

{
public MainWindow()
{
InitializeComponent();


}

private void btnFirstWindow_Click(object sender, RoutedEventArgs e)
{
Window1 sWindow = new Window1(btnFirstWindow);
sWindow.Show();

}
}

Mainwindow XAML

Title="MainWindow" Height="450" Width="800">

<Grid>

<Button x:Name="btnFirstWindow" Content="Button" HorizontalAlignment="Left" Height="140" Margin="492,77,0,0" VerticalAlignment="Top" Width="151" Click="btnFirstWindow_Click"/>

</Grid>

**

public partial class Window1 : Window

{
private Button btnfirstWindow;

public Window1(Button btnfirstWindow)

{
this.btnfirstWindow = btnfirstWindow;

InitializeComponent();
}

private void Window_Loaded(object sender, RoutedEventArgs e)
{
btnfirstWindow.Click += btnfirstWindow_Click;
}

void btnfirstWindow_Click(object sender, RoutedEventArgs e)
{
lblShowUser.Background = Brushes.Red;
}

}

** Window1 XAML**

Title="Window1" Height="450" Width="800">

<Grid>
<Label Name="lblShowUser" Content="Label" HorizontalAlignment="Left" Height="119" Margin="321,98,0,0" VerticalAlignment="Top" Width="281"/>

</Grid>
 
Last edited:
Firstly, please provide meaningful thread titles that summarise the issue. Everyone who posts needs help so your title is as useful as no title at all.

As for the issue, are you using the MVVM design pattern? If you're not then you're not really using WPF properly. You can do things the old WinForms way but it's not how WPF should be used. The solution to your problem would be different in each case.
 
I think am not using MVVM design pattern. I just opend a wpf application in visual studio. made 2 windows. Put a label on one of them (win1) and a button on the other(win2).

now i dont know how to make it work that by pressing the button in win2 win1 label color changes. i dont know how to do it. Thats why am asking here on how it can be done
 
Firstly, please provide meaningful thread titles that summarise the issue. Everyone who posts needs help so your title is as useful as no title at all.

As for the issue, are you using the MVVM design pattern? If you're not then you're not really using WPF properly. You can do things the old WinForms way but it's not how WPF should be used. The solution to your problem would be different in each case.
Like here is the button and label and how it works if its on 1 window
(( MainWindow.xaml.cs ))

namespace WpfApp3
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}

private void btn_Click(object sender, RoutedEventArgs e)
{
if (Label.Background == Brushes.Black)
{
Label.Background = new LinearGradientBrush(Colors.Red, Colors.Red, 90);

}
else
{
Label.Background = Brushes.Red;

}
}
}
}

((((( MainWindow.xaml ))))))))))

Title="MainWindow" Height="450" Width="800">
<Grid>
<Label Background="NavajoWhite" Name="Label" Content="Label" FontSize="140" HorizontalAlignment="Left" Height="210" Margin="275,104,0,0" VerticalAlignment="Top" Width="497"/>
<Button Name="btn" Content="Button" HorizontalAlignment="Left" Height="56" Margin="40,43,0,0" VerticalAlignment="Top" Width="377" Click="btn_Click"/>

</Grid>


this works but both button and label are at same window. Now how can i make it work if the button and the label are in diffrent windows
 
If you are not doing things the MVVM way, then you would do it the old WinForms way: Let the second window fire an event each time the button is pressed. Let the first window listen to that event and do whatever it needs to do -- in this case change label colors.

If you are going to use WPF, I highly recommend that you use MVVM. With MVVM, you would have a shared model, or shared view model between the two windows. When a button is pressed on the second window, the view model knows about it and updates the model. The first window has a it's label color bound to a property on the view model. Since something changed in the view model, the binding will automatically pickup the change (if you implement the view model properly).
 
Another thing to consider is whether the second window is modal or not. If it's modal, then there is really no need to update the UI if the first window until the second window closes.
 

Latest posts

Back
Top Bottom