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!