2013-02-19 61 views
1

我有10個項目的列表框。 默認情況下垂直滾動已啓用,我可以看到前3個項目。 客戶希望我添加2個按鈕「向上」和「向下」,並且在按鈕點擊列表框中應顯示接下來的3個項目。如何使用WPF ListBox或ScrollView顯示下一個n項目?

例如,我想通過「向下」單擊顯示第4項,第5項,第6項

enter image description here

WPF如何它可以做到與默認控件列表框和滾動型?

+1

+1爲圖形;) – 2013-02-19 17:11:47

回答

1

您可以使用ScrollViewer.ScrollToVerticalOffset方法(msdn)。

例子:

<Grid> 
    <Grid.RowDefinitions> 
     <RowDefinition Height="20" /> 
     <RowDefinition Height="50" /> 
     <RowDefinition Height="20" /> 
     <RowDefinition Height="25" /> 
    </Grid.RowDefinitions> 

    <Button x:Name="btnUp" Content="UP" Click="btnUp_Click" /> 

    <ScrollViewer x:Name="scroll" Grid.Row="1"> 
     <ListBox x:Name="lbData"> 
      <ListBoxItem>Item1</ListBoxItem> 
      <ListBoxItem>Item2</ListBoxItem> 
      <ListBoxItem>Item3</ListBoxItem> 
      <ListBoxItem>Item4</ListBoxItem> 
      <ListBoxItem>Item5</ListBoxItem> 
      <ListBoxItem>Item6</ListBoxItem> 
      <ListBoxItem>Item7</ListBoxItem> 
      <ListBoxItem>Item8</ListBoxItem> 
      <ListBoxItem>Item9</ListBoxItem> 
      <ListBoxItem>Item10</ListBoxItem> 
     </ListBox> 
    </ScrollViewer> 

    <Button x:Name="btnDown" Content="Down" Click="btnDown_Click" Grid.Row="2" /> 

    <StackPanel Grid.Row="3" Orientation="Horizontal"> 
     <TextBlock Text="Start with:" Margin="2" /> 
     <ComboBox x:Name="cbIndex" Loaded="cbIndex_Loaded" Margin="2" /> 
     <Button x:Name="btnGo" Content="GO" Click="btnGo_Click" Margin="2" /> 
    </StackPanel> 
</Grid> 

代碼隱藏:

private void btnUp_Click(object sender, RoutedEventArgs e) 
{ 
    scroll.ScrollToVerticalOffset(scroll.VerticalOffset - 50); 
} 

private void btnDown_Click(object sender, RoutedEventArgs e) 
{ 
    scroll.ScrollToVerticalOffset(scroll.VerticalOffset + 50); 
} 

public double GetOffset(int itemIndex) 
{ 
    double result = 0;   
    for (int i = 0; i < itemIndex; i++) 
    { 
     result += (lbData.Items[i] as ListBoxItem).ActualHeight; 
    } 

    return result; 
} 

private void cbIndex_Loaded(object sender, RoutedEventArgs e) 
{ 
    cbIndex.ItemsSource = Enumerable.Range(1, lbData.Items.Count); 
} 

private void btnGo_Click(object sender, RoutedEventArgs e) 
{ 
    scroll.ScrollToVerticalOffset(GetOffset(cbIndex.SelectedIndex)); 
} 
+0

是否可以確定某些項目的滾動位置。例如,我想我的滾動條從item3開始。我如何確定它的位置? – Evgeny 2013-02-20 14:19:54

+1

@Evgeny再次檢查我的答案!如果你想這樣做,你可以使用'ListBoxItem'類的'ActualHeight'屬性。 – kmatyaszek 2013-02-20 15:49:00

1

解決方案水平列表:

/// <summary> 
     /// Show next n items starting from first visible. 
     /// </summary> 
     public void ShowNext() 
     { 
      if (this.scrollviewer == null) 
      { 
       return; 
      } 

      var rightLimit = this.scrollviewer.HorizontalOffset + this.scrollviewer.ViewportWidth; 
      double horizontalOffset = 0; 

      foreach (var item in this.Items) 
      { 
       var container = this.ItemContainerGenerator.ContainerFromItem(item) as FrameworkElement; 
       if (container == null) 
       { 
        continue; 
       } 

       if (horizontalOffset + container.ActualWidth >= rightLimit) 
       { 
        // We found last item offset 
        break; 
       } 

       horizontalOffset += container.ActualWidth; 
      } 

      this.scrollviewer.ScrollToHorizontalOffset(horizontalOffset); 
     } 

     /// <summary> 
     /// Show previous n items starting last visible item. 
     /// </summary> 
     public void ShowPrevious() 
     { 
      if (this.scrollviewer == null) 
      { 
       return; 
      } 

      double horizontalOffset = 0; 
      foreach (var item in this.Items) 
      { 
       var container = this.ItemContainerGenerator.ContainerFromItem(item) as FrameworkElement; 

       if (container == null) 
       { 
        continue; 
       } 

       horizontalOffset += container.ActualWidth; 
       if (horizontalOffset >= this.scrollviewer.HorizontalOffset) 
       { 
        // We found last item offset 
        break; 
       } 
      } 

      horizontalOffset -= this.scrollviewer.ViewportWidth; 
      this.scrollviewer.ScrollToHorizontalOffset(horizontalOffset); 
     } 

該代碼會顯示從上可見在列表框旁邊\以前的項目。

相關問題