void.pointer
void.pointer

Reputation: 26415

Making window size the same as background image size in WPF

I am new to WPF so bear with me. I have set the background image of my window using an ImageBrush and now I want my window's size to be exactly the size of the background image. The obvious solution to this is to simply set the width/height property manually with the pixel measurements of my background image, but this seems too naive. What is the best way to do this? Here is my XAML so far:

<Window x:Class="FruitFactory.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="Fruit Factory" Height="576" WindowStyle="None" DataContext="{Binding}" ResizeMode="NoResize" Width="834">
    <Window.Background>
        <ImageBrush ImageSource="/FruitFactory;component/Graphics/FruitFactoryBackground.png"></ImageBrush>
    </Window.Background>
    <Window.Resources>
    </Window.Resources>
    <Grid>
        <ComboBox Height="23" HorizontalAlignment="Left" Margin="52,27,0,0" Name="comboBox1" VerticalAlignment="Top" Width="120" />
    </Grid>
</Window>

I'm using Visual Studio 2010 and .NET 4.0

Upvotes: 0

Views: 5367

Answers (2)

Fredrik Hedblad
Fredrik Hedblad

Reputation: 84674

You can bind Width and Height to PixelWidth and PixelHeight of the ImageSource for ImageBrush like this

Update

Realized my previous solution didn't work because ImageSource didn't have PixelWidth/PixelHeight since it wasn't a BitmapImage. I had to use a BitmapImage resource instead but then the bindings didn't work if I didn't declare the Resource before the bindings (bug anyone?)

<Window x:Class="WindowSameSizeAsBackground.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="Fruit Factory"
        WindowStyle="None"
        DataContext="{Binding}"
        ResizeMode="NoResize">
    <Window.Resources>
        <BitmapImage x:Key="backgroundBrush"
                     UriSource="/FruitFactory;component/Graphics/FruitFactoryBackground.png"/>
    </Window.Resources>
    <Window.Width>
        <Binding Source="{StaticResource backgroundBrush}" Path="PixelWidth"/>
    </Window.Width>
    <Window.Height>
        <Binding Source="{StaticResource backgroundBrush}" Path="PixelHeight"/>
    </Window.Height>
    <Window.Background>
        <ImageBrush x:Name="imageBrush"
                    ImageSource="{StaticResource backgroundBrush}"></ImageBrush>
    </Window.Background>
    <Grid>
        <ComboBox Height="23" HorizontalAlignment="Left" Margin="52,27,0,0" Name="comboBox1" VerticalAlignment="Top" Width="120" />
        <Button Content="Button" Height="23" HorizontalAlignment="Left" Margin="258,179,0,0" Name="button1" VerticalAlignment="Top" Width="75" Click="button1_Click" />
    </Grid>
</Window>

Upvotes: 6

Alex Zhevzhik
Alex Zhevzhik

Reputation: 3407

ImageBrush isn't best solution for such requirements, because it hasn't Width/Height properties and does not present in VisualTree (it's just a tool for rendering).

Layers will help you:

<Window>
    <Grid >
        <Image Stretch="Fill" 
               Source="***some uri***"/>
        <StackPanel>
            <Button Height="40" 
                    Margin="20"
                    Content="Ololo"/>
        </StackPanel>
    </Grid>
<Window>

Upvotes: 1

Related Questions