如何在图像上添加“地标” WPF 中的滚动条内?
我有一个长滚动查看器,我想在滚动条上用小图像标记重要点。如果单击图像,滚动条将跳转到相应的内容。
我已经在一些应用程序中看到了此功能,例如 Eclipse 和 Chrome 并想知道如何使用 WPF 重现它。
I have a long Scroll Viewer and I want to mark important spots with small images on the scroll bar. If the the image is clicked the scroll bar will jump to the corresponding content.
I've seen this functionality in a few applications such as Eclipse and Chrome and was wondering how to reproduce it with WPF.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
简短的回答是“更改 ScrollBar 的模板”。
长答案是...我将在 ScrollBar 控件的模板中添加一个 ItemsControl 。我会将此 ItemsControl 放在模板顶部,并将其 IsHitTestVisible 设置为 false,以便它不会捕获鼠标事件。
然后我将使用 Canvas 作为 ItemsPanelTemplate 以便能够正确放置这些点。我将使用数据绑定与 ItemsControl 的 ItemsSource 属性和 DataTemplate 以便使用图像呈现每个元素。
这是我使用 Blend 制作的示例。当然它并不完整(例如它不处理鼠标事件),但我希望它将成为您的起点。
(来源:japf.fr)
Short answer is "change the template of the ScrollBar".
Long answer is... That I would add an ItemsControl in the template of the ScrollBar control. I would put this ItemsControl on top of the template with its IsHitTestVisible set to false so that it does not capture mouse events.
Then I would use a Canvas as the ItemsPanelTemplate in order to be able to place the spots properly. I would use databinding with the ItemsSource property of the ItemsControl and a DataTemplate in order to render each element with an image.
Here is a sample that I did using Blend. Of course it's not complete (it does not handle mouse event for example), but I hope it will be a starting point for you.
(source: japf.fr)
为 japfs 答案做出贡献:
我解决了调整大小问题的更新:
您可以使用 japfs Style 并将 ItemsSource 应用于 ItemControl:
只需确保 Positions 为 ObservableCollection 类型,并且在 SizeChanged 事件中重新计算位置。另外,在该事件调用中(您的 ViewModel 应该实现的 INotifyPropertyChanged 接口)
首先尝试使用列表,但没有正确更新。与 ObservableCollection 一起工作得很好。
To contribute to japfs answer:
I solved the update on resize issue:
You can use japfs Style and apply a ItemsSource to the ItemControl:
Just make sure Positions is of type ObservableCollection and the positions are recalculated in SizeChanged-event. Additionally in that event call (INotifyPropertyChanged interface which should your ViewModel implement)
Tried it with a List first, but that did not update correctly. Worked with ObservableCollection just fine.