2011-01-13 57 views

回答

1

你想要的是讓列表框每按一次滾動兩行就可以點擊重複按鈕。這是一種可以添加到您的ListBox中的行爲,可以做到這一點。

首先添加此命名空間:

xmlns:i="clr-namespace:System.Windows.Interactivity;assembly=System.Windows.Interactivity" 

和相應的參考項目。

然後XAML看起來像這樣:

<ListBox ScrollViewer.VerticalScrollBarVisibility="Visible" Height="40"> 
    <i:Interaction.Behaviors> 
     <local:ScrollBehavior LineMultiplier="2"/> 
    </i:Interaction.Behaviors> 
    <ListBoxItem Content="Item1"/> 
    <ListBoxItem Content="Item2"/> 
    <ListBoxItem Content="Item3"/> 
    <ListBoxItem Content="Item4"/> 
</ListBox> 

,這裏是行爲:

class ScrollBehavior : Behavior<FrameworkElement> 
{ 
    public int LineMultiplier 
    { 
     get { return (int)GetValue(LineMultiplierProperty); } 
     set { SetValue(LineMultiplierProperty, value); } 
    } 

    public static readonly DependencyProperty LineMultiplierProperty = 
     DependencyProperty.Register("LineMultiplier", typeof(int), typeof(ScrollBehavior), new UIPropertyMetadata(1)); 

    protected override void OnAttached() 
    { 
     AssociatedObject.Loaded += new RoutedEventHandler(AssociatedObject_Loaded); 
    } 

    private ScrollViewer scrollViewer; 

    private void AssociatedObject_Loaded(object sender, RoutedEventArgs e) 
    { 
     scrollViewer = GetScrollViewer(AssociatedObject); 
     scrollViewer.CommandBindings.Add(new CommandBinding(ScrollBar.LineUpCommand, LineCommandExecuted)); 
     scrollViewer.CommandBindings.Add(new CommandBinding(ScrollBar.LineDownCommand, LineCommandExecuted)); 
    } 

    private void LineCommandExecuted(object sender, ExecutedRoutedEventArgs e) 
    { 
     if (e.Command == ScrollBar.LineUpCommand) 
     { 
      for (int i = 0; i < LineMultiplier; i++) 
       scrollViewer.LineUp(); 
     } 

     if (e.Command == ScrollBar.LineDownCommand) 
     { 
      for (int i = 0; i < LineMultiplier; i++) 
       scrollViewer.LineDown(); 
     } 
    } 

    private ScrollViewer GetScrollViewer(DependencyObject o) 
    { 
     if (o is ScrollViewer) 
      return o as ScrollViewer; 
     for (int i = 0; i < VisualTreeHelper.GetChildrenCount(o); i++) 
     { 
      var result = GetScrollViewer(VisualTreeHelper.GetChild(o, i)); 
      if (result != null) 
       return result; 
     } 
     return null; 
    } 
} 
0

我有哪些要求我每個項目的滾動所以設置scrollviewer.isvirtualizing到真正的是一個應用程序足夠。

但是,我需要實現與dockpanel類似的行爲,所以我用Rick Sladkey的方法來完成我所需要的。