【问题标题】:Zero-height of ScrollViewer-templated item inside ItemsControl?ItemsControl 中 ScrollViewer 模板化项目的高度为零?
【发布时间】:2023-03-25 01:34:01
【问题描述】:

我有一个带有任意项目的ItemsControl。一些项目被包裹在ScrollViewer 中。这些可滚动项的代码隐藏使用ViewportWidth(几乎等同于ActualWidth)和ViewportHeight(几乎等同于ActualHeight)属性来排列/调整其可视子项的大小。只要我不将项目放在ItemsControl 中,这就会起作用。当项目出现在ItemsControl 中时,ViewportHeight 的值等于 0 - 有效地使我的项目不可见。请注意,我想垂直排列项目,使所有项目的高度相等!没有花哨的东西,只是普通的StackPanel

使用DataType自动应用模板:

<MyControl.Resources>
   <DataTemplate DataType="{x:Type MyScrollableItem}">
      <MyControlWrappedInScrollViewer Text="{Binding Text}" />
   </DataTemplate>
   <DataTemplate DataType="{x:Type MyItem}">
      <TextBlock Text="{Binding Text}"/>
   </DataTemplate>
</MyControl.Resources>
<ItemsControl ItemsSource="{Binding MyCollection}" />

MyControlWrappedInScrollViewer 的结构如下所示:

<UserControl>
   <Grid>
      <ScrollViewer CanContentScroll="True">
          <Canvas />
      </ScrollViewer>
   </Grid>
</UserControl>

为什么我的ScrollViewer 的高度为0?我怎样才能告诉我的ItemsControl 适当地调整项目的大小? 例如。一项产生 ItemsControl 高度的高度。两个项目产生一半,依此类推。

【问题讨论】:

  • 当您将 ScrollViewer 中的项目包装在具有 ScrollViewer (ItemsControl) 的项目中时,可能会发生各种有趣的事情;除此之外,它闻起来像是一个糟糕的设计决定,即这根本不是用户友好的。

标签: wpf scrollviewer itemscontrol


【解决方案1】:

这成功了:

<Style TargetType="{x:Type ItemsControl}">
    <Setter Property="ItemsPanel">
        <Setter.Value>
            <ItemsPanelTemplate>
                <UniformGrid Columns="1" IsItemsHost="True"/>
            </ItemsPanelTemplate>
        </Setter.Value>
    </Setter>
</Style>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-02-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-03-26
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多