WPF Grid:如果我需要插入新行,是否有一种简单的方法可以重新调整行项目? [英] WPF Grid: Is there an easy way to readjust the row items if I need to insert new rows?

查看:23
本文介绍了WPF Grid:如果我需要插入新行,是否有一种简单的方法可以重新调整行项目?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

网格行和列的定义似乎是这样硬编码的:

It appears that grid row and column definitions are hard-coded like this:

Grid.Row="3" Grid.Column="1"

我正在开发一个新的 WPF 应用程序,我不得不不断地在我的网格中添加和删除新行,因为客户决定了表单的外观.我发现这是一项相当乏味的任务.如果在顶部附近插入一行,我必须手动更改我刚刚插入的行下方的 XAML 中的所有行索引.

I am in the middle of development on a new WPF app and I am having to constantly add and delete new rows from my grid as the client makes up their mind on how the form should look. I am discovering that this is quite a tedious task. If insert a row near the top, I have to manually change all the row indexes in the XAML beneath the row I just inserted.

有自动调整所有行的简单方法吗?

Is there an easy way to auto-adjust all the rows?

推荐答案

一个 Grid 是一个 grid 并且行和列被编号是为了特定的原因.这就像一张方格纸被切成固定数量的正方形.因为行数和列数是固定的,所以您可以执行强大的操作,例如跨行和跨列.相反,对于可变数量的行或列,这灵活.

A Grid is a grid and the rows and columns are numbered for a specific reason. It's like a piece of graph paper sliced up into a fixed numbers of squares. Because the numbers of rows and columns is fixed you can do powerful things like row and column spanning. Conversely, this is not flexible for a variable number of rows or columns.

如果您需要可变数量的行,或者您在设计时不断添加行,那么 Grid 可能不是最适合您的设计元素.或者更好的是,您可以将 Grid 与另一个布局元素结合使用,以获得所需的灵活性.

If you need a variable number of rows, or you are continually adding rows at design time, then it may be that the Grid is not the best design element for you. Or better still, you can use the Grid in combination with another layout element to get the flexibility you need.

例如,您可以将所有可变行放入单个 Grid 行,现在您的网格行数不再变化.但是,您将需要一个支持可变行数的布局元素才能放入该 Grid 行.好吧,有很多可供选择,但有两个有用的例子是 StackPanelDockPanel.

For example, you can put all your variable rows into a single Grid row and now your grid row count doesn't change any more. But then you will need a layout element that supports a variable number of rows to put into that Grid row. Well, there are a lot of them to choose from but two for example that are useful are StackPanel and DockPanel.

布局元素 DockPanel 特别是对于可变数量的行或列非常强大,尤其是在 DockPanels 或其他中用作 DockPanels 时组合.处理此布局问题所需要做的就是细分和征服

The layout element DockPanel in particular is very powerful for variable numbers of rows or columns, particularly when used as DockPanels within DockPanels or other combinations. All you need to handle this layout problem is to subdivide and conquer!

下面是 DockPanel 的介绍:WPF 教程:Dock 面板

这篇关于WPF Grid:如果我需要插入新行,是否有一种简单的方法可以重新调整行项目?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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