Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/wpf/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何编辑重复按钮间隔?_C#_Wpf_Listbox_Repeatbutton - Fatal编程技术网

C# 如何编辑重复按钮间隔?

C# 如何编辑重复按钮间隔?,c#,wpf,listbox,repeatbutton,C#,Wpf,Listbox,Repeatbutton,有一个列表框和4个项目。 2可见 2 colpased: 点击: -真糟糕 我需要这个: 我需要重新设置按钮更改间隔!?!?如何操作您需要的是,列表框在每次单击“重复”按钮时滚动两行。下面是一个可以添加到列表框中的行为,它可以做到这一点 首先添加此命名空间: xmlns:i="clr-namespace:System.Windows.Interactivity;assembly=System.Windows.Interactivity" 以及对您的项目的相应引用 然后XAML如下所示: &

有一个列表框和4个项目。 2可见 2 colpased:

点击: -真糟糕

我需要这个:


我需要重新设置按钮更改间隔!?!?如何操作

您需要的是,列表框在每次单击“重复”按钮时滚动两行。下面是一个可以添加到
列表框中的行为,它可以做到这一点

首先添加此命名空间:

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;
    }
}
类滚动行为:行为
{
公共整数行乘法器
{
获取{return(int)GetValue(LineMultiplierProperty);}
set{SetValue(LineMultiplierProperty,value);}
}
公共静态只读DependencyProperty LineMultiplierProperty=
Register(“LineMultiplier”、typeof(int)、typeof(ScrollBehavior)、新UIPropertyMetadata(1));
受保护的覆盖无效附加()
{
AssociatedObject.Loaded+=新路由EventHandler(AssociatedObject\u Loaded);
}
私有滚动查看器;
已加载关联对象的私有无效(对象发送方,路由目标)
{
scrollViewer=GetScrollViewer(关联对象);
添加(新的CommandBinding(ScrollBar.LineUpCommand,LineCommandExecuted));
添加(新的CommandBinding(ScrollBar.LineDownCommand,LineCommandExecuted));
}
private void LineCommandExecuted(对象发送方,ExecutedRoutedEventArgs e)
{
if(e.Command==ScrollBar.LineUpCommand)
{
对于(int i=0;i
我有一个应用程序要求我滚动每个项目,所以将scrollviewer.IsVirtualization设置为true就足够了

然而,我需要用dockpanel实现类似的行为,所以我使用Rick Sladkey的方法来完成我所需要的