ItemsPanel、Grid、GridSplitter

发布于 2024-08-23 02:05:44 字数 626 浏览 6 评论 0原文

我目前正在尝试为 ItemsControl 构建一个 ControlTemplate,该控件使用 Grid 作为其 ItemsPanel,其中每个项目水平堆叠并用 GridSplitter 分隔。

基本目标是拥有一个动态可绑定 ItemsControl,其中所有项目堆叠成一行,并且每个项目都可以使用拆分器调整大小。

有两件事我无法理解:GridSplitter 应该如何在每个项目之间自动结束?如何为每个项目设置 Grid.Column。

如果这不能用简单的控制模板来完成,那么实现这样的事情的常见且好的方法是什么?我需要为此编写一个新的 ItemsControl 吗?

我需要实际的 (Grid)Splitter 控件,以便可以为它们提供自定义 ControlTemplates。另外,我认为为单元格提供额外的布局功能(GridLengthUnitType、拉伸、对齐)会非常方便。

因此,当我自己滚动时,我想我需要一个自定义 ItemsControl (为每个项目生成拆分器)和一个自定义面板(其行为类似于单行/单列网格 - 因此不需要附加的 Grid.Row、Grid.Column 属性,.Orientation 就足够了),可以采用 Splitter 控件并知道如何在布局方面处理它们。

您对这种方法有何看法?是首选还是好的方法?

I am currently trying to build a ControlTemplate for an ItemsControl that uses the Grid as its ItemsPanel where each item is stacked horizontally and delimited with a GridSplitter.

The basic goal is to have a dynamic bindable ItemsControl where all items stack up in a row and where each item can be resized with a splitter.

There are two things I can't wrap my head around: How is the GridSplitter supposed to end up automatically between each item? How do I set Grid.Column for each item.

If this can't be done with a simple control template what would be a common and good way to implement something like this? Do I need to write a new ItemsControl for this?

I need actual (Grid)Splitter controls so there can be custom ControlTemplates for them. Also I think it would come in quite handy to have the additional layout functionality for the cells (GridLengthUnitType, Stretch, Alignment).

So when rolling my own I guess I would need a custom ItemsControl (that generates the splitters for each item) and a custom panel (that behaves like a onerow/onecolumn grid - so no need for the attached Grid.Row, Grid.Column properties, .Orientation would suffice) that can take Splitter controls and knows how to deal with them in terms of layouting.

What do you think of this approach? Is the preferred or a good way?

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

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

发布评论

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

评论(2

長街聽風 2024-08-30 02:05:44

我的理解是,ItemsControl 基于这样的想法:对于每个项目,它仅创建一个控件并将其添加到 itemshost。为每个项目创建 GridSplitter 和默认项目容器违反了这一原则。

由于您只有一列,并且只想垂直调整大小,因此我建议编写您自己的面板,其行为类似于 StackPanel,但总是在子元素之间留下几个像素的间隙。如果鼠标位于此间隙上方,并且用户开始拖动,面板可以调整最近的子元素的大小。

因此,调整大小逻辑必须在自定义面板上实现,这被认为是最大的缺点,但在我看来这是值得的,因为它只隐藏在一个地方。除了使用此面板作为 itemshost 之外,您不需要在 ItemsControl/ItemTemplate/ItemContainerStyle 中执行任何特殊操作。

您还可以使用标准 StackPanel 并向其中添加鼠标处理程序,以实现调整大小逻辑。但是,您必须在 ItemContainerStyle 中设置边距才能创建间隙。

My understanding is that the ItemsControl is based on the idea that for each item it only creates and adds one control to the itemshost. Creating both a GridSplitter and the default itemcontainer per item goes against this principle.

Since you only have one column, and only want to resize vertically, i'd suggest writing your own panel, which behaves like a StackPanel but always leaves a gap of a few pixels between the child elements. If the mouse is over this gap, and the user begins to drag, the panel can resize the nearest child elements.

So the resize logic would have to be implemented on the custom panel., which is think the biggest drawback, but IMO well worth it because it is tucked away in one place only. You don't need to do anything special in your ItemsControl/ItemTemplate/ItemContainerStyle other than use this Panel as itemshost.

You could also use a standard StackPanel and add mouse handlers to it which implement the resize logic. But then you'd have to set a margin in your ItemContainerStyle to create the gaps.

何时共饮酒 2024-08-30 02:05:44

您需要将 gridsplitter 放在其自己的列中。假设网格只有一行,则无需为任何项目设置 Grid.Row。不过,如果您愿意,可以将其设置为 0(第一行)。

不过,这可能是编写您自己的自定义容器的一个很好的理由。也许基于堆栈面板而不是网格。我相信 gridsplitter 会影响其自身两侧的项目大小(我认为您想要的是它移动除正在调整大小的项目之外的所有项目)。

You need to put the gridsplitter in its own column. Assuming the grid will only have one row, there is no need to set the Grid.Row for any item. If you want, though, you can set it to 0 (the first row).

This might be a good reason to write your own custom container, though. Maybe based on the stackpanel instead of the grid. I believe that the gridsplitter will effect the size of the items on either side of itself (I think what you want is for it to shift all except for te one it is resizing).

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