1
**

I have 2 windows in app. mainwindow with 2 buttons and window1 with a label. btn one opens window1 and btn 2 changes window1 label color.

**

public partial class MainWindow : Window

{

Window1 sWindow = new Window1();public MainWindow()

{InitializeComponent();

    }

    private void btnFirstWindow_Click(object sender, RoutedEventArgs e)
    {
       sWindow.Show();
    }

    private void btnChangeBackground_Click_1(object sender, RoutedEventArgs e)
    {
       
        sWindow.ChangeBackground();
    }
}

public partial class Window1 : Window {

    public Window1()
    {
        InitializeComponent();
    }

    public void ChangeBackground()
    {

        
        if (lblShowUser.Background == Brushes.Green)
        {

            lblShowUser.Background = new LinearGradientBrush(Colors.Red, Colors.Red, 90);
        }
        else
        {
            lblShowUser.Background = Brushes.Green;
        }
    }

}

IF I remove this part **
private void btnFirstWindow_Click(object sender, RoutedEventArgs e) { sWindow.Show(); } ** From mainwindow and add both windows at app xaml Startup="App_Startup" StartupUri="MainWindow.xaml">

public partial class App : Application { void App_Startup(object sender, StartupEventArgs e) { Window1 sWindow = new Window1();

        sWindow.Show();
    }
}

the method of label changing color when i press the other button doesnt work am new to C# and wpf So when answering please also give explantion on what to be done to the code and why

expecting it to work even if changes are made regarding when the other page shows up but its not working

1 Answer 1

0

Create an object as a field from Window1 class, then you can access its objects.

Output (tested in Visual Studio 2017, .Net Framework 4.5.2): Output Note: if this solution helps you, please mark as answer (don't forget to vote for me).

XAML (Mainwindow):

<Window x:Class="WpfApp.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:WpfApp"
    mc:Ignorable="d"
    Title="MainWindow" Height="350" Width="525">
<Grid>
    <Button x:Name="OpenButton" Click="OpenButton_Click" Content="Open" HorizontalAlignment="Left" VerticalAlignment="Center" Width="75" Margin="99,0,0,0"/>
    <Button x:Name="ChangeColorButton" Click="ChangeColorButton_Click" Content="Changing color" HorizontalAlignment="Left" VerticalAlignment="Center" Width="99" Margin="331,0,0,0"/>
</Grid>
</Window>

XAML (Window1):

<Window x:Class="WpfApp.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:WpfApp"
    mc:Ignorable="d"
    Title="Window1" Closing="Window_Closing" Height="300" Width="300">
<Grid>
    <Label x:Name="Label" Content="Label" HorizontalAlignment="Center" VerticalAlignment="Center" Width="Auto"/>
</Grid>
</Window>

C# (MainWindow):

public partial class MainWindow : Window
{
    Window1 W1 = new Window1();
    public MainWindow()
    {
        InitializeComponent();
    }
    
    private void OpenButton_Click(object sender, RoutedEventArgs e)
    {
        W1.ShowDialog();
        
    }
    private void ChangeColorButton_Click(object sender, RoutedEventArgs e)
    {
        W1.Label.Background = new SolidColorBrush(Colors.Blue);
    }
}

C# (Window1):

public partial class Window1 : Window
{
    public Window1()
    {
        InitializeComponent();
    }

    private void Window_Closing(object sender, System.ComponentModel.CancelEventArgs e)
    {
        e.Cancel = true;
        this.Visibility = Visibility.Hidden;
    }
}

Thanks

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.