Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ssl/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
WPF ListBox WrapPanel剪辑长组_Wpf_Listbox_Scroll_Grouping_Wrappanel - Fatal编程技术网

WPF ListBox WrapPanel剪辑长组

WPF ListBox WrapPanel剪辑长组,wpf,listbox,scroll,grouping,wrappanel,Wpf,Listbox,Scroll,Grouping,Wrappanel,我已经创建了一个列表框来显示分组中的项目,当这些组不再适合列表框面板的高度时,它们会从右向左包装。因此,组在列表框中的显示类似于此,其中每个组的高度是任意的(例如,组1的高度是组2的两倍): 下面的XAML工作正常,因为它执行换行,并允许在项目从列表框的右侧运行时显示水平滚动条 <ListBox> <ListBox.ItemsPanel> <ItemsPanelTemplate> <StackPanel Orientatio

我已经创建了一个列表框来显示分组中的项目,当这些组不再适合列表框面板的高度时,它们会从右向左包装。因此,组在列表框中的显示类似于此,其中每个组的高度是任意的(例如,组1的高度是组2的两倍):

下面的XAML工作正常,因为它执行换行,并允许在项目从列表框的右侧运行时显示水平滚动条

<ListBox> 
  <ListBox.ItemsPanel> 
    <ItemsPanelTemplate> 
      <StackPanel Orientation="Vertical"/> 
    </ItemsPanelTemplate> 
  </ListBox.ItemsPanel> 

  <ListBox.GroupStyle> 
    <ItemsPanelTemplate> 
      <WrapPanel Orientation="Vertical" 
                 Height="{Binding Path=ActualHeight, 
                          RelativeSource={RelativeSource 
                            FindAncestor, 
                            AncestorLevel=1, 
                            AncestorType={x:Type ScrollContentPresenter}}}"/> 
    </ItemsPanelTemplate> 
  </ListBox.GroupStyle> 
</ListBox>

当一组项目的长度超过包装袋的高度时,就会出现问题。不允许垂直滚动条显示以查看“截止”项目组,而是简单地剪裁该组中的项目。我假设这是WrapPanel中高度绑定的副作用-滚动条认为它不必启用


是否有任何方法可以启用滚动条,或者有其他方法可以解决我没有看到的问题?

我认为您是正确的,它与绑定有关。移除绑定时会发生什么?使用绑定时,您是否尝试填充列表框的整个高度?如果是这样,请考虑绑定到MyHealm,或者尝试使用<代码>垂直对齐< /代码>属性。

< P>谢谢您的回答,戴维。 当绑定被移除时,不会发生包裹。WrapPanel将每个组放入一个垂直列中

绑定的目的是强制WrapPanel实际进行包装。如果未设置绑定,WrapPanel将假定高度是无限的,并且从不缠绕


绑定到
MinHeight
会导致一个空列表框。我可以看出
VerticalAlignment
属性似乎是一个解决方案,但是对齐本身阻止了任何包装的发生。当绑定和对齐一起使用时,对齐对问题没有影响。

通过将WrapPanel上的Height属性设置为ScrollContentPresenter的高度,它将永远不会垂直滚动。但是,如果删除该绑定,它将永远不会换行,因为在布局过程中,它的布局高度是无限的

我建议您创建自己的panel类以获得所需的行为。有一个单独的依赖属性,可以将所需高度绑定到该属性,以便可以使用该属性计算测量和排列步骤中的目标高度。如果任何一个子项的高度高于所需的高度,请使用该个子项的高度作为计算包裹的目标高度

下面是执行此操作的示例面板:

public class SmartWrapPanel : WrapPanel
{
    /// <summary>
    /// Identifies the DesiredHeight dependency property
    /// </summary>
    public static readonly DependencyProperty DesiredHeightProperty = DependencyProperty.Register(
        "DesiredHeight",
        typeof(double),
        typeof(SmartWrapPanel),
        new FrameworkPropertyMetadata(Double.NaN, 
            FrameworkPropertyMetadataOptions.AffectsArrange |
            FrameworkPropertyMetadataOptions.AffectsMeasure));

    /// <summary>
    /// Gets or sets the height to attempt to be.  If any child is taller than this, will use the child's height.
    /// </summary>
    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));
    }
}
公共类SmartWrapPanel:WrapPanel
{
/// 
///标识所需的高度依赖项属性
/// 
公共静态只读DependencyProperty DesiredHeightProperty=DependencyProperty.Register(
“理想高度”,
类型(双),
类型(SmartWrapPanel),
新的FrameworkPropertyMetadata(Double.NaN,
FrameworkPropertyMetadataOptions.AffectsRange|
FrameworkPropertyMetadataOptions.AffectsMeasure));
/// 
///获取或设置要尝试达到的高度。如果任何子级高于此高度,将使用该子级的高度。
/// 
公众双期望高度
{
获取{return(double)GetValue(DesiredHeightProperty);}
set{SetValue(DesiredHeightProperty,value);}
}
受保护的覆盖尺寸测量覆盖(尺寸约束)
{
尺寸ret=基准测量超越(约束);
双h=返回高度;
如果(!Double.IsNaN(所需高度))
{
h=所需高度;
foreach(UIElement子元素中的子元素)
{
如果(child.DesiredSize.Height>h)
h=child.DesiredSize.Height;
}
}
返回新尺寸(返回宽度,h);
}
受保护的覆盖System.Windows.Size ArrangeOverride(大小最终化)
{
双h=最终确定高度;
如果(!Double.IsNaN(所需高度))
{
h=所需高度;
foreach(UIElement子元素中的子元素)
{
如果(child.DesiredSize.Height>h)
h=child.DesiredSize.Height;
}
}
返回base.ArrangeOverride(新大小(finalSize.Width,h));
}
}

这是一个稍加修改的代码,它允许水平和垂直滚动,这都归功于之前发布它的Abe Heidebrecht。唯一的变化是MeasureOverride的返回值需要是base.MeasureOverride(新大小(ret.width,h))

//原始代码:Abe Heidebrecht
公共类SmartWrapPanel:WrapPanel
{
/// 
///标识所需的高度依赖项属性
/// 
公共静态只读DependencyProperty DesiredHeightProperty=DependencyProperty.Register(
“理想高度”,
类型(双),
类型(SmartWrapPanel),
新的FrameworkPropertyMetadata(Double.NaN,
FrameworkPropertyMetadataOptions.AffectsRange|
FrameworkPropertyMetadataOptions.AffectsMeasure));
/// 
///获取或设置要尝试达到的高度。如果任何子级高于此高度,将使用该子级的高度。
/// 
公众双期望高度
{
获取{return(double)GetValue(DesiredHeightProperty);}
set{SetValue(DesiredHeightProperty,value);}
}
受保护的覆盖尺寸测量覆盖(尺寸约束)
{
尺寸ret=基准测量超越(约束);
双h=返回高度;
如果(!Double.IsNaN(所需高度))
{
h=所需高度;
foreach(UIElement子元素中的子元素)
{
如果(child.DesiredSize.Height>h)
h=child.DesiredSize.Height;
}
}
返回基准测量值(新尺寸(相对宽度,h));
}
受保护的覆盖System.Windows.Size ArrangeOverride(大小最终化)
{
双h=最终确定高度;
如果(!Double.IsNaN(Desi
public class SmartWrapPanel : WrapPanel
{
    /// <summary>
    /// Identifies the DesiredHeight dependency property
    /// </summary>
    public static readonly DependencyProperty DesiredHeightProperty = DependencyProperty.Register(
        "DesiredHeight",
        typeof(double),
        typeof(SmartWrapPanel),
        new FrameworkPropertyMetadata(Double.NaN, 
            FrameworkPropertyMetadataOptions.AffectsArrange |
            FrameworkPropertyMetadataOptions.AffectsMeasure));

    /// <summary>
    /// Gets or sets the height to attempt to be.  If any child is taller than this, will use the child's height.
    /// </summary>
    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));
    }
}
// Original code : Abe Heidebrecht
public class SmartWrapPanel : WrapPanel
{
  /// <summary>
  /// Identifies the DesiredHeight dependency property
  /// </summary>
  public static readonly DependencyProperty DesiredHeightProperty = DependencyProperty.Register(
    "DesiredHeight",
    typeof(double),
    typeof(SmartWrapPanel),
    new FrameworkPropertyMetadata(Double.NaN, 
            FrameworkPropertyMetadataOptions.AffectsArrange |
            FrameworkPropertyMetadataOptions.AffectsMeasure));

  /// <summary>
  /// Gets or sets the height to attempt to be.  If any child is taller than this, will use the child's height.
  /// </summary>
  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 base.MeasureOverride(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));
  }
}