TextBlock 超链接和锚点?

发布于 2024-10-27 10:33:36 字数 1039 浏览 0 评论 0原文

我有一个像这样的简单 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

很快妥协 2024-11-03 10:33:36

您可以使用 BringIntoView 方法,该方法应将您的scrollViewer滚动到 您在其上调用了 BringIntoView 的 FrameworkElement。下一步是将单击的超链接与目标超链接进行匹配。最直接的方法是使用字典。最后一步是处理 Hyperlink.Click 事件。

隐藏代码:

private readonly Dictionary<Hyperlink,FrameworkElement> HyperlinkTargets = 
                    new Dictionary<Hyperlink,FrameworkElement>();

public Constructor()
{
    InitializeComponent();
    HyperlinkTargets.Add(TestHyperlink, TestAnchor);
}

// this event handler should be attached to hyperlinks which will be used for navigation
private void Hyperlink_Click(object sender, RoutedEventArgs e)
{
    var clickedHyperlink = (Hyperlink)sender;
    var targetHyperlink = HyperlinkTargets[clickedHyperlink];
    targetHyperlink.BringIntoView();
}

这基本上是 HB 评论中想法的实现。


我刚刚想到的另一个解决方案。如果要将更多代码移至 XAML 中,您可以创建一个命令,该命令将导航到作为参数传递的元素。这是一个命令类:

class NavigateToCommand : ICommand
{
    public void Execute(object parameter)
    {
        ((FrameworkElement)parameter).BringIntoView();
    }

    public bool CanExecute(object parameter)
    {
        return parameter is FrameworkElement;
    }

    public event EventHandler CanExecuteChanged;
}

您可以在示例中使用它,如下所示:

<ScrollViewer Height="50" VerticalAlignment="Top">
<ScrollViewer.Resources>
     <local:NavigateToCommand x:Key="navigateToCommand" />
</ScrollViewer.Resources>
<TextBlock>
    <Hyperlink Command="{StaticResource navigateToCommand}"
               CommandParameter="{Binding ElementName=TestAnchor}">Test</Hyperlink><LineBreak />
    /* TextBlocks */
    <Hyperlink Name="TestAnchor" />
</TextBlock>
</ScrollViewer>   

这将允许您在 XAML 中拥有所有内容(新的 ICommand 类除外)

You can use BringIntoView method which should scroll your scrollViewer to the FrameworkElement on which you invoked BringIntoView. The next step is to match clicked hyperlink with target hyperlink. Most straightforward approach would be using a Dictionary. And last step would be to handle Hyperlink.Click event.

Code behind:

private readonly Dictionary<Hyperlink,FrameworkElement> HyperlinkTargets = 
                    new Dictionary<Hyperlink,FrameworkElement>();

public Constructor()
{
    InitializeComponent();
    HyperlinkTargets.Add(TestHyperlink, TestAnchor);
}

// this event handler should be attached to hyperlinks which will be used for navigation
private void Hyperlink_Click(object sender, RoutedEventArgs e)
{
    var clickedHyperlink = (Hyperlink)sender;
    var targetHyperlink = HyperlinkTargets[clickedHyperlink];
    targetHyperlink.BringIntoView();
}

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:

class NavigateToCommand : ICommand
{
    public void Execute(object parameter)
    {
        ((FrameworkElement)parameter).BringIntoView();
    }

    public bool CanExecute(object parameter)
    {
        return parameter is FrameworkElement;
    }

    public event EventHandler CanExecuteChanged;
}

And you can use it in your sample like this:

<ScrollViewer Height="50" VerticalAlignment="Top">
<ScrollViewer.Resources>
     <local:NavigateToCommand x:Key="navigateToCommand" />
</ScrollViewer.Resources>
<TextBlock>
    <Hyperlink Command="{StaticResource navigateToCommand}"
               CommandParameter="{Binding ElementName=TestAnchor}">Test</Hyperlink><LineBreak />
    /* TextBlocks */
    <Hyperlink Name="TestAnchor" />
</TextBlock>
</ScrollViewer>   

This will allow you to have everything (except new ICommand class) in XAML

惯饮孤独 2024-11-03 10:33:36

目标名称 根据 MSDN,仅适用于窗口和框架,因此这是不可能的。

TargetName only works for windows and frames according to MSDN, so this is not possible.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文