将网格拉伸到窗口大小

发布于 2024-12-06 13:35:08 字数 1083 浏览 1 评论 0原文

我刚刚开始使用一个基本的空项目学习 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>

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

I've just started learning C# WPF with a basic empty project and I want to make a grid, with a background image, which stretches exactly over the window.

The way it is now, the grid stretches, but not the way I want it. For example my background image is 1000x1000px and my window size is 1700x1200px so the grid stretches to 1200x1200px (it keeps the aspect ratio of the image. I don't want this, I want it simply to stretch all over the window.

Here is my code:

<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>

Any help would be great, thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

℉絮湮 2024-12-13 13:35:08

尝试

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

Try

<Grid Width="{Binding ActualWidth, 
              RelativeSource = {RelativeSource AncestorType = {x:Type Window}}}" 
      Height="{Binding ActualHeight, 
              RelativeSource ={RelativeSource AncestorType = {x:Type Window}}}">
野の 2024-12-13 13:35:08

明白了。正是 把我的事情搞砸了。删除了它,它起作用了:)

Got it figured out. It was the <Grid.ColumnDefinitions> that messed things up for me. Deleted that and it worked :)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文