2012-03-07 29 views
1

是否有可能爲兩個列表框分別放置一個滾動條,以便它們像一個列表框一樣平滑滾動。 在此先感謝:)兩個列表框,一個在另一個的頂部有一個滾動條

+0

有沒有理由不能只使用一個列表框? – 2012-03-07 18:10:54

+0

列表框放在彼此的頂部或..? – 2012-03-07 18:11:09

+1

「在彼此之上」在Y或Z方向? – 2012-03-07 18:11:58

回答

1

你的問題不是很清楚,但我相信你可以使用這樣的事情:

<ScrollViewer Height="50"> 
    <StackPanel> 
     <ListBox> 
      <ListBoxItem Content="00 -Item0"/> 
      <ListBoxItem Content="00 -Item1"/> 
      <ListBoxItem Content="00 -Item2"/> 
     </ListBox> 
     <ListBox> 
      <ListBoxItem Content="01 -Item0"/> 
      <ListBoxItem Content="01 -Item1"/> 
      <ListBoxItem Content="01 -Item2"/> 
     </ListBox> 
    </StackPanel> 
</ScrollViewer> 
+0

是否需要硬編碼scrollviewer的高度? – Girirsh 2012-03-07 18:25:31

+0

不...它只是樣本 – DmitryG 2012-03-07 18:27:03

+0

但嘗試用這個列表框中的幾個項目... – 2012-03-07 18:57:28

1

您可以將它們疊放在另一個之上,並使它們都自動調整大小,以使它們都沒有滾動條。

然後將該工具放置在ScrollViewer中。

我不是100%確定AutoSize/No Scrollbar是不是Std ListBox的選項,但您應該可以改用ItemsPanel。

0

下面的代碼示例應該幫助,如果我理解你的意圖。

注意使用UIHelpers.FindVisualChild(...)此方法的代碼可以通過搜索「wpf FindVisualChild」在線找到。 此外,垂直偏移(e.NewValue * 10)的計算似乎很好,但10的值是從幾個測試中得出的。您可能會計算出更好的價值或以更好的方式推導出它。

<Grid > 
    <Grid.ColumnDefinitions> 
     <ColumnDefinition Width="Auto"/> 
     <ColumnDefinition Width="Auto"/> 
    </Grid.ColumnDefinitions> 
    <Grid.RowDefinitions> 
     <RowDefinition Height="Auto"/> 
    </Grid.RowDefinitions> 
    <StackPanel Orientation="Vertical" Grid.Row="0" Grid.Column="0"> 
     <ListBox Name="ListBox1" Height="50" Width="100" ScrollViewer.VerticalScrollBarVisibility="Hidden"> 
      <ListBoxItem Content="Item0"/> 
      <ListBoxItem Content="Item1"/> 
      <ListBoxItem Content="Item2"/> 
      <ListBoxItem Content="Item3"/> 
      <ListBoxItem Content="Item4"/> 
      <ListBoxItem Content="Item5"/> 
      <ListBoxItem Content="Item6"/> 
      <ListBoxItem Content="Item7"/> 
      <ListBoxItem Content="Item8"/> 
      <ListBoxItem Content="Item9"/> 
     </ListBox> 
     <ListBox Name="ListBox2" Height="50" Width="100" ScrollViewer.VerticalScrollBarVisibility="Hidden"> 
      <ListBoxItem Content="Item0"/> 
      <ListBoxItem Content="Item1"/> 
      <ListBoxItem Content="Item2"/> 
      <ListBoxItem Content="Item3"/> 
      <ListBoxItem Content="Item4"/> 
      <ListBoxItem Content="Item5"/> 
      <ListBoxItem Content="Item6"/> 
      <ListBoxItem Content="Item7"/> 
      <ListBoxItem Content="Item8"/> 
      <ListBoxItem Content="Item9"/> 
     </ListBox> 
    </StackPanel> 
    <ScrollBar Scroll="HandleScollChangeScrollBar" Height="100" Grid.Row="0" Grid.Column="1"/> 
</Grid> 

private void HandleScollChangeScrollBar(object sender, System.Windows.Controls.Primitives.ScrollEventArgs e) 
{ 
    ScrollViewer scrollViewer1 = UIHelpers.FindVisualChild<ScrollViewer>(ListBox2); 
    scrollViewer1.ScrollToVerticalOffset(e.NewValue * 10); 

    ScrollViewer scrollViewer2 = UIHelpers.FindVisualChild<ScrollViewer>(ListBox1); 
    scrollViewer2.ScrollToVerticalOffset(e.NewValue * 10); 
} 
0

我想你設置ScrollViewer.CanContentScroll在頂部列表框=「假」 - 我有這個確切的問題。

相關問題