将网格拉伸至窗口大小

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>

非常感谢您的帮助。

如果有任何疑问,请随时联系我们。

2个回答

22

尝试一下

<Grid Width="{Binding ActualWidth, 
              RelativeSource = {RelativeSource AncestorType = {x:Type Window}}}" 
      Height="{Binding ActualHeight, 
              RelativeSource ={RelativeSource AncestorType = {x:Type Window}}}">

1
搞定了。问题出在<Grid.ColumnDefinitions>上,删除它就可以了 :)

1
是的,需要拉伸的列不应该有 Width="auto" - Vojtěch Dohnal

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接