0

iam trying to create a TextBlock with runobject at runtime. i need the textblock to show following text:

Please Click Here to go to next Page.

Click here should be a "link" like, where the user can click on it and he will move to the next tabitem ..

i have:

var tbGoToNextTab = new TextBlock
 {
  Content = "Please Click Here to go to next Page.",
  Foreground = new SolidColorBrush(Colors.Black)
 };
 
 tbGoToNextTab .Click +=new RoutedEventHandler(tbGoToNextTab_Click);

how would i just make the Click Here Clickable, have underline and blue text color and perform an action when clicking on it ?

thanks in advance

EDIT:

I just want the 2 Words "Click Here" clickable ...the rest should be displayed as normal text.. I think i should be doing this with Inlines.. any suggestions ?

Community
  • 1
  • 1
lebhero
  • 1,391
  • 5
  • 18
  • 35

5 Answers5

2

Something like this? (untested)

var hyperlink    = new HyperLink(new Run("Click Here"));
hyperlink.Click += new RoutedEventHandler(tbGoToNextTab_Click);

var span = new Span();
span.Inlines.Add(new Run("Please "));
span.Inlines.Add(hyperlink);
span.Inlines.Add(new Run(" to go to next Page"));

var tbGoToNextTab = new TextBlock
{
  Content = span,
  Foreground = new SolidColorBrush(Colors.Black)
};
GazTheDestroyer
  • 20,722
  • 9
  • 70
  • 103
1

Ok found the Answer ...

As i supposed i had to use the Inline property ..

i found the answer in this post:

Add hyperlink to textblock wpf From Stanislav Kniazev

Thanks all...

Community
  • 1
  • 1
lebhero
  • 1,391
  • 5
  • 18
  • 35
0

Have something like this.. Textblock as content in the button..so that its clickable.

        Canvas pnel = new Canvas();
        var btntab = new Button();
        var tbGoToNextTab1 = new TextBlock();
        var tbGoToNextTab2 = new TextBlock();
        var tbGoToNextTab3 = new TextBlock();
        tbGoToNextTab1.Text = "Please ";
        tbGoToNextTab2.Text = "Click Here";
        tbGoToNextTab3.Text = " to go to next Page.";
        tbGoToNextTab1.Margin = new Thickness(0, 0, 0, 0);
        btntab.Margin = new Thickness(40, 0, 0, 0);
        tbGoToNextTab3.Margin = new Thickness(95, 0, 0, 0);
        tbGoToNextTab1.Foreground = new SolidColorBrush(Colors.Black);
        tbGoToNextTab2.Foreground = new SolidColorBrush(Colors.Black);
        tbGoToNextTab3.Foreground = new SolidColorBrush(Colors.Black);
        btntab.Click += new RoutedEventHandler(btntab_Click);
        btntab.Content = tbGoToNextTab2;
        pnel.Children.Add(tbGoToNextTab1);
        pnel.Children.Add(btntab);
        pnel.Children.Add(tbGoToNextTab3);
        Text112.Children.Add(pnel);
Mullaly
  • 320
  • 4
  • 18
0

To manage a left click action, add a handler on the PreviewMouseLeftButtonDown

tbGoToNextTab.PreviewMouseLeftButtonDown += OntbGoToNextTabPreviewMouseLeftButtonDown;

void OntbGoToNextTabPreviewMouseLeftButtonDown(object sender, System.Windows.Input.MouseButtonEventArgs e)
    {
        // write code here...
    }

To manage underline and color change, write a Style in xaml, in this Style put a Trigger that will change your TextBlock on the IsMouseOver property change.

<Window.Resources>
    <Style x:Key="HyperLinkStyle" TargetType="{x:Type TextBlock}">
        <Style.Triggers>
            <Trigger Property="IsMouseOver" Value="True">
                <Setter Property="TextDecorations" Value="Underline" />
            </Trigger>
        </Style.Triggers>
    </Style>
</Window.Resources>

tbGoToNextTab.Style = FindResource("HyperLinkStyle") as Style;

You will have to declare a TextBlock specifically for the clickable text and other TextBlocks for the static text.

Sisyphe
  • 4,626
  • 1
  • 25
  • 39
0

If you handle Click event for the TextBlock, it will process click anywhere within the boundary of the control.

If you need only part of the text clickable, you should place a Hyperlink within the body of your TextBlock. By the way, it has an extra benefit of highlighting the clickable area so that your was not confused with the text that says, "Click Here" which is not clickable.

Here's relevant WPF snippet:

<TextBlock>
    <Run>Please</Run>
    <Hyperlink>
        <Run Text="Click Here"/>
    </Hyperlink>
    <Run>to go to next Page.</Run>
</TextBlock>
Be Brave Be Like Ukraine
  • 7,596
  • 3
  • 42
  • 66