从另一个窗口的按钮 - WPF更改MainWindow的网格背景图像

时间:2017-02-24 17:15:06

标签: c# wpf xaml grid background-image

我试图通过按下另一个窗口上的按钮来改变MainWindow网格的图像,这是一个没有灯光的房间,然后想出一个带有灯光背景的房间。问题是,当我编辑按钮的onClick事件时,我无法更改MainWindow的网格背景。

MainWindow网格的XAML:

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

窗口代码:

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"));
    }
}

我无法将b1与MainWindow的网格背景连接起来。

提前致谢。

**修改

我通过Windows浏览:

位于MainWindow.cs的

*按钮

private void button_Click(object sender, RoutedEventArgs e)
{
    var newForm = new Window1();
    newForm.Show();
    this.Close();
}

3 个答案:

答案 0 :(得分:0)

如果您使用需要更改图像的网格从窗口打开Window3,您可以在构造函数中传递网格窗口的引用

像这样:

(在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;
}

答案 1 :(得分:0)

看来你是WPF的新手。

我会在ViewModel中创建ViewModel并使用此Windows,并在Window3中设置背景属性,它将MainWindow反映在Binding中}}

<强> 视图模型

    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

答案 2 :(得分:0)

您可以使用MainWindow属性获取对Application.Current.Windows的引用。

您还需要使“PIC”Grid字段internalpublic能够从其他窗口访问它。最简单的方法是在XAML标记中设置x:FieldModifier属性。

试试这个:

<强> 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;
}