开发者

How to set a background of WPF Window?

开发者 https://www.devze.com 2023-01-19 16:40 出处:网络
I have a simple WPF window. I intend to set it\'s background to be one of the images I added to project as Embedded Resource. This is what I tried:

I have a simple WPF window. I intend to set it's background to be one of the images I added to project as Embedded Resource. This is what I tried:

<Window x:Class="A_Boggle.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/w开发者_StackOverflow中文版infx/2006/xaml"
Title="A-Boggle" Height="300" Width="625" ResizeMode="NoResize" WindowStartupLocation="CenterScreen" Name="Game">
<Window.Background>
    <ImageBrush ImageSource="background.jpg"></ImageBrush>
</Window.Background>
<Grid>
</Grid>

But with this, i always get this: "Error 1 The file splash.jpg is not part of the project or its 'Build Action' property is not set to 'Resource'."

Any ideas?


Go to the image within VS and set the item to be a Resource. Right click -> Properties -> Build Action -> Resource

Update:

You need to change the path if it is in a folder. ie...Resources/background.jpg


you can use this in main.xaml.cs

  InitializeComponent();
        ImageBrush myBrush = new ImageBrush();
        myBrush.ImageSource =
            new BitmapImage(new Uri("F://13.png", UriKind.Absolute));
        this.Background = myBrush;


the issue in my case was becouse i set the Heigth and width properties on the window and use maximized windows state , when i remove the width and heigth properties the error dissapear.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号