0

In XAML how can I set a hyperlink to take the user to a particular section of my window. Like how you can with anchor tags in HTML. Basically we want the user to be able to click on an error in a list of errors and the link will take them to that area.

twreid
  • 1,453
  • 2
  • 22
  • 42

1 Answers1

1

XAML Hyperlink NavigateUri could work with a bit of code behind, i.e.

<Window x:Class="fwAnchorInWindow.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
    <StackPanel>
        <TextBox x:Name="TextBoxName" Text="Enter Name"/>
        <TextBox x:Name="TextBoxNumber" Text="Enter Number"/>
        <TextBlock>
          <Hyperlink NavigateUri="TextBoxName" RequestNavigate="Hyperlink_RequestNavigate">
              There is a name error.
          </Hyperlink>
        </TextBlock>
    </StackPanel>
</Window>

using System.Windows;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Navigation;
namespace fwAnchorInWindow
{
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }

        private void Hyperlink_RequestNavigate(object sender, RequestNavigateEventArgs e)
        {
            if (sender is Hyperlink)
            {
                string controlName = ((Hyperlink)sender).NavigateUri.ToString();
                IInputElement control = (IInputElement)this.FindName(controlName);
                Keyboard.Focus(control);
            }
        }
    }
}

FindName is only one way to find a child control. There are also other ways per this post: WPF ways to find controls.

It is also important to note that WPF distinguishes between Logical Focus and Keybaord Focus: Mark Smith's It's Bascially Focus. In the code above having keyboard focus automatically indicates logical focus.

Community
  • 1
  • 1
SpeedCoder5
  • 8,188
  • 6
  • 33
  • 34