TextBlock 超链接和锚点?
我有一个像这样的简单 TextBlock(它必须是 TextBlock):
<ScrollViewer Height="50" VerticalAlignment="Top">
<TextBlock>
<Hyperlink TargetName="TestAnchor">Test</Hyperlink><LineBreak />
<TextBlock Text="Line" /><LineBreak />
<TextBlock Text="Line" /><LineBreak />
<TextBlock Text="Line" /><LineBreak />
<TextBlock Text="Line" /><LineBreak />
<TextBlock Text="Line" /><LineBreak />
<TextBlock Text="Line" /><LineBreak />
<TextBlock Text="Line" /><LineBreak />
<TextBlock Text="Line" /><LineBreak />
<TextBlock Text="Line" /><LineBreak />
<TextBlock Text="Line" /><LineBreak />
<TextBlock Text="Line" /><LineBreak />
<TextBlock Text="Line" /><LineBreak />
<Hyperlink Name="TestAnchor" />
</TextBlock>
</ScrollViewer>
我想做的是,当用户单击顶部的超链接时,它将向下滚动底部的锚点。这在 WPF 中可能吗?
谢谢!
I have a simple TextBlock like this (it has to be a TextBlock):
<ScrollViewer Height="50" VerticalAlignment="Top">
<TextBlock>
<Hyperlink TargetName="TestAnchor">Test</Hyperlink><LineBreak />
<TextBlock Text="Line" /><LineBreak />
<TextBlock Text="Line" /><LineBreak />
<TextBlock Text="Line" /><LineBreak />
<TextBlock Text="Line" /><LineBreak />
<TextBlock Text="Line" /><LineBreak />
<TextBlock Text="Line" /><LineBreak />
<TextBlock Text="Line" /><LineBreak />
<TextBlock Text="Line" /><LineBreak />
<TextBlock Text="Line" /><LineBreak />
<TextBlock Text="Line" /><LineBreak />
<TextBlock Text="Line" /><LineBreak />
<TextBlock Text="Line" /><LineBreak />
<Hyperlink Name="TestAnchor" />
</TextBlock>
</ScrollViewer>
What I would like to do is when the user clicks the HyperLink on top, that it will scroll down the Anchor on the bottom. Is this even possible in WPF?
Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以使用 BringIntoView 方法,该方法应将您的scrollViewer滚动到
您在其上调用了
。下一步是将单击的超链接与目标超链接进行匹配。最直接的方法是使用BringIntoView
的 FrameworkElement字典
。最后一步是处理Hyperlink.Click
事件。隐藏代码:
这基本上是 HB 评论中想法的实现。
我刚刚想到的另一个解决方案。如果要将更多代码移至 XAML 中,您可以创建一个命令,该命令将导航到作为参数传递的元素。这是一个命令类:
您可以在示例中使用它,如下所示:
这将允许您在 XAML 中拥有所有内容(新的
ICommand
类除外)You can use BringIntoView method which should scroll your scrollViewer to the
FrameworkElement
on which you invokedBringIntoView
. The next step is to match clicked hyperlink with target hyperlink. Most straightforward approach would be using aDictionary
. And last step would be to handleHyperlink.Click
event.Code behind:
This is basically an implementation of H.B.'s idea from his comment.
Another solution which just come to my mind. If you want to move more code into XAML you can create a command which will navigate to element passed as parameter. Here is a command class:
And you can use it in your sample like this:
This will allow you to have everything (except new
ICommand
class) in XAML目标名称
根据 MSDN,仅适用于窗口和框架,因此这是不可能的。TargetName
only works for windows and frames according to MSDN, so this is not possible.