[英]ScrollViewer and ToolTip
我有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可以在两侧平滑滚动。 每个矩形都有自己的工具提示(超大颜色矩形)。
因此,问题是:
在滚动时,除了我的事件处理程序外,还有一个用于滚动查看器工作的标准事件处理程序,因此您可以在滚动开始时看到抖动。 如何关闭标准事件处理程序?
即使我设置了属性ToolTipService.SetBetweenShowDelay和ToolTipService.SetInitialShowDelay,ToolTip也无法正常工作。 延迟仅在第一次工作。 第一次出现后,ToolTip显得非常重要。 因此,在滚动时,ToolTip会反复出现,这就是工作缓慢且不流畅的原因。 怎么处理呢?
谢谢!
在svViewer_MouseWheel
事件处理程序中使用e.Handled = true
来指定您要处理该事件,而不是让ScrollViewer处理它。
对此一无所知...我想它应该可以工作,尽管您将延迟设置为仅1秒,实际上并没有比默认值(0.4秒)长很多。
为了浪费旧的事件处理程序,您必须创建一个具有覆盖事件的自定义控件,例如本示例中的WPF TextBox。
class TextBoxA : TextBox
{
protected override void OnTouchUp(System.Windows.Input.TouchEventArgs e)
{
base.OnTouchUp(e);
}
}
只需将您使用的控件替换为新的控件即可。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.