Possible bug: Hyperlink in a ListBox does not work
I have used the following dataTemplate to populate a WPF listbox.... What happens is that when I click the link it does not open in web browser.
<
DataTemplate x:Key="SearchResultsListBoxItemsTemplate1">
<StackPanel x:Name="StackPanel">
<TextBlock x:Name="TextBlock" Padding="0,10,0,0" Text="{Binding Title}" FontSize="11" Foreground="Blue" TextWrapping="Wrap"/>
<TextBlock x:Name="TextBlock1" Text="{Binding Description}" MaxHeight="27" FontSize="11" TextWrapping="Wrap" TextTrimming="CharacterEllipsis"/>
<TextBlock>
<Hyperlink NavigateUri="{Binding Link}">
<TextBlock Text="{Binding Link}" FontSize="11" Foreground="DarkGray" TextWrapping="NoWrap" TextTrimming="CharacterEllipsis"/>
</Hyperlink>
</TextBlock>
</StackPanel>
</DataTemplate>I also have one more problem.... When i click the link it selects a listbox element, and I don't want that.... I only want to open the link in a web browser....
Any ideas what might be wrong?
P.S. I am using this template in a desktrop wpf application.
Answers
you have to handle RequestNavigate event
<
ListBox ItemTemplate="{StaticResource dtq}" Name="list1" Hyperlink.RequestNavigate="hyperlink_RequestNavigate"></
ListBox>void
hyperlink_RequestNavigate(object sender, System.Windows.Navigation.RequestNavigateEventArgs e){
System.Diagnostics.
Process.Start(e.Uri.ToString());}
All Replies
Also is there a way to change the underline color from blue to black, and when mouse is over from blue to gray?
Thanks in advance
- Marko Vuksnovic.you have to handle RequestNavigate event
<
ListBox ItemTemplate="{StaticResource dtq}" Name="list1" Hyperlink.RequestNavigate="hyperlink_RequestNavigate"></
ListBox>void
hyperlink_RequestNavigate(object sender, System.Windows.Navigation.RequestNavigateEventArgs e){
System.Diagnostics.
Process.Start(e.Uri.ToString());}
Hey Lee,
Thanks, this worked fine.... I still need answer to my previous questions....
markovuksanovic wrote: Also is there a way to change the underline color from blue to black, and when mouse is over from blue to gray?
Thanks in advance
- Marko Vuksnovic.so if anyone knows, please do let me know how to solve that problem...
- One more question, why is this event handled in the listbox and not in the datatemplate (hyperlink), where it is defined?
- we can handle in one place as the events are bubbled up if not marked handled=true on the source that raised the event
try this
<TextBlock>
<Hyperlink NavigateUri="{Binding}">
<Hyperlink.Resources>
<Style TargetType="{x:Type Hyperlink}">
<Style.Triggers>
<Trigger Property="Hyperlink.IsMouseOver" Value="True">
<Setter Property="Hyperlink.Foreground" Value="gray"></Setter>
</Trigger>
</Style.Triggers>
</Style>
</Hyperlink.Resources>
<Hyperlink.TextDecorations>
<TextDecoration
PenThicknessUnit="FontRecommended">
<TextDecoration.Pen>
<Pen Brush="Black" Thickness="1" />
</TextDecoration.Pen>
</TextDecoration>
</Hyperlink.TextDecorations>
Click here
</Hyperlink>
</TextBlock>
I have done as above:
<TextBlock>
<Hyperlink NavigateUri="{Binding Link}">
<Hyperlink.Resources>
<Style TargetType="{x:Type Hyperlink}">
<Style.Triggers>
<Trigger Property="Hyperlink.IsMouseOver" Value="True">
<Setter Property="Hyperlink.Foreground" Value="Gray"></Setter>
</Trigger>
</Style.Triggers>
</Style>
</Hyperlink.Resources>
<Hyperlink.TextDecorations>
<TextDecoration
PenThicknessUnit="FontRecommended">
<TextDecoration.Pen>
<Pen Brush="Black" Thickness="1" />
</TextDecoration.Pen>
</TextDecoration>
</Hyperlink.TextDecorations>
<TextBlock Text="{Binding Link}" FontSize="11" Foreground="DarkGray" TextWrapping="NoWrap" TextTrimming="CharacterEllipsis"/>
</Hyperlink>
</TextBlock>But have no rollover effect... What might be wrong?
- remove this Foreground="DarkGray"


