Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/298.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/wpf/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 从另一个窗口更改主窗口的网格背景图像';s按钮-WPF_C#_Wpf_Xaml_Grid_Background Image - Fatal编程技术网

C# 从另一个窗口更改主窗口的网格背景图像';s按钮-WPF

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> 我无

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

主窗口网格的XAML:

<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我会看到任何常见的建议谢谢你的注意谢谢你的回答,我试过这么做,但我需要用按钮导航到这些窗口。通过您的代码,我得到以下错误:Window2
private void button1\u Click(对象发送器,RoutedEventArgs e){var newForm=newwindow3();//here newForm.Show();this.Close();}
出现以下错误:没有给出与Windows3所需的形式参数“parent”对应的参数。Windows3(窗口)感谢您的回答,我尝试过这样做,但我需要使用按钮导航到这些窗口。通过您的代码,我得到以下错误:Window2
private void button1\u Click(对象发送器,RoutedEventArgs e){var newForm=newwindow3();//here newForm.Show();this.Close();}
出现以下错误:没有给出与Windows3所需的形式参数“parent”对应的参数。Windows3(窗口)感谢您的回答!事实上,我是WPF的新手,我想了解更多。我尝试绑定网格的背景并在主窗口中实现
INotifyPropertyChanged
,但无法从