我有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在两侧平滑滚动。每个矩形都有自己的工具提示(超大的彩色矩形)。
因此,问题是:
在滚动时,除了我的事件处理程序外,还有一个用于滚动查看器的标准事件处理程序,所以您可以看到滚动开始时的抖动。如何关闭standart事件处理程序?
即使我在ShowDelay和ToolTipService.SetInitialShowDelay之间设置了属性ToolTipServices.SetBet,ToolTip也不能像我预期的那样工作。延迟只适用于第一次。第一次出现工具提示后立即出现。因此,当滚动工具提示一次又一次地出现时,这就是工作缓慢和不流畅的原因。如何处理?
谢谢!
-
在
svViewer_MouseWheel
事件处理程序中使用e.Handled = true
,指定要处理该事件,而不是让ScrollViewer处理它。 -
对此一无所知。。。我想它应该有效,尽管你只设置了1秒的延迟,这并不比默认值(0.4秒)长多少。
为了浪费旧的事件处理程序,您必须创建一个带有overriden事件的自定义控件,如本例中的WPF TextBox。
class TextBoxA : TextBox
{
protected override void OnTouchUp(System.Windows.Input.TouchEventArgs e)
{
base.OnTouchUp(e);
}
}
只需用新控件替换您使用过的控件。。应该有效。