在WPF中使窗口大小与背景图像大小相同
我是 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以将宽度和高度绑定到 ImageBrush 的 ImageSource 的 PixelWidth 和 PixelHeight,就像这样
更新
意识到我以前的解决方案不起作用,因为 ImageSource 没有 PixelWidth/PixelHeight,因为它不是 BitmapImage。我必须改用 BitmapImage 资源,但如果我没有在绑定之前声明资源,则绑定将不起作用(有问题吗?)
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?)
ImageBrush 不是满足此类要求的最佳解决方案,因为它没有宽度/高度属性,并且不存在于 VisualTree 中(它只是一个渲染工具)。
图层将帮助您:
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: