【问题标题】:Silverlight Scrollviewer not displaying with bound treeviewSilverlight Scrollviewer 不显示绑定树视图
【发布时间】:2012-07-30 21:05:28
【问题描述】:
我有一个包含在 ScrollViewer 中的 4 行 1 列网格。
这些行是绑定到对象层次结构(嵌套列表)的树视图。
我正在使用 HierarchicalDataTemplate 来布置 TreeViewItem 节点及其子节点/ItemTemplates。
我的问题是,每当我展开 TreeViewItems 时,垂直滚动条 不会 显示出来。
奇怪的是,当我展开作为子节点一部分的 AccordionItem 时,水平滚动条 会显示出来。
我尝试创建一个事件处理程序,该处理程序在 TreeViewItem 展开时调用 UpdateLayout(),但这不会导致滚动条出现。我也尝试针对 TreeView、Grid 和 ScrollViewer 调用 UpdateLayout()。
【问题讨论】:
标签:
binding
silverlight-4.0
treeview
【解决方案1】:
经过一些实验,这个问题的答案变得非常明显。
为 ScrollViewer 指定 Height 属性是使一切按预期工作的缺失部分。
<ScrollViewer HorizontalScrollBarVisibility="Auto"
VerticalScrollBarVisibility="Auto"
Height="350"> <!-- THIS was the missing piece!! -->
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
<RowDefinition Height="Auto"/>
</Grid.RowDefinitions>
<Controls:TreeView Grid.Row="0" ItemsSource="{Binding ListOne}"
ItemTemplate="{StaticResource ListOneTemplate}" >
</Controls:TreeView>
<Controls:TreeView Grid.Row="1" ItemsSource="{Binding ListTwo}"
ItemTemplate="{StaticResource ListTwoTemplate}" >
</Controls:TreeView>
</Grid>
</ScrollViewer>
FWIW,我尝试了许多其他解决方案,包括为 Grid、GridRows 和 TreeView 设置高度和最大高度。我还尝试创建一个事件以在网格高度更改时设置滚动查看器的高度,并在树视图项更改时触发级联 UpdateLayouts() 的事件。
在 ScrollViewer 上设置高度是我发现的唯一让一切按预期工作的方法。
【解决方案2】:
另外,您可以设置行定义:
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
<RowDefinition Height="Auto"/>
<RowDefinition Height="*"/> // tree view position
<RowDefinition Height="Auto"/>
</Grid.RowDefinitions>
树形视图将占用屏幕的可用空间,并在必要时显示滚动条。