如何编辑repeatButton Interval?

时间:2011-01-13 23:04:40

标签: c# wpf listbox repeatbutton

A有ListBox和4个项目。 2可见 2 colpased:

alt text

点击: alt text - 这太糟糕了!

我需要这个:

alt text

我需要在reapeatButton中设置更改间隔!?!?该怎么做

2 个答案:

答案 0 :(得分: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;
    }
}

答案 1 :(得分:0)

我有一个应用程序要求我滚动每个项目,所以将scrollviewer.isvirtualizing设置为true就足够了。

但是,我需要使用dockpanel实现类似的行为,所以我使用Rick Sladkey的方法来完成我需要的东西。