我有一个ItemsControl,我将ItemsPanel设置为Canvas。Canvas需要能够根据我放入其中的内容动态调整大小,并且当内容超出控件边界时,我需要能够滚动。问题是我无法使内容滚动。我将滚动条可见性设置为自动,这样当内容超出边缘时就不会看到滚动条弹出。
我尝试将ItemsControl放在ScrollViewer中,并尝试在ItemsControl的模板中使用ScrollViewer。
以下是ScrollViewer内部的ItemsControl:
我尝试将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>
这篇MSDN文章看起来很有前途,但是在我的代码中实现它,甚至用Canvas代替WrapPanel明确地实现它都没有效果(或者说,我无法让它工作)。
我也看了这篇文章,但解决方案对我不起作用,因为我需要Canvas能够随内容大小调整(否则滚动条总是可见的)。
提前感谢你的帮助!