【发布时间】:2018-02-13 07:11:00
【问题描述】:
我有一个 ItemsControl 并将 ItemsPanel 设置为 Canvas。 Canvas 需要能够根据我放入其中的内容动态调整大小,并且当内容超出控件的边界时我需要能够滚动。问题是我无法滚动内容。我将滚动条可见性设置为自动,因此当内容超出边缘时,我最终不会看到滚动条弹出。
我尝试将 ItemsControl 放在 ScrollViewer 中,并尝试在 ItemsControl 的模板中使用 ScrollViewer。
这是 ScrollViewer 中的 ItemsControl:
<ScrollViewer VerticalScrollBarVisibility="Auto" HorizontalScrollBarVisibility="Auto">
<ItemsControl ItemsSource="{Binding Tiles}">
<ItemsControl.ItemsPanel>
<ItemsPanelTemplate>
<Canvas />
</ItemsPanelTemplate>
</ItemsControl.ItemsPanel>
<ItemsControl.ItemContainerStyle>
<Style>
<Setter Property="Canvas.Left" Value="{Binding Left}" />
<Setter Property="Canvas.Top" Value="{Binding Top}" />
</Style>
</ItemsControl.ItemContainerStyle>
<ItemsControl.ItemTemplateSelector>
...
</ItemsControl.ItemTemplateSelector>
</ItemsControl>
</ScrollViewer>
这里是模板中的 ScrollViewer:
<ItemsControl ItemsSource="{Binding Tiles}">
<ItemsControl.ItemsPanel>
<ItemsPanelTemplate>
<Canvas />
</ItemsPanelTemplate>
</ItemsControl.ItemsPanel>
<ItemsControl.ItemContainerStyle>
<Style>
<Setter Property="Canvas.Left" Value="{Binding Left}" />
<Setter Property="Canvas.Top" Value="{Binding Top}" />
</Style>
</ItemsControl.ItemContainerStyle>
<ItemsControl.Template>
<ControlTemplate>
<ScrollViewer VerticalScrollBarVisibility="Auto" HorizontalScrollBarVisibility="Auto">
<ItemsPresenter />
</ScrollViewer>
</ControlTemplate>
</ItemsControl.Template>
<ItemsControl.ItemTemplateSelector>
...
</ItemsControl.ItemTemplateSelector>
</ItemsControl>
This post on MSDN 似乎很有希望,但是在我的代码上实现它,或者甚至用 Canvas 代替 WrapPanel 显式地实现它是行不通的(或者,我应该说,我无法让它工作)。
我还查看了this post,但该解决方案对我不起作用,因为我需要画布能够根据内容调整大小(否则滚动条始终可见)。
提前谢谢您!
【问题讨论】:
标签: wpf canvas scrollviewer itemscontrol