14

I want to present some text in the GUI and give the user that ability to double click it. I want to catch this event and deal with it.

I thought to do it like this :

 <TextBlock   
        Height="39" 
        TextElement.FontSize="18" 
        FontFamily="Verdana"
        HorizontalAlignment="Left"
        VerticalAlignment="Center"
        Name="Filelink"
        Padding="5,0,0,0"
        TextDecorations="Underline"
        Text="{Binding Path=FilePath}"/>

But seems that it's not easy to deal with clicks in TextBlock .

Any ideas what is the best way to present a click able text.

Thanks.

slavoo
  • 5,798
  • 64
  • 37
  • 39
Night Walker
  • 20,638
  • 52
  • 151
  • 228
  • Is this question similar - http://stackoverflow.com/questions/4022109/wpf-dynamically-created-text-with-clickable-links-in-it-via-binding ? – ChrisF Oct 20 '11 at 21:05
  • 1
    Wouldn't this be confusing? How would the user know he can double click on it? – Justin XL Oct 20 '11 at 22:09

4 Answers4

25

If you want clickable text you can just restyle a Button:

<Button Content="Text here" Click="Click_Handler">
    <Button.Template>
        <ControlTemplate TargetType="{x:Type Button}">
            <ContentPresenter />
        </ControlTemplate>
    </Button.Template>
</Button>

Also see this question.

Community
  • 1
  • 1
H.B.
  • 166,899
  • 29
  • 327
  • 400
  • 2
    As I see it this is the right solution, because then you can also bind commands to the button. when manipulating a textbox you won't have a CommandBindable control – Boas Enkler Oct 21 '11 at 06:31
22

You can embed a hyberlink in a Textblock as shown in this example

<TextBlock>
    <Hyperlink NavigateUri="Reviews.xaml">Click Me </Hyperlink>
</TextBlock>

You can also handle the hyperlinks Click event to call Navigate for example

Loman
  • 989
  • 6
  • 10
  • This answer does not help in handling the Doubleclick event but I upvoted because I think using a hyperlink is a really good practice to show that a text is clickable. – Ucodia Oct 20 '11 at 21:41
2

Why don't you just use a Label and listen for the MouseDoubleClick event (although I do agree with Xin's comment about usability)?

sellmeadog
  • 7,437
  • 1
  • 31
  • 45
1

If using a Label or a Hyperlink won't work in your situation, you could take the approach of a creating a new derived TextBlock that simply defines a new DoubleClick routed event which bubbles up through the tree:

public class ClickableTextBlock : TextBlock
{
    #region Overrides

    protected override void OnMouseLeftButtonDown(System.Windows.Input.MouseButtonEventArgs e)
    {
        base.OnMouseLeftButtonDown(e);

        if (e.ClickCount == 2)
        {
            RaiseEvent(new RoutedEventArgs(DoubleClickEvent, this));
        }
    }

    #endregion

    #region DoubleClick RoutedEvent

    public static readonly RoutedEvent DoubleClickEvent = EventManager.RegisterRoutedEvent("DoubleClick",
        RoutingStrategy.Bubble, typeof(RoutedEventHandler), typeof(ClickableTextBlock));

    public event RoutedEventHandler DoubleClick
    {
        add { AddHandler(DoubleClickEvent, value); }
        remove { RemoveHandler(DoubleClickEvent, value); }
    }

    #endregion
}

This control can be used in the same way as your standard TextBlock. In this example, double clicking on the TextBlock will raise the DoubleClick event which is then acted upon by the parent StackPanel to start an animation:

<StackPanel x:Name="myStackPanel" Background="LightGreen">
        <StackPanel.Triggers>
            <EventTrigger RoutedEvent="l:ClickableTextBlock.DoubleClick">
                <BeginStoryboard>
                    <Storyboard>
                        <DoubleAnimation Storyboard.TargetProperty="Opacity"
                                         Duration="0:0:1"
                                         To="0.5"
                                         FillBehavior="Stop"/>
                    </Storyboard>
                </BeginStoryboard>
            </EventTrigger>
        </StackPanel.Triggers>
        <l:ClickableTextBlock HorizontalAlignment="Center"
                              VerticalAlignment="Center"
                              Background="LightPink"
                              Text="Double Click to change parent's opacity" />
    </StackPanel>

Hope this helps!

FunnyItWorkedLastTime
  • 3,225
  • 1
  • 22
  • 20