在WPF中使窗口大小与背景图像大小相同

发布于 2024-10-14 19:00:57 字数 950 浏览 2 评论 0原文

我是 WPF 新手,请耐心等待。我已经使用 ImageBrush 设置了窗口的背景图像,现在我希望窗口的大小与背景图像的大小完全相同。对此的明显解决方案是简单地使用背景图像的像素测量值手动设置宽度/高度属性,但这似乎太天真了。最好的方法是什么?到目前为止,这是我的 XAML:

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

我正在使用 Visual Studio 2010 和 .NET 4.0

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

会发光的星星闪亮亮i 2024-10-21 19:00:57

您可以将宽度和高度绑定到 ImageBrush 的 ImageSource 的 PixelWidth 和 PixelHeight,就​​像这样

更新

意识到我以前的解决方案不起作用,因为 ImageSource 没有 PixelWidth/PixelHeight,因为它不是 BitmapImage。我必须改用 BitmapImage 资源,但如果我没有在绑定之前声明资源,则绑定将不起作用(有问题吗?)

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

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>
偏爱你一生 2024-10-21 19:00:57

ImageBrush 不是满足此类要求的最佳解决方案,因为它没有宽度/高度属性,并且不存在于 VisualTree 中(它只是一个渲染工具)。

图层将帮助您:

<Window>
    <Grid >
        <Image Stretch="Fill" 
               Source="***some uri***"/>
        <StackPanel>
            <Button Height="40" 
                    Margin="20"
                    Content="Ololo"/>
        </StackPanel>
    </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>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文