将网格拉伸到窗口大小

本文关键字:窗口大小 网格 | 更新日期: 2023-09-27 18:09:30

我刚刚开始学习c# WPF与一个基本的空项目,我想做一个网格,与背景图像,延伸正好在窗口。

现在的样子,网格伸展,但不是我想要的样子。例如,我的背景图像是1000x1000px,我的窗口大小是1700x1200px,所以网格延伸到1200x1200px(它保持图像的长宽比)。我不想要这个,我只想让它在整个窗口延伸。

下面是我的代码:
<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>

将网格拉伸到窗口大小

Try

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

明白了。是<Grid.ColumnDefinitions>把我搞砸了。删除它,它工作了:)