我是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>
Run Code Online (Sandbox Code Playgroud)
我正在使用Visual Studio 2010和.NET 4.0
您可以将宽度和高度绑定到ImageBource的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>
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
4314 次 |
| 最近记录: |