Reputation: 10552
Hey all I have the following code that supposed to take my jpg image and place it on the background of the form. However, all I see is a black background.
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
And this is my current 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>
Anyone see anything I could be missing?
Upvotes: 0
Views: 453
Reputation: 128157
Set the x:Name
attribute of the Grid in XAML, which will create a field in your MainWindow class:
<Window ...>
<Grid x:Name="rootGrid">
...
</Grid>
</Window>
Then, instead of creating a new Grid instance, set the Background of the one declared in XAML:
rootGrid.Background = New ImageBrush(New BitmapImage(
New Uri("pack://application:,,,/Resources/1680-logoless.jpg")))
Upvotes: 1