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!
source share