Hyperlink in C#

Introduction

The HyperLink Control is the control to link to another page. The HyperLink can navigate to an "URL" as well as an XAML page.

HyperLink properties

  1. Background: The background property used to change the background color of the HyperLink control.
  2. ClickMode: The click mode says when to fire a click event.
  3. Foreground: It's the property to change color of the font of content.
  4. Content: Is the content of the control.
  5. Name: Name of the control.
  6. Fontfamily: Font type of the HyperLink content.
  7. Horizontal alignment and vertical alignment: These align the HyperLink control.
  8. Horizontalcontentalignment and  verticalcontentalignment: Aligns the content of the control.
  9. Click: The click event that is fired when the control is clicked

XAML page

<UserControl xmlns:input="clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls.Input"
             x:Class="ControlSamples.MainPage"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
             mc:Ignorable="d"
             d:DesignWidth="640"
             d:DesignHeight="480">
    <ScrollViewer>
        <StackPanel Orientation="Vertical">
            <StackPanel>
                <Grid x:Name="LayoutRoot" Width="1000" Height="100" VerticalAlignment="Top">
                    <Grid.Background>
                        <LinearGradientBrush StartPoint="0.5,0" EndPoint="0.5,1">
                            <GradientStop Color="SkyBlue" Offset="0"/>
                            <GradientStop Color="White" Offset="0.75"/>
                            <GradientStop Color="SkyBlue" Offset="1"/>
                        </LinearGradientBrush>
                    </Grid.Background>
                </Grid>
            </StackPanel>
            <StackPanel Height="500" Width="200" HorizontalAlignment="Left" Margin="0,10,0,0">
                <StackPanel.Background>
                    <LinearGradientBrush StartPoint="0,0" EndPoint="0.5,1">
                        <GradientStop Color="SkyBlue" Offset="0"/>
                        <GradientStop Color="White" Offset="1"/>
                    </LinearGradientBrush>
                </StackPanel.Background>
                <HyperlinkButton Background="Yellow"
                                 Name="MyHyperlink"
                                 ClickMode="Press"
                                 Content="AutoCompleteBox"
                                 Foreground="Blue"
                                 FontFamily="Arial"
                                 FontSize="15"
                                 Click="HyperlinkButton_Click">
                </HyperlinkButton>
            </StackPanel>
        </StackPanel>
    </ScrollViewer>
</UserControl>

The XAML page has the design of the page with the HyperLink Control.

Navigation

<HyperlinkButton Background="Yellow"
                 Name="MyHyperlink"
                 ClickMode="Press"
                 Content="AutoCompleteBox"
                 Foreground="Blue"
                 FontFamily="Arial"
                 FontSize="15"
                 NavigateUri="http://www.google.com">
</HyperlinkButton>

This navigates to the Google page.

Navigation from XAML to another XAML

<HyperlinkButton Background="Yellow"
                 Name="MyHyperlink"
                 ClickMode="Press"
                 Content="AutoCompleteBox"
                 Foreground="Blue"
                 FontFamily="Arial"
                 FontSize="15"
                 Click="HyperlinkButton_Click">
</HyperlinkButton>

Code

private void HyperlinkButton_Click(object sender, RoutedEventArgs e)
{
    this.Content = new Page1();
}

Thank you


Similar Articles