Change WPF window background image in C# code

后端 未结 7 1651
梦毁少年i
梦毁少年i 2020-12-09 08:11

I have a couple of Images configured as application resources.

When my application starts, the background of the main window is set via XAML:



        
相关标签:
7条回答
  • 2020-12-09 09:03

    I have been trying all the answers here with no success. Here is the simplest way to do it with ms-appx

            ImageBrush myBrush = new ImageBrush();
            Image image = new Image();
            image.Source = new BitmapImage(new Uri(@"ms-appx:///Assets/background.jpg"));
            myBrush.ImageSource = image.Source;
            TheGrid.Background = myBrush;
    

    Assets folder is in the first level of my project, so make sure to change the path as convenient.

    0 讨论(0)
提交回复
热议问题