我跟着this關於如何向ItemsControl添加滾動條的小教程,它在Designer視圖中工作,但不是當我編譯和執行程序時(只有前幾項顯示出來,並且沒有滾動條可以查看更多 - 即使VerticalScrollbarVisibility設置爲「可見」而不是「自動」時)。WPF:使用滾動條的ItemsControl(滾動查看器)
關於如何解決這個問題的任何想法?
這是我用它來顯示我的物品(一般我用數據綁定工作,但看到我設計的項目我手動添加它們)的代碼:
<ItemsControl x:Name="itemCtrl" Style="{DynamicResource UsersControlStyle}">
<ItemsControl.ItemsPanel>
<ItemsPanelTemplate>
<StackPanel Orientation="Vertical" HorizontalAlignment="Center" VerticalAlignment="Top">
</StackPanel>
</ItemsPanelTemplate>
</ItemsControl.ItemsPanel>
<uc:UcSpeler />
<uc:UcSpeler />
<uc:UcSpeler />
<uc:UcSpeler />
<uc:UcSpeler />
</ItemsControl>
這我的模板:
<Style x:Key="UsersControlStyle" TargetType="{x:Type ItemsControl}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type ItemsControl}">
<Border SnapsToDevicePixels="true" Background="{TemplateBinding Background}" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" Padding="{TemplateBinding Padding}">
<ScrollViewer VerticalScrollBarVisibility="Visible">
<ItemsPresenter SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}"/>
</ScrollViewer>
</Border>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
很好,謝謝! – Xuntar 2010-01-08 16:28:28
當你看到它時非常明顯......因爲我是來自Windows Forms,我經常發現自己陷入了錯誤的思維模式。看來WPF維權很多...... +1。 – 2010-11-09 08:26:14
非常感謝 - 非常有幫助。我同意萊特的觀點,即我的WinForms大腦最初不會「得到」這個。 – itsmatt 2010-12-14 20:22:52