1

Iam trying to change the image of the MainWindow's Grid, which is a room with no lights, by pressing a button on another Window, and come up with a room with lights background. The problem is that when i edit the onClick event of the button i cant change the MainWindow's Grid Background.

XAML of MainWindow's Grid:

<Grid x:Name="PIC">
    <Grid.Background>
        <ImageBrush ImageSource="lightsofflaptop.jpg"/>
    </Grid.Background>

And the Window's Code:

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

    private void button_Click(object sender, RoutedEventArgs e)
    {
        ImageBrush b1 = new ImageBrush();
        b1.ImageSource = new BitmapImage(new Uri(@"---\lightsonNOlaptop.jpg"));
    }
}

i cant connect b1 with the Grid Background of the MainWindow.

Thanks in advance.

**Edit

I navigate through Windows by this:

*button located in MainWindow.cs

private void button_Click(object sender, RoutedEventArgs e)
{
    var newForm = new Window1();
    newForm.Show();
    this.Close();
}
mm8
  • 163,881
  • 10
  • 57
  • 88
  • Possible duplicate of [MVVM changing grid's background color on click](http://stackoverflow.com/questions/34331654/mvvm-changing-grids-background-color-on-click) – Ethilium Feb 24 '17 at 17:21
  • `---\lightsonNOlaptop.jpg` is not a valid path. Besides that, you don't do anything with the newly created ImageBrush. You should assign it to `PIC.Background`. – Clemens Feb 24 '17 at 17:38
  • If the image files are part of your Visual Studio project, set their Build Action to Resource and load them by a Pack URI, like `new BitmapImage(new Uri("pack://application:,,,/lightsonNOlaptop.jpg"))` – Clemens Feb 24 '17 at 17:42
  • @Clemens I cant assign it because it s in the Window3 and it doesn't let me to input PIC.background that is the problem. Iam trying to connect somehow MainWindow and Window3 so I can change it – Imperial Programmer Feb 24 '17 at 17:53
  • @Ethilium ill see any common proposes thanks for noticing – Imperial Programmer Feb 24 '17 at 17:54
  • How are you calling `Window3`? You could pass the PIC reference when calling it, then you use that reference to set the background. Anyway, if you are starting with WPF, I recommend reading about MVVM, that would make things like this more natural, but requires some learning. – LoRdPMN Feb 24 '17 at 18:18
  • @LoRdPMN I navigate with var newForm = new Window3(); newForm.show(); this.Close(); I will thanks for answering – Imperial Programmer Feb 24 '17 at 21:07
  • @ImperialProgrammer now you are closing your `MainWindow` by `this.Close()` . What is the meaning of this bkg change then ? – AnjumSKhan Feb 25 '17 at 14:54
  • @AnjumSKhan by bkg you mean? – Imperial Programmer Feb 25 '17 at 15:00
  • @ImperialProgrammer background – AnjumSKhan Feb 25 '17 at 15:06
  • @AnjumSKhan MainWindow's background is a room with no lights and by changing its background **-From the Button in Window3-** i put an image of the same room with lights, which the user sees. Should i navigate differently through windows? – Imperial Programmer Feb 25 '17 at 15:17
  • @ImperialProgrammer See my updated answer – AnjumSKhan Feb 25 '17 at 15:57

3 Answers3

0

If you are opening Window3 from the Window with the Grid that needs the image changed, you could pass a reference of the grid window in the constructor

Like this:

(In Window 3 Code)

private Window1 _parentWindow = null;

public Window3(Window1 parent)
{
   _parentWindow = parent;
   InitializeComponents();
} 

private void button_Click(object sender, RoutedEventArgs e)
{
    ImageBrush b1 = new ImageBrush();
    b1.ImageSource = new BitmapImage(new Uri(@"---\lightsonNOlaptop.jpg"));
    _parentWindow.PIC.Background = b1;
}
Kelly Barnard
  • 1,131
  • 6
  • 8
  • Thanks for your answer, i tried doing that but i need to navigate to these Windows with buttons. By your code i get the following error : Window2 `private void button1_Click(object sender, RoutedEventArgs e) { var newForm = new Window3(); //here newForm.Show(); this.Close(); }` With the following error : **There is no argument given that corresponds to the required formal parameter 'parent' of Windows3.Windows3(Window)** – Imperial Programmer Feb 24 '17 at 21:00
0

It appears that you are a novice to WPF.

I would make a ViewModel and use this ViewModel in both Windows, and set the background property in Window3 and it will get reflected in MainWindow by Binding.

ViewModel

    using System.ComponentModel;
    using System.Windows.Media;

    namespace WpfDatabase
    {
        public class ViewModel:INotifyPropertyChanged
        {
            private ImageBrush _background;
            public ImageBrush Background
            {
                get { return _background; }
                set { _background = value; OnPropertyChanged("Background"); }
            }

            public event PropertyChangedEventHandler PropertyChanged = delegate { };
            private void OnPropertyChanged(string prop)
            {
                PropertyChanged(this, new PropertyChangedEventArgs(prop));
            }
        }
    }

MainWindow.xaml

<Grid Background="{Binding Background}">

MainWindow.xaml.cs

public partial class MainWindow : Window
    {
        public ViewModel VM
        {
            get { return this.DataContext as ViewModel; }
            set { this.DataContext = value; }
        }        

        public MainWindow()
        {
            this.VM = new ViewModel();
            InitializeComponent();

            this.VM.Background = new ImageBrush(new BitmapImage(new Uri(@"C:\Users\Public\Pictures\Sample Pictures\Koala.jpg")));
        }        

        private void Button_Click_1(object sender, RoutedEventArgs e)
        {
            Window3 win = new Window3(VM);
            win.Owner = this;
            win.Show();
        }
    }

Window3

public partial class Window3 : Window
{
    public ViewModel VM
    {
        get { return this.DataContext as ViewModel; }
        set { this.DataContext = value; }
    }        

    public Window1(ViewModel vm)
    {
        InitializeComponent();
        VM = vm;
    }

    private void Button_Click_1(object sender, RoutedEventArgs e)
    {
        this.VM.Background = new ImageBrush(new BitmapImage(new Uri(@"C:\Users\Public\Pictures\Sample Pictures\Desert.jpg")));
    }
}

See this

AnjumSKhan
  • 9,647
  • 1
  • 26
  • 38
  • Thanks for your answer! Indeed iam a novice to WPF and i d like to learn more. I tried **binding grid's background** and implementing `INotifyPropertyChanged` in MainWindow but i cant get it to change from the Window3. – Imperial Programmer Feb 25 '17 at 14:27
  • @ImperialProgrammer How you are showing `Window3` ? And if `MainWindow` is closed, should `Window3` be closed too ? – AnjumSKhan Feb 25 '17 at 14:36
  • Iam navigating by pressing the button of MainWindow like this : `private void button1_Click(object sender, RoutedEventArgs e) { var newForm = new Window3(); newForm.Show(); this.Close(); }` – Imperial Programmer Feb 25 '17 at 14:43
  • The button that i want to change the MainWindow's Grid's Background is located in Window3 – Imperial Programmer Feb 25 '17 at 14:44
  • @ImperialProgrammer Post this piece of code in your question now. – AnjumSKhan Feb 25 '17 at 14:48
  • Again i want to thank you so much for helping me and putting effort into it. Ive tried it and replaced the grid background with the window background. Ive also replaced the links with my pictures and run it without errors. But the picture iam afraid, wont change. Ive tried deleting the picture from XAML code and only show it by the code of MainWindow.cs but it wont show up. – Imperial Programmer Feb 25 '17 at 16:45
  • code is straightforward and it should run without errors. also see for binding errors in output window. – AnjumSKhan Feb 25 '17 at 16:51
  • ive made a new project only with 2 windows just to test it again. When i start the Application there is no image in the First Window (MainWindow), neither when i press the button on the other. Any thoughts? – Imperial Programmer Feb 25 '17 at 17:03
  • Do i need to add something in Designer for VM to work? I just deleted **VM** in `this.VM.Background = new ImageBrush(new BitmapImage(new Uri(@"C:\Users\Public\Pictures\Sample Pictures\Desert.jpg")));` and the pictured showed up. – Imperial Programmer Feb 25 '17 at 17:13
  • @ImperialProgrammer I have added markup in my answer, use a `Button` in `Window3`, follow the code closely. – AnjumSKhan Feb 26 '17 at 03:31
0

You could get a reference to the MainWindow using the Application.Current.Windows property.

You also need to make the "PIC" Grid field internal or public to be able to access it from another window. The easiest way to do this is to set the x:FieldModifier attribute in the XAML markup.

Try this:

MainWindow.xaml:

<Grid x:Name="PIC" x:FieldModifier="public">
    <Grid.Background>
        <ImageBrush ImageSource="lightsofflaptop.jpg"/>
    </Grid.Background>

...

Window3.xaml.cs:

private void button_Click(object sender, RoutedEventArgs e)
{
    ImageBrush b1 = new ImageBrush();
    b1.ImageSource = new BitmapImage(new Uri(@"---\lightsonNOlaptop.jpg"));

    MainWindow mw = Application.Current.Windows.OfType<MainWindow>().FirstOrDefault();
    if(mw != null)
        mw.PIC.Backgound = b1;
}
mm8
  • 163,881
  • 10
  • 57
  • 88
  • It runs without errors but when i run the app and click the button it delivers an error `An unhandled exception of type 'System.NullReferenceException' occurred in WpfApplication1.exe Additional information: Object reference not set to an instance of an object.` – Imperial Programmer Feb 27 '17 at 16:17
  • Do you have a window called "MainWindow" that is currently opened and active when you click on the button? You should check if "mw" is null before you access it as per my edited answer. – mm8 Feb 27 '17 at 16:19