如何设置WPF窗口的背景?

时间:2010-10-07 20:23:04

标签: wpf background

我有一个简单的WPF窗口。我打算将它的背景设置为我作为嵌入式资源项目添加的图像之一。这就是我试过的:

<Window x:Class="A_Boggle.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/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>

但有了这个,我总是得到这个:“错误1文件splash.jpg不是项目的一部分,或者它的'Build Action'属性没有设置为'Resource'。”

有什么想法吗?

3 个答案:

答案 0 :(得分:19)

转到VS中的图像并将项目设置为资源。右键单击 - &gt;属性 - &gt;构建行动 - &gt;资源

<强>更新

如果路径位于文件夹中,则需要更改路径。即...资源/ background.jpg

答案 1 :(得分:3)

您可以在main.xaml.cs

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

答案 2 :(得分:1)

在我的情况下的问题是因为我在窗口上设置了高度和宽度属性并使用了最大化的窗口状态,当我删除宽度和高度属性时,错误消失了。

相关问题