C# 从另一个窗口更改主窗口的网格背景图像';s按钮-WPF
我试图通过按下另一个窗口上的按钮来改变主窗口网格的图像,这是一个没有灯光的房间,并得到一个有灯光背景的房间。问题是,当我编辑按钮的onClick事件时,我无法更改主窗口的网格背景 主窗口网格的XAML:C# 从另一个窗口更改主窗口的网格背景图像';s按钮-WPF,c#,wpf,xaml,grid,background-image,C#,Wpf,Xaml,Grid,Background Image,我试图通过按下另一个窗口上的按钮来改变主窗口网格的图像,这是一个没有灯光的房间,并得到一个有灯光背景的房间。问题是,当我编辑按钮的onClick事件时,我无法更改主窗口的网格背景 主窗口网格的XAML: <Grid x:Name="PIC"> <Grid.Background> <ImageBrush ImageSource="lightsofflaptop.jpg"/> </Grid.Background> 我无
<Grid x:Name="PIC">
<Grid.Background>
<ImageBrush ImageSource="lightsofflaptop.jpg"/>
</Grid.Background>
我无法将b1与主窗口的网格背景连接
提前谢谢
**编辑
我通过以下方式在Windows中导航:
*按钮位于MainWindow.cs中
private void button_Click(object sender, RoutedEventArgs e)
{
var newForm = new Window1();
newForm.Show();
this.Close();
}
如果从需要更改图像的网格窗口打开Window3,可以在构造函数中传递网格窗口的引用 像这样: (在窗口3代码中)
如果从需要更改图像的网格窗口打开Window3,可以在构造函数中传递网格窗口的引用 像这样: (在窗口3代码中)
看来您是WPF的新手 我将制作一个
ViewModel
,并在两个Windows
中使用该ViewModel
,并在Window3中设置background属性,它将通过Binding
反映在main窗口中
视图模型
main window.xaml
Window3
看来您是WPF的新手
我将制作一个ViewModel
,并在两个Windows
中使用该ViewModel
,并在Window3中设置background属性,它将通过Binding
反映在main窗口中
视图模型
main window.xaml
Window3
您可以使用应用程序.Current.Windows
属性获取对主窗口的引用
您还需要使“PIC”网格
字段内部
或公共
能够从另一个窗口访问它。最简单的方法是在XAML标记中设置x:FieldModifier
属性
试试这个:
MainWindow.xaml:
<Grid x:Name="PIC" x:FieldModifier="public">
<Grid.Background>
<ImageBrush ImageSource="lightsofflaptop.jpg"/>
</Grid.Background>
...
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;
}
...
Window3.xaml.cs:
<Grid x:Name="PIC" x:FieldModifier="public">
<Grid.Background>
<ImageBrush ImageSource="lightsofflaptop.jpg"/>
</Grid.Background>
...
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;
}
private void按钮\u单击(对象发送者,路由目标)
{
ImageBrush b1=新的ImageBrush();
b1.ImageSource=新的位图图像(新的Uri(@“--\lightsonNOlaptop.jpg”);
MainWindow mw=Application.Current.Windows.OfType().FirstOrDefault();
如果(mw!=null)
mw.PIC.Backgound=b1;
}
您可以使用应用程序.Current.Windows
属性获取对主窗口的引用
您还需要使“PIC”网格
字段内部
或公共
能够从另一个窗口访问它。最简单的方法是在XAML标记中设置x:FieldModifier
属性
试试这个:
MainWindow.xaml:
<Grid x:Name="PIC" x:FieldModifier="public">
<Grid.Background>
<ImageBrush ImageSource="lightsofflaptop.jpg"/>
</Grid.Background>
...
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;
}
...
Window3.xaml.cs:
<Grid x:Name="PIC" x:FieldModifier="public">
<Grid.Background>
<ImageBrush ImageSource="lightsofflaptop.jpg"/>
</Grid.Background>
...
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;
}
private void按钮\u单击(对象发送者,路由目标)
{
ImageBrush b1=新的ImageBrush();
b1.ImageSource=新的位图图像(新的Uri(@“--\lightsonNOlaptop.jpg”);
MainWindow mw=Application.Current.Windows.OfType().FirstOrDefault();
如果(mw!=null)
mw.PIC.Backgound=b1;
}
可能重复的--\lightsonNOlaptop.jpg
路径无效。除此之外,您不需要对新创建的ImageBrush执行任何操作。您应该将其分配给PIC.Background
。如果图像文件是Visual Studio项目的一部分,请将其生成操作设置为Resource,并通过包URI加载它们,如new BitmapImage(新URI(“pack://application:,,,/lightsonNOlaptop.jpg”))
@Clemens我无法分配它,因为它在Window3中,不允许我输入PIC.background,这就是问题所在。我正在尝试以某种方式连接MainWindow和Window3,以便进行更改it@Ethilium请注意,-\lightsonNOlaptop.jpg
的路径无效,我将看到任何常见的建议。除此之外,您不需要对新创建的ImageBrush执行任何操作。您应该将其分配给PIC.Background
。如果图像文件是Visual Studio项目的一部分,请将其生成操作设置为Resource,并通过包URI加载它们,如new BitmapImage(新URI(“pack://application:,,,/lightsonNOlaptop.jpg”))
@Clemens我无法分配它,因为它在Window3中,不允许我输入PIC.background,这就是问题所在。我正在尝试以某种方式连接MainWindow和Window3,以便进行更改it@Ethilium我会看到任何常见的建议谢谢你的注意谢谢你的回答,我试过这么做,但我需要用按钮导航到这些窗口。通过您的代码,我得到以下错误:Window2private void button1\u Click(对象发送器,RoutedEventArgs e){var newForm=newwindow3();//here newForm.Show();this.Close();}
出现以下错误:没有给出与Windows3所需的形式参数“parent”对应的参数。Windows3(窗口)感谢您的回答,我尝试过这样做,但我需要使用按钮导航到这些窗口。通过您的代码,我得到以下错误:Window2private void button1\u Click(对象发送器,RoutedEventArgs e){var newForm=newwindow3();//here newForm.Show();this.Close();}
出现以下错误:没有给出与Windows3所需的形式参数“parent”对应的参数。Windows3(窗口)感谢您的回答!事实上,我是WPF的新手,我想了解更多。我尝试绑定网格的背景并在主窗口中实现INotifyPropertyChanged
,但无法从