WPF Grid 允许内部控件自动调整宽度/高度 [英] WPF Grid allow controls inside to auto size width/height

查看:169
本文介绍了WPF Grid 允许内部控件自动调整宽度/高度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嘿,如果用户屏幕大于默认尺寸,我一直试图找到如何自动调整网格控件中控件大小的示例.

Hey all i have been trying to find examples of how to go about auto sizing the controls i have within a grid control if the users screen is larger than the default size.

目前我无法在放大表单时调整控件的大小.当前是否有任何代码可以找到网格内的所有控件并在调整表单大小时即时调整它们的大小?

Currently i am unable to resize the controls when i enlarge the form. Is there any code currently that can find all controls inside the grid and resize them on the fly when the form is resized?

我当前的代码是:

<Window x:Class="Window1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="Window1" Height="768" Width="1024">
    <Grid>
        <Grid Height="115" Margin="190,0,195,145" Name="Grid1" VerticalAlignment="Bottom" Background="Cyan">
            <Button Height="45" HorizontalAlignment="Left" Margin="10,10,0,0" Name="Button4" VerticalAlignment="Top">Button</Button>
            <Button Height="45" HorizontalAlignment="Left" Margin="101,10,0,0" Name="Button5" VerticalAlignment="Top">Button</Button>
            <Button Height="45" HorizontalAlignment="Left" Margin="192,10,0,0" Name="Button6" VerticalAlignment="Top">Button</Button>
            <Button Height="45" HorizontalAlignment="Right" Margin="280,10,257,0" Name="Button7" VerticalAlignment="Top">Button</Button>
            <Button Height="45" HorizontalAlignment="Right" Margin="0,10,166,0" Name="Button8" VerticalAlignment="Top">Button</Button>
            <Button Height="45" HorizontalAlignment="Right" Margin="0,10,75,0" Name="Button9" VerticalAlignment="Top">Button</Button>
            <Button Height="45" HorizontalAlignment="Left" Margin="10,0,0,9" Name="Button10" VerticalAlignment="Bottom">Button</Button>
            <Button Height="45" HorizontalAlignment="Left" Margin="101,0,0,9" Name="Button11" VerticalAlignment="Bottom">Button</Button>
            <Button Height="45" HorizontalAlignment="Left" Margin="192,0,0,9" Name="Button12" VerticalAlignment="Bottom">Button</Button>
            <Button Height="45" HorizontalAlignment="Right" Margin="280,0,257,9" Name="Button13" VerticalAlignment="Bottom">Button</Button>
            <Button Height="45" HorizontalAlignment="Right" Margin="0,0,166,9" Name="Button14" VerticalAlignment="Bottom">Button</Button>
            <Button Height="45" HorizontalAlignment="Right" Margin="0,0,75,9" Name="Button15" VerticalAlignment="Bottom">Button</Button>
        </Grid>
    </Grid>
</Window>

任何帮助都会很棒!谢谢!

Any help would be great! Thanks!

推荐答案

看起来您没有按照预期的方式使用网格.

It looks like you aren't using the grid in the way it was intended.

如果您希望 UI 随窗口动态调整大小,则不应有任何(或至少最小化)硬编码宽度和高度的数量.我看到的第一个问题是内部网格的硬编码高度为 127,因此无论窗口的大小如何,网格都将始终为该高度.消除该高度是一个很好的第一步.

If you want your UI to dynamically resize with the window, you shouldn't have any (or at least minimize) the number of hard coded widths and heights. The first problem I see is that the inner grid has a hard coded height of 127, so regardless of the size of the window that grid will always be that height. Removing that height is a good first step.

我也不知道为什么有两个网格 - 难道所有的按钮不能都是外部网格的子元素吗?

I'm also not sure why there are two grids - couldn't all of the buttons just be children of the outer grid?

最后,您没有在网格中定义任何行或列.我认为您试图在各个按钮上使用 VerticalAlignmentHorizo​​ntalAlignment 属性来控制它们在窗口上的位置.根据您希望按钮所在的位置定义行和列要好得多.MSDN 有一个关于如何使用多行多列的网格.

Finally, you aren't defining any rows or columns in the grid. I think you were trying to use the VerticalAlignment and HorizontalAlignment properties on the individual buttons to control where they are on the window. It is much better to define rows and columns based on where you want the buttons to be. MSDN has an example of how to use a grid with multiple rows and columns.

这篇关于WPF Grid 允许内部控件自动调整宽度/高度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆