2009-07-15 72 views
26

我在使用包含ItemsControl的我的堆栈面板周围的scrollviewer控件。当ItemsControl中有很多项目时,它应该滚动,但由于某种原因,它只是削减了项目。下面是代码:ScrollViewer无法在WPF中滚动

<StackPanel> 
    <ScrollViewer CanContentScroll="True" VerticalScrollBarVisibility="Visible"> 
     <ItemsControl Name="icEvents" Width="Auto" Height="100" Background="AliceBlue" 
         ItemsSource="{Binding Path=EventSources}"> 
      <ItemsControl.ItemTemplate> 
       <DataTemplate> 
        <StackPanel> 
         <TextBlock Text="Source:"/> 
         <TextBlock Text="{Binding Path=Source}" /> 
         <TextBlock Text="Original Source:"/> 
         <TextBlock Text="{Binding Path=OriginalSource}" /> 
        </StackPanel> 
       </DataTemplate> 
      </ItemsControl.ItemTemplate> 
     </ItemsControl> 
    </ScrollViewer> 
</StackPanel> 

回答

40

试试你ScrollViwer代替StackPanel周围的网格。我认为StackPanel将提供与内部内容一样多的高度,因此Scrollviwer无法正常工作,因为其高度不受其父级控制的限制。

您可以从下面的示例中了解问题。

<StackPanel> 
    <ScrollViewer> 
     <ItemsControl > 
      <Rectangle Stroke="#FFC3C3C3" Height="300" Fill="Black" StrokeThickness="4" Width="200"/> 
      <Rectangle Stroke="#FFC3C3C3" Width="200" Height="300" Fill="Black" StrokeThickness="4"/> 
      <Rectangle Stroke="#FFC3C3C3" Height="300" Fill="Black" StrokeThickness="4" Width="200"/> 
      <Rectangle Stroke="#FFC3C3C3" Width="200" Height="300" Fill="Black" StrokeThickness="4"/> 
      <Rectangle Stroke="#FFC3C3C3" Width="200" Height="300" Fill="Black" StrokeThickness="4"/> 
     </ItemsControl> 
    </ScrollViewer> 
</StackPanel> 

上面的代码与您的类似,它不会给您滚动条。但看到下面的代码中,我不仅改变了StackPanelGrid(其尊重其子女的基础上面板尺寸大小的任何面板,但StackPanel中没有)

<Grid> 
    <ScrollViewer> 
     <ItemsControl > 
      <Rectangle Stroke="#FFC3C3C3" Height="300" Fill="Black" StrokeThickness="4" Width="200"/> 
      <Rectangle Stroke="#FFC3C3C3" Width="200" Height="300" Fill="Black" StrokeThickness="4"/> 
      <Rectangle Stroke="#FFC3C3C3" Height="300" Fill="Black" StrokeThickness="4" Width="200"/> 
      <Rectangle Stroke="#FFC3C3C3" Width="200" Height="300" Fill="Black" StrokeThickness="4"/> 
      <Rectangle Stroke="#FFC3C3C3" Width="200" Height="300" Fill="Black" StrokeThickness="4"/> 
     </ItemsControl> 
    </ScrollViewer> 
</Grid> 

UPDATE:但如果你真的需要使用StackPanel,那么你可能需要设置大小为您ScrollViwer获取内容滚动

2

ItemsControl应包含ScrollViewer,而不是周围的其他方式。所有ScrollViewer知道的是ItemsControl本身在你的情况 - 它不知道内在的项目。

尝试这样:

<ItemsControl Name="icEvents" Width="Auto" Height="100" 
    Background="AliceBlue" 
    ItemsSource="{Binding Path=EventSources}"> 
    <ItemsControl.ItemTemplate> 
     <DataTemplate> 
      <StackPanel> 
       <TextBlock Text="Source:"/> 
       <TextBlock Text="{Binding Path=Source}" /> 
       <TextBlock Text="Original Source:"/> 
       <TextBlock Text="{Binding Path=OriginalSource}" /> 
      </StackPanel> 
     </DataTemplate> 
    </ItemsControl.ItemTemplate> 
    <ItemsControl.Template> 
     <ControlTemplate TargetType="ItemsControl"> 
      <ScrollViewer VerticalScrollBarVisibility="Auto"> 
       <ItemsPresenter Margin="5" /> 
      </ScrollViewer> 
     </ControlTemplate> 
    </ItemsControl.Template> 
</ItemsControl> 
+0

这是没有必要有一个scrollviwer,在讨论的问题是StackPanel的。 – 2009-07-15 18:16:26

+1

但是,您需要某个ScrollViewer,因为默认情况下ItemsControl不会滚动。我已经使用这个代码,它的工作原理,虽然你的答案也可能工作。 – Andy 2009-07-15 18:18:44

+0

是的,你的代码也可以工作,只有当他移除外层StackPanel时,它并不重要,你在ControlTemplate内部或ItemsControl外部的滚动条都几乎相同。 – 2009-07-15 18:22:15

21

必须解决的ScrollViewer的高度,但可以很容易绑定到StackPanel中的ActualHeight:
(测试代码)

<StackPanel Name="mypanel"> 
    <ScrollViewer Height="{Binding ElementName=mypanel, Path=ActualHeight}"> 
     <ItemsControl> 
      <Rectangle Stroke="#FFC3C3C3" Height="300" Fill="Black" Width="200"/> 
      <Rectangle Stroke="#FFC3C3C3" Width="200" Height="300" Fill="Black" /> 
      <Rectangle Stroke="#FFC3C3C3" Height="300" Fill="Black" Width="200"/> 
      <Rectangle Stroke="#FFC3C3C3" Width="200" Height="300" Fill="Black" /> 
      <Rectangle Stroke="#FFC3C3C3" Width="200" Height="300" Fill="Black" /> 
     </ItemsControl> 
    </ScrollViewer> 
</StackPanel> 

或者更好的是,如果你不能改变的StackPanel的名称:

<StackPanel> 
    <ScrollViewer Height="{Binding RelativeSource={RelativeSource FindAncestor, 
         AncestorType={x:Type StackPanel}}, Path=ActualHeight}"> 
     <ItemsControl> 
      <Rectangle Stroke="#FFC3C3C3" Height="300" Fill="Black" Width="200"/> 
      <Rectangle Stroke="#FFC3C3C3" Width="200" Height="300" Fill="Black" /> 
      <Rectangle Stroke="#FFC3C3C3" Height="300" Fill="Black" Width="200"/> 
      <Rectangle Stroke="#FFC3C3C3" Width="200" Height="300" Fill="Black" /> 
      <Rectangle Stroke="#FFC3C3C3" Width="200" Height="300" Fill="Black" /> 
     </ItemsControl> 
    </ScrollViewer> 
</StackPanel> 

它是一种“你先”的问题,该StackPanel中要求的ScrollViewer的高度和ScrollViewer中问它可以是最大高度的StackPanel。

0

Scroll需要是调整大小的子级的父级,因此请将调整大小控件(在此示例中是堆栈面板)放在其中。

<ScrollViewer> 
    <StackPanel> 
     <Rectangle Stroke="#FFC3C3C3" Height="300" Fill="Black" StrokeThickness="4" Width="200"/> 
     <Rectangle Stroke="#FFC3C3C3" Width="200" Height="300" Fill="Black" StrokeThickness="4"/> 
     <Rectangle Stroke="#FFC3C3C3" Height="300" Fill="Black" StrokeThickness="4" Width="200"/> 
     <Rectangle Stroke="#FFC3C3C3" Width="200" Height="300" Fill="Black" StrokeThickness="4"/> 
     <Rectangle Stroke="#FFC3C3C3" Width="200" Height="300" Fill="Black" StrokeThickness="4"/> 
    </StackPanel> 

0

我有一个问题,其中我在网格中才能看到滚动条呈ItemsControl,我不得不放弃这个网格单元*尺度的高度。

但是,这意味着ItemsControl总是填满单元格,即使只有少数值。

我想ItemsControl只能显示它的大小,但如果它有太多的项目,我希望它滚动。换句话说,我希望这只适用于任何尺寸的显示器。

这段代码在Loaded事件工作对我来说:

Size x = new Size(double.PositiveInfinity, double.PositiveInfinity); 
myItemscontrol.Measure(x);