简体   繁体   English

ItemsPanel与Grid与GridSplitter

[英]ItemsPanel vs Grid vs GridSplitter

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. 我当前正在尝试为ItemsControl构建一个ControlTemplate,该控件使用Grid作为其ItemsPanel,其中每个项目都水平堆叠并用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. 基本目标是要有一个动态的可绑定ItemsControl,其中所有项目都排成一堆,并且每个项目都可以用拆分器调整大小。

There are two things I can't wrap my head around: How is the GridSplitter supposed to end up automatically between each item? 我不能绕过两件事:GridSplitter如何自动在每个项目之间结束? How do I set Grid.Column for each item. 如何为每个项目设置Grid.Column。

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? 我需要为此编写一个新的ItemsControl吗?

I need actual (Grid)Splitter controls so there can be custom ControlTemplates for them. 我需要实际的(Grid)Splitter控件,因此可以为它们提供自定义ControlTemplates。 Also I think it would come in quite handy to have the additional layout functionality for the cells (GridLengthUnitType, Stretch, Alignment). 另外,我认为为单元格具有其他布局功能(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. 因此,在我自己滚动时,我想我需要一个自定义ItemsControl(为每个项目生成拆分器)和一个自定义面板(其行为类似于onerow / onecolumn网格-因此不需要附加的Grid.Row,Grid.Column属性,.Orientation就足够了),它可以使用Splitter控件,并且知道如何在布局方面进行处理。

What do you think of this approach? 您如何看待这种方法? Is the preferred or a good way? 是首选还是好的方法?

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. 我的理解是ItemsControl是基于这样的思想,即对于每个项目,它仅创建一个控件并将其添加到itemshost。 Creating both a GridSplitter and the default itemcontainer per item goes against this principle. 同时为每个项目创建GridSplitter和默认的itemcontainer违反了这一原则。

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. 由于您只有一列,并且只想垂直调整大小,因此建议您编写自己的面板,该面板的行为类似于StackPanel,但始终在子元素之间留下几个像素的间隙。 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. 因此,调整大小逻辑必须在自定义面板上实现。这被认为是最大的缺点,但是IMO值得这样做,因为它只藏在一个地方。 You don't need to do anything special in your ItemsControl/ItemTemplate/ItemContainerStyle other than use this Panel as itemshost. 除了将此Panel用作itemshost之外,您不需要在ItemsControl / ItemTemplate / ItemContainerStyle中执行任何特殊操作。

You could also use a standard StackPanel and add mouse handlers to it which implement the resize logic. 您还可以使用标准的StackPanel并向其添加实现调整大小逻辑的鼠标处理程序。 But then you'd have to set a margin in your ItemContainerStyle to create the gaps. 但是,然后您必须在ItemContainerStyle中设置边距以创建间隙。

You need to put the gridsplitter in its own column. 您需要将gridsplitter放在其自己的列中。 Assuming the grid will only have one row, there is no need to set the Grid.Row for any item. 假设网格只有一行,则无需为任何项目设置Grid.Row。 If you want, though, you can set it to 0 (the first row). 但是,如果需要,可以将其设置为0(第一行)。

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). 我相信,网格分割器会影响项目本身两侧的大小(我想您想要的是移动所有内容,除了要调整大小的项目)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM