简体   繁体   English

ListBox与ItemTemplate(和ScrollBar!)

[英]ListBox with ItemTemplate (and ScrollBar!)

I have a databound and itemtemplated ListBox: 我有一个数据绑定和itemtemplated ListBox:

<ListBox x:Name="lbLista" 
         ScrollViewer.VerticalScrollBarVisibility="Visible">
    <ListBox.ItemTemplate>
      <DataTemplate>
        <StackPanel Orientation="Horizontal">
          <CheckBox IsChecked="{Binding Deleteable, Mode=TwoWay}" />
          <Label Content="{Binding Name}" />
        </StackPanel>
      </DataTemplate>
    </ListBox.ItemTemplate>
</ListBox>

The ites show fine and they come from an ObservableCollection. 它显示很好,它们来自ObservableCollection。

The problem is the scrollbar which appears but is not usable - it does not have a handle to grab. 问题是出现但不可用的滚动条 - 它没有抓取的句柄。 I've tried setting some ScrollView attached properties on ListBox, but they do not affect the situation. 我已经尝试在ListBox上设置一些ScrollView附加属性,但它们不会影响这种情况。

I pasted your code into test project, added about 20 items and I get usable scroll bars, no problem, and they work as expected. 我将你的代码粘贴到测试项目中,添加了大约20个项目,我得到了可用的滚动条,没有问题,并且它们按预期工作。 When I only add a couple items (such that scrolling is unnecessary) I get no usable scrollbar. 当我只添加一些项目(这样不需要滚动)时,我没有可用的滚动条。 Could this be the case? 可能是这种情况吗? that you are not adding enough items? 你没有添加足够的物品?

If you remove the ScrollViewer.VerticalScrollBarVisibility="Visible" then the scroll bars only appear when you have need of them. 如果删除ScrollViewer.VerticalScrollBarVisibility="Visible"则只有在需要时才会显示滚动条。

ListBox will try to expand in height that is available.. When you set the Height property of ListBox you get a scrollviewer that actually works... ListBox将尝试扩展可用的高度。当您设置ListBox的Height属性时,您将获得一个实际工作的scrollviewer ...

If you wish your ListBox to accodate the height available, you might want to try to regulate the Height from your parent controls.. In a Grid for example, setting the Height to Auto in your RowDefinition might do the trick... 如果您希望ListBox符合可用高度,您可能希望尝试从父控件调节高度。例如,在Grid中,在RowDefinition中将Height设置为Auto可能会有所帮助......

HTH HTH

I have never had any luck with any scrollable content placed inside a stackpanel (anything derived from ScrollableContainer. The stackpanel has an odd layout mechanism that confuses child controls when the measure operation is completed and I found the vertical size ends up infinite, therefore not constrained - so it goes beyond the boundaries of the container and ends up clipped. The scrollbar doesn't show because the control thinks it has all the space in the world when it doesn't. 我从来没有幸运任何可放置在堆叠面板内的可滚动内容(任何从ScrollableContainer派生的内容。stackpanel有一个奇怪的布局机制,当测量操作完成时会混淆子控件,我发现垂直尺寸最终无限,因此不受约束 - 因此它超出了容器的边界并最终被剪切。滚动条没有显示,因为控件认为它没有世界上的所有空间。

You should always place scrollable content inside a container that can resolve to a known height during its layout operation at runtime so that the scrollbars size appropriately. 您应该始终将可滚动内容放在容器中,该容器可以在运行时的布局操作期间解析为已知高度,以便滚动条大小适当。 The parent container up in the visual tree must be able to resolve to an actual height, and this happens in the grid if you set the height of the RowDefinition o to auto or fixed. 可视树中的父容器必须能够解析为实际高度,如果将RowDefinition o的高度设置为auto或fixed,则会在网格中发生这种情况。

This also happens in Silverlight. 这也发生在Silverlight中。

-em- -em-

Thnaks for answer. 谢谢回答。 I tried it myself too to an Empty Project and - lo behold allmighty creator of heaven and seven seas - it worked. 我自己也尝试了空项目 - 看到天堂和七海的全能创造者 - 它起作用了。 I originally had ListBox inside which was inside of root . 我最初的ListBox里面是root。 For some reason ListBox doesn't like being inside of StackPanel, at all! 由于某种原因,ListBox根本不喜欢在StackPanel内部! =) =)

-pom- -pom-

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

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