5

我刚开始用一个基本的空项目学习 C# WPF,我想制作一个带有背景图像的网格,它正好延伸到窗口上。

现在的方式,网格延伸,但不是我想要的方式。例如,我的背景图像是 1000x1000 像素,我的窗口大小是 1700x1200 像素,因此网格拉伸到 1200x1200 像素(它保持图像的纵横比。我不想要这个,我希望它只是拉伸整个窗口。

这是我的代码:

<Window x:Class="Backgammon.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Window1" Height="auto" Width="auto">
<Grid VerticalAlignment="Stretch">
    <Grid.ColumnDefinitions>
        <ColumnDefinition Width="auto" MinWidth="510" />
    </Grid.ColumnDefinitions>
    <Image Source="C:\Users\Edy\Pictures\cool-wallpapers1.jpg" Stretch="UniformToFill" HorizontalAlignment="Left"></Image>
    <Button Height="33" HorizontalAlignment="Right" Margin="0,0,12,12" Name="button1" VerticalAlignment="Bottom" Width="145" Click="button1_Click" ClipToBounds="False">Connect</Button>
    <ListBox Margin="12,12,0,149" Name="listBox1" HorizontalAlignment="Left" Width="225" />
</Grid>

任何帮助都会很棒,谢谢。

4

2 回答 2

17

尝试

<Grid Width="{Binding ActualWidth, 
              RelativeSource = {RelativeSource AncestorType = {x:Type Window}}}" 
      Height="{Binding ActualHeight, 
              RelativeSource ={RelativeSource AncestorType = {x:Type Window}}}">
于 2011-09-22T21:45:12.170 回答
1

想通了。这<Grid.ColumnDefinitions>让我搞砸了。删除它并且它有效:)

于 2011-09-22T21:44:15.570 回答