WPF 网格:如果我需要插入新行,是否有一种简单的方法来重新调整行项目?

发布于 2024-11-08 07:37:18 字数 238 浏览 7 评论 0原文

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

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

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

有没有一种简单的方法可以自动调整所有行?

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

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

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?

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

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

发布评论

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

评论(4

述情 2024-11-15 07:37:18

在 Visual Studio 2015 中,您还可以将鼠标悬停在行的边缘上,然后会出现一个小下拉框。当您单击向下箭头时,其中两个选项是“在前面移动行”和“在后面移动行”。这是我发现的最好的方法。

示例图片:

截图

In Visual Studio 2015 you can also hover over the edge of a row and a little dropdown box will appear. When you click the down arrow two of the options are "move row before" and "move row after". That's the best way I've found.

Example image:

screenshot

提笔落墨 2024-11-15 07:37:18

Grid 是一个网格,行和列因特定原因而编号。它就像一张方格纸被切成固定数量的正方形。由于行数和列数是固定的,因此您可以执行强大的操作,例如行和列跨越。相反,对于可变数量的行或列来说,这灵活。

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

例如,您可以将所有变量行放入单个 Grid 行中,现在网格行数不再发生变化。但是,您将需要一个布局元素,该元素支持将可变数量的行放入该 Grid 行中。嗯,有很多可供选择,但其中两个很有用,例如 StackPanelDockPanel

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

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

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.

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.

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.

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!

Here's an introduction to DockPanel: WPF Tutorial: Dock Panel

橘虞初梦 2024-11-15 07:37:18

没有简单的方法可以自动调整网格的行/列。

我过去使用过的一个潜在替代方案是 UniformGrid,因为它不需要在每个项目中引用行/列。它非常适合屏蔽数据的占位符。

如果您正在进行初始设计,我建议您仅使用 Visual Studio 中的拖放界面或 Balsamiq混合。 Balsamiq 是我的最爱,因为它故意看起来不像实际的应用程序,因此客户不会沉迷于事物的实际外观,而是专注于数据。

There is no easy way to auto-adjust your rows/columns for a Grid.

A potential alternative that I've used in the past is a UniformGrid since it doesn't need a Row/Column reference in each item. It's nice for blocking out placeholders for the data.

If you are doing initial designs, I'd recommend just using the Drag/Drop interface in visual studio or a design tool like Balsamiq or Blend. Balsamiq is my favorite since it intentionally doesn't look like an actual application, so clients don't get hung up on the actual appearance of the thing and focus on the data instead.

时光与爱终年不遇 2024-11-15 07:37:18

我知道这是一个非常古老的问题,但如果其他人遇到它,在 Visual Studio 2013 中(也许更早,我不确定)你可以将鼠标悬停在网格的边缘,它会让你将一行分成两行。

它将使行的高度达到设定的数字,因此您可能需要返回并更改为“自动”或“*”或您想要使用的任何内容。它还使原始行中的任何内容跨越两行,因此您需要删除这些项目上的 RowSpan="2" 属性并将它们放入正确的行中,但它会自动将较低行中的所有项目向下推送。

它不是全自动的,并且需要进行一些清理,但如果您有很多行,它比手动对所有内容重新编号要快。

I know this is a super old question but in case anyone else comes across it, in Visual Studio 2013 (maybe earlier too I'm not sure) you can just hover over the edge of the grid and it will let you split a row into two rows.

It will make the rows a set number height so you will probably need to go back and change then to 'Auto' or '*' or whatever you would like to use. It also makes anything in the original row span both rows so you would need to remove the RowSpan="2" attribute on those items and put them into the correct rows, but it will push all items in lower rows down automatically.

It's not fully automatic and there is a little cleanup, but if you have a lot of rows it's faster than renumbering everything manually.

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