.NET Framework Developer Center > .NET Development Forums > Windows Presentation Foundation (WPF) > Possible bug: Hyperlink in a ListBox does not work
Ask a questionAsk a question
 

AnswerPossible bug: Hyperlink in a ListBox does not work

  • Tuesday, October 03, 2006 9:23 PMmarkovuksanovic Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     

    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

  • Wednesday, October 04, 2006 2:09 PMlee dModeratorUsers MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     Answer

    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

  • Wednesday, October 04, 2006 7:56 AMmarkovuksanovic Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     

    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.

  • Wednesday, October 04, 2006 2:09 PMlee dModeratorUsers MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     Answer

    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());

    }

     

     

  • Wednesday, October 04, 2006 2:50 PMmarkovuksanovic Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     

    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...

  • Wednesday, October 04, 2006 2:53 PMmarkovuksanovic Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    One more question, why is this event handled in the listbox and not in the datatemplate (hyperlink), where it is defined?
  • Wednesday, October 04, 2006 2:57 PMlee dModeratorUsers MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    we can handle in one place as the events are bubbled up if not marked handled=true on the source that raised the event
  • Wednesday, October 04, 2006 3:35 PMlee dModeratorUsers MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     

    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>

     

     

  • Friday, October 06, 2006 1:33 PMmarkovuksanovic Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     

    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?

  • Friday, October 06, 2006 2:15 PMlee dModeratorUsers MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    remove this Foreground="DarkGray"