c# - 将网格拉伸(stretch)到窗口大小

标签 c# wpf grid size stretch

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

现在的样子,网格拉伸(stretch),但不是我想要的方式。例如,我的背景图像是 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>

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

最佳答案

尝试

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

关于c# - 将网格拉伸(stretch)到窗口大小,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7521841/

相关文章:

html - 在 div 中垂直间隔元素(flexbox space-around 不起作用..)

c# - 使用 MonoDroid 接收短信

c# - 在 WPF 中使用字典进行双向数据绑定(bind)

c# - WPF如何优化布局/渲染周期?

wpf - 如何将许多图像拼接在一起以提供像 Photosynth 一样的 3D 沉浸式体验?

html - 在这种情况下使用整数作为类名是否可以接受/正确

css 属性 - 底部不适用于位置为 :fixed 的网格容器

c# - 在 C# 中从列表到对象的装箱和拆箱

javascript - ASP.NET mcv : How to send javascript array to c# controller

c# - 如何在 Linq C# 中执行此操作