开发者

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/FruitFact开发者_JAVA百科oryBackground.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


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>


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>
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜