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

Changing Grid Background Image of MainWindow from another Window's button - WPF

我正在尝试通过按另一个 Window 上的按钮来更改 MainWindow 的 Grid 的图像,这是一个没有灯光的房间,并想出一个有灯光背景。问题是,当我编辑按钮的 onClick 事件时,我无法更改 MainWindow 的网格背景。

XAML 主Window 的网格:

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

和 Window 的代码:

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

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

像这样:

(在Window 3码)

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

看来您是WPF新手。

我会制作一个 ViewModel 并在两个 Windows 中使用此 ViewModel,并在 Window3 中设置背景 属性,它将反映在 MainWindow 来自 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

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

您还需要将 "PIC" Grid 字段设置为 internalpublic 以便能够从另一个 window 访问它。最简单的方法是在 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;
}