WPF Interactive Text Display

I want to present some text in the graphical interface and give the user the opportunity to double-click on it. I want to catch this event and deal with it.

I decided 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 it seems that clicks in TextBlock not easy to handle.

Any ideas are the best way to present a clickable text.

Thanks.

+6
source share
4 answers

You can embed hypertext in a Textblock, as shown in this example.

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

You can also handle hyperlinks. Click an event to trigger Navigation, for example

+16
source

If you need text with text, you can simply reinstall Button :

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

Also see this question .

+15
source

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

+2
source

If using Label or Hyperlink will not work in your situation, you can take the approach of creating a new derived TextBlock that simply defines a new routed DoubleClick event that bubbles up 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 the standard TextBlock . In this example, double-clicking on the TextBlock icon will trigger the DoubleClick event, which will then act as the parent StackPanel to trigger the 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 opacity" /> </StackPanel> 

Hope this helps!

+1
source

Source: https://habr.com/ru/post/899737/


All Articles