2016-11-17 13 views
0

こんにちはすべて私はと仮定して、と私のjpgイメージを取り、フォームの背景に配置する次のコードを持っています。しかし、私が見るのは黒い背景だけです。フォームのWPFロード背景画像

Dim myBrush As New ImageBrush() 
Dim image As New Image() 
Dim grid As New Grid() 

image.Source = New BitmapImage(New Uri("pack://application:,,,/WpfApplication1;component/Resources/1680-logoless.jpg")) 
myBrush.ImageSource = image.Source 
grid.Background = myBrush 

enter image description here

そして、これは私の現在のXAMLです:

<Window x:Class="MainWindow" 
     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
     xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
     xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
     xmlns:local="clr-namespace:WpfApplication1" 
     mc:Ignorable="d" 
     Title="MainWindow" Height="350" Width="525"> 
    <Grid> 
     <TextBox x:Name="textBox1" HorizontalAlignment="Left" Height="23" TextWrapping="Wrap" Text="TextBox" VerticalAlignment="Top" Width="120" Margin="157,145,0,0"/> 
     <Label x:Name="label1" Content="Label" HorizontalAlignment="Left" Margin="68,59,0,0" VerticalAlignment="Top" Width="111"/> 
     <Button x:Name="button" Content="Button" HorizontalAlignment="Left" VerticalAlignment="Top" Width="75" Margin="335,62,0,0"/> 
     <Button x:Name="button1" Content="Encrypt" HorizontalAlignment="Left" Height="17" Margin="335,123,0,0" VerticalAlignment="Top" Width="120"/> 
     <TextBox x:Name="toEncrypt" HorizontalAlignment="Left" Height="23" TextWrapping="Wrap" Text="TextBox" VerticalAlignment="Top" Width="172" Margin="335,145,0,0"/> 
     <TextBox x:Name="toDecrypt" HorizontalAlignment="Left" Height="23" TextWrapping="Wrap" Text="TextBox" VerticalAlignment="Top" Width="172" Margin="335,173,0,0"/> 
     <Button x:Name="button2" Content="Decrypt" HorizontalAlignment="Left" Height="23" Margin="335,201,0,0" VerticalAlignment="Top" Width="120"/> 

    </Grid> 
</Window> 

誰もが私が失われる可能性が何かを参照してください?あなたのメイン・ウィンドウクラスにフィールドを作成しますXAMLでのグリッドのx:Name属性、設定し

+0

イメージファイルのビルドアクションを 'リソース'に設定しましたか? – Clemens

+0

@クレメンスはい、そうでした。 – StealthRT

+0

Imageコントロールとは何ですか?代わりに 'myBrush.ImageSource = New BitmapImage(...)'と書いてください。 – Clemens

答えて

1

:次に

<Window ...> 
    <Grid x:Name="rootGrid"> 
     ... 
    </Grid> 
</Window> 

は、代わりに新しいグリッドのインスタンスを作成するので、XAMLで宣言された1の背景を設定します:

rootGrid.Background = New ImageBrush(New BitmapImage(
    New Uri("pack://application:,,,/Resources/1680-logoless.jpg"))) 
+0

それがあります!助けてくれてありがとう、クレメンス – StealthRT