ScrollViewer和ToolTip

时间:2015-06-22 08:45:33

标签: c# wpf tooltip scrollviewer

我有xaml:

<Grid>
    <ScrollViewer x:Name="svViewer" HorizontalScrollBarVisibility="Disabled" VerticalScrollBarVisibility="Hidden" Grid.Column="0" Grid.Row="0">
        <Border>
            <ItemsControl x:Name="svItemControl" VerticalAlignment="Stretch" MouseWheel="svViewer_MouseWheel">

            </ItemsControl>
        </Border>
    </ScrollViewer>
</Grid>

及其代码:

public partial class MainWindow : Window
{

    double Friction;
    private DispatcherTimer animationTimer = new DispatcherTimer();
    double scrollVelocity;
    double scrollOffset;
    const double c_vel_colors = 8;
    public MainWindow()
    {
        InitializeComponent();

        Friction = 0.9;
        InitializeComponent();
        loadContent();

        animationTimer.Interval = new TimeSpan(0, 0, 0, 0, 5);
        animationTimer.Tick += new EventHandler(HandleWorldTimerTick);
        animationTimer.Start();
    }

    private void HandleWorldTimerTick(object sender, EventArgs e)
    {
        if (Math.Abs(scrollVelocity) > 1)
        {
            svViewer.ScrollToVerticalOffset(scrollOffset);
            scrollOffset += scrollVelocity;
            scrollVelocity *= Friction;
        }
    }

    public void svViewer_MouseWheel(object sender, MouseWheelEventArgs e)
    {
        scrollVelocity = (e.Delta > 0) ? -1 * (c_vel_colors) : (c_vel_colors);
        scrollOffset = svViewer.VerticalOffset + scrollVelocity;
    }

    void loadContent()
    {
        StackPanel sp2 = new StackPanel();
        sp2.Orientation = Orientation.Vertical;

        Rectangle[] rc = new Rectangle[50];
        Random rnd = new Random();
        SolidColorBrush _brush;

        for (int i = 0; i < rc.Length; i++)
        {
            _brush = new SolidColorBrush(Color.FromArgb((byte)255, (byte)rnd.Next(0, 255), (byte)rnd.Next(0, 255), (byte)rnd.Next(0, 255)));
            rc[i] = new Rectangle(); rc[i].Height = 50; rc[i].Width = 50;
            rc[i].Fill = _brush;

            StackPanel sp_tt_Colors = new StackPanel();
            Rectangle tt_Rect = new Rectangle
            {
                Fill = _brush,
                Width = 100,
                Height = 100
            };

            TextBlock tb = new TextBlock
            {
                Foreground = new SolidColorBrush(Color.FromArgb((byte)255, (byte)255, (byte)255, (byte)255)),
                FontSize = 12,
                Text = i.ToString()
            };

            sp_tt_Colors.Children.Add(tt_Rect);
            sp_tt_Colors.Children.Add(tb);

            ToolTip tt = new ToolTip();
            ToolTipService.SetIsEnabled(rc[i], true);
            ToolTipService.SetBetweenShowDelay(rc[i], 1000);
            ToolTipService.SetInitialShowDelay(rc[i], 1000);

            tt.Content = sp_tt_Colors;
            tt.Background = new SolidColorBrush(Color.FromArgb((byte)32, (byte)10, (byte)10, (byte)245));

            rc[i].ToolTip = tt;
            sp2.Children.Add(rc[i]);
            i++;
        }

        svItemControl.Items.Add(sp2);
    }
}

目标是使用彩色矩形滚动此列表,我有自己的MouseWheeel事件处理程序 - ScrollViewer在两侧平滑滚动。每个矩形都有自己的工具提示(超大颜色矩形)。

所以,问题是:

  1. 滚动除了我的事件处理程序之外,还有用于scrollviewer工作的标准事件处理程序,因此您可以在滚动开始时看到抖动。如何关闭标准事件处理程序?

  2. 即使我设置了属性ToolTipService.SetBetweenShowDelay和ToolTipService.SetInitialShowDelay,ToolTip也无法正常工作。延迟仅适用于第一次。第一次使用ToolTip后立即出现。因此,滚动工具提示一次又一次出现,这是缓慢和不顺畅工作的原因。如何处理?

  3. 谢谢!

2 个答案:

答案 0 :(得分:0)

  1. e.Handled = true事件处理程序中使用svViewer_MouseWheel来指定您要处理该事件,而不是让ScrollViewer处理它。

  2. 对此问题一无所知......我猜它应该可行,但你只设置了1秒的延迟,这并不比默认值长很多(0.4秒) )。

答案 1 :(得分:-1)

为了浪费旧的事件处理程序,您必须使用覆盖事件创建自定义控件,例如WPF TextBox的示例。

class TextBoxA : TextBox
{
    protected override void OnTouchUp(System.Windows.Input.TouchEventArgs e)
    {
        base.OnTouchUp(e);
    }
}

只需将您使用过的控件替换为新控件即可。应该可以正常工作。

相关问题