我试图通过按下另一个窗口上的按钮来改变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();
}
答案 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")));
}
}
答案 2 :(得分:0)
您可以使用MainWindow
属性获取对Application.Current.Windows
的引用。
您还需要使“PIC”Grid
字段internal
或public
能够从其他窗口访问它。最简单的方法是在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;
}