A have ListBox and 4 Items. 2 visible 2 colpased:
Click:
-this bad!
I need this:
I need Set in reapeatButton change Interval!?!? how to do it
What you want is for the list box to scroll by two lines for every one time you click the repeat buttons. Here is a behavior that you can add to your ListBox
that will do just that.
First add this namespace:
xmlns:i="clr-namespace:System.Windows.Interactivity;assembly=System.Windows.Interactivity"
and the corresponding reference to your project.
Then the XAML looks like this:
<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>
and here is the behavior:
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;
}
}
I had an application which required me to scroll per item so setting scrollviewer.isvirtualizing to true was enough.
However, I needed to implement a similar behavior with a dockpanel, so I used Rick Sladkey's method to accomplish what I needed.