作者:余逮月笑下死手 | 来源:互联网 | 2023-09-05 16:07
IvecreatedaListBoxtodisplayitemsingroups,wherethegroupsarewrappedrighttoleftwhent
I've created a ListBox to display items in groups, where the groups are wrapped right to left when they can no longer fit within the height of the ListBox's panel. So, the groups would appear similar to this in the listbox, where each group's height is arbitrary (group 1, for instance, is twice as tall as group 2):
我创建了一个ListBox来显示组中的项目,当它们不再适合ListBox面板的高度时,这些组从右到左包装。因此,组在列表框中看起来与此类似,其中每个组的高度是任意的(例如,组1是组2的两倍):
[ 1 ][ 3 ][ 5 ]
[ ][ 4 ][ 6 ]
[ 2 ][ ]
The following XAML works correctly in that it performs the wrapping, and allows the horizontal scroll bar to appear when the items run off the right side of the ListBox.
以下XAML正常工作,它执行包装,并允许水平滚动条在项目从ListBox的右侧运行时出现。
The problem occurs when a group of items is longer than the height of the WrapPanel. Instead of allowing the vertical scroll bar to appear to view the cutoff item group, the items in that group are simply clipped. I'm assuming that this is a side effect of the Height binding in the WrapPanel - the scrollbar thinks it does not have to enabled.
当一组项目长于WrapPanel的高度时,会发生此问题。不是允许垂直滚动条显示以查看截止项组,而是简单地剪切该组中的项目。我假设这是WrapPanel中高度绑定的副作用 - 滚动条认为它不必启用。
Is there any way to enable the scrollbar, or another way around this issue that I'm not seeing?
有没有办法启用滚动条,或者我没有看到这个问题的另一种方法?
4 个解决方案
2
By setting the Height property on the WrapPanel to the height of the ScrollContentPresenter, it will never scroll vertically. However, if you remove that Binding, it will never wrap, since in the layout pass, it has infinite height to layout in.
通过将WrapPanel上的Height属性设置为ScrollContentPresenter的高度,它将永远不会垂直滚动。但是,如果删除该Binding,它将永远不会换行,因为在布局过程中,它具有无限的布局高度。
I would suggest creating your own panel class to get the behavior you want. Have a separate dependency property that you can bind the desired height to, so you can use that to calculate the target height in the measure and arrange steps. If any one child is taller than the desired height, use that child's height as the target height to calculate the wrapping.
我建议您创建自己的面板类以获得所需的行为。有一个单独的依赖项属性,您可以将所需的高度绑定到,因此您可以使用它来计算度量中的目标高度并安排步骤。如果任何一个孩子的身高高于所需的身高,请使用该孩子的身高作为目标身高来计算包裹。
Here is an example panel to do this:
以下是执行此操作的示例面板:
public class SmartWrapPanel : WrapPanel
{
///
/// Identifies the DesiredHeight dependency property
///
public static readonly DependencyProperty DesiredHeightProperty = DependencyProperty.Register(
"DesiredHeight",
typeof(double),
typeof(SmartWrapPanel),
new FrameworkPropertyMetadata(Double.NaN,
FrameworkPropertyMetadataOptions.AffectsArrange |
FrameworkPropertyMetadataOptions.AffectsMeasure));
///
/// Gets or sets the height to attempt to be. If any child is taller than this, will use the child's height.
///
public double DesiredHeight
{
get { return (double)GetValue(DesiredHeightProperty); }
set { SetValue(DesiredHeightProperty, value); }
}
protected override Size MeasureOverride(Size constraint)
{
Size ret = base.MeasureOverride(constraint);
double h = ret.Height;
if (!Double.IsNaN(DesiredHeight))
{
h = DesiredHeight;
foreach (UIElement child in Children)
{
if (child.DesiredSize.Height > h)
h = child.DesiredSize.Height;
}
}
return new Size(ret.Width, h);
}
protected override System.Windows.Size ArrangeOverride(Size finalSize)
{
double h = finalSize.Height;
if (!Double.IsNaN(DesiredHeight))
{
h = DesiredHeight;
foreach (UIElement child in Children)
{
if (child.DesiredSize.Height > h)
h = child.DesiredSize.Height;
}
}
return base.ArrangeOverride(new Size(finalSize.Width, h));
}
}