2011-01-13 4 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

내가 그렇게했다 true로 scrollviewer.isvirtualizing 설정 항목 당 스크롤 내게 필요한 응용 프로그램을 가지고 충분히.

그러나 Dockpanel과 비슷한 동작을 구현해야하므로 필자가 필요로했던 것을 달성하기 위해 Rick Sladkey의 방법을 사용했습니다.

관련 문제