当鼠标悬停在按钮上时,如何在重复按钮中获取滚动功能

时间:2017-12-11 06:18:25

标签: c# wpf

我有一个重复按钮,滚动滚动查看器的内容。我想在鼠标悬停或点击按钮时滚动。

<RepeatButton x:Name="down" 
            Grid.Column="0"
            Grid.Row="0"
          Background="{StaticResource uparrow}" 
            Command="{x:Static ScrollBar.LineDownCommand}"      
            CommandTarget="{Binding ElementName=scrollViewer}">
            <RepeatButton.Visibility >
                <MultiBinding Converter="{StaticResource Converter}">
                    <Binding Path="ActualHeight"  ElementName="scrollViewer"/>
                    <Binding Path="ActualHeight"  ElementName="test"/>
                </MultiBinding>
            </RepeatButton.Visibility>
        </RepeatButton>
        <RepeatButton x:Name="up" 
            Grid.Column="0"
            Grid.Row="2"
          Background="{StaticResource downarrow}" 

            Command="{x:Static ScrollBar.LineUpCommand}"      
            CommandTarget="{Binding ElementName=scrollViewer}">
            <RepeatButton.Visibility>
                <MultiBinding Converter="{StaticResource Converter}">
                    <Binding Path="ActualHeight"  ElementName="scrollViewer"/>
                    <Binding Path="ActualHeight"  ElementName="test"/>
                </MultiBinding>
            </RepeatButton.Visibility>
        </RepeatButton>
        <ScrollViewer Grid.Column="1" Grid.Row="1"  x:Name="scrollViewer" 
                  VerticalScrollBarVisibility="Hidden" 
                  HorizontalScrollBarVisibility="Hidden">
            <StackPanel x:Name="test"  Orientation="Vertical">
            <Button Width="150" Height="20"/>
            <Button Width="150" Height="20"/>
            <Button Width="150" Height="20"/>
            <Button Width="150" Height="20"/>
            <Button Width="150" Height="20"/>
            <Button Width="150" Height="20"/>

            <Button Width="150" Height="20"/>
            </StackPanel>
        </ScrollViewer>

单击按钮时代码有效。但我不知道当鼠标悬停在它上面时如何启用滚动。

2 个答案:

答案 0 :(得分:1)

我通过添加重复按钮的PreviewMouseWheel事件来修复此问题。 背后的代码是

{r, emotional circumplex, fig.align = "center", echo=FALSE}

答案 1 :(得分:0)

您可以尝试使用按钮上的MouseEnterMouseLeave个活动来实现目标。以下代码段可能对您有所帮助:

       private void Mouse_Enter(object sender, MouseEventArgs e)
       {
           int max = txtbox1.Text.Length;
           for (int i = 0; i < max; i++)
           {
               txtbox1.ScrollToVerticalOffset(i);
               //add some delay to see the scrolling effect
           }
       }

      private void Mouse_Leave(object sender, MouseEventArgs e)
      {
          return;
      }