2015-06-22 63 views
2

我有XAML:的ScrollViewer和工具提示

<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滾動雙方順利進行。每個矩形都有自己的ToolTip(超大顏色矩形)。

所以,問題是:

  1. 雖然滾動,除了我的事件處理程序沒有爲ScrollViewer中的作品非標準事件處理程序,所以你可以看到滾動的開始顫抖。如何關閉標準事件處理程序?

  2. 即使我設置了屬性ToolTipService.SetBetweenShowDelay和ToolTipService.SetInitialShowDelay,ToolTip也無法正常工作。延遲只是第一次。 ToolTip第一次出現後立即出現。所以,當滾動工具提示一次又一次出現時,這是工作緩慢和不流暢的原因。如何處理它?

謝謝!

回答

-1

對於浪費舊的事件處理程序,您必須創建一個帶有重寫事件的自定義控件,如WPF文本框的此示例。

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

簡單地用你的新控件替換你的使用控制。應該工作。

+0

你的例子與這個問題並不真正相關......如何重寫,以幫助他管理滾動? – almulo

+0

請在downvoting之前閱讀該問題。 「如何關閉標準事件處理程序?」 – Marc

0
  1. 使用e.Handled = truesvViewer_MouseWheel事件處理程序,指定要處理的,而不是讓ScrollViewer中處理它該事件。

  2. 對此一無所知...我想它應該工作,雖然你設置的延遲只有1秒,這並不比默認值(0.4秒)長很多。

+0

1.正常!謝謝。 2.絕非。即使我第一次將工具提示設置爲延遲5000毫秒,它將重置爲默認值。 – CannyBaker

+0

這很奇怪...沒有多少使用'ToolTipService',所以我沒有更多的想法 – almulo