Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. WPF
  4. ListBoxItem DataTemplate with HyperLink Problem

ListBoxItem DataTemplate with HyperLink Problem

Scheduled Pinned Locked Moved WPF
wpfquestionwcfhelp
3 Posts 2 Posters 9 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • K Offline
    K Offline
    Kevin Marois
    wrote on last edited by
    #1

    I have a listbox bound to an entity called NavigationItemEntity which displays hyperlinks as the list items: XAML

    <ListBox x:Name="itemsList"
    ItemsSource="{Binding Items, RelativeSource={RelativeSource TemplatedParent}}"
    ScrollViewer.HorizontalScrollBarVisibility="Auto"
    ScrollViewer.VerticalScrollBarVisibility="Auto"
    Margin="2"
    BorderBrush="Transparent"
    BorderThickness="0">

    <ListBox.ItemTemplate>
        <DataTemplate>
    
            <TextBlock>
    
                <local:MaroisHyperlink LinkText="{Binding Caption}">
                                                    
                    <i:Interaction.Triggers>
                        <i:EventTrigger EventName="LinkClicked">
                            <i:InvokeCommandAction Command="{Binding RelativeSource={RelativeSource FindAncestor, 
                                                                        AncestorType={x:Type local:MaroisNavigationPane}}, 
                                                                        Path=ItemClickedCommand}"
                                                    CommandParameter="{Binding Path=SelectedItem,ElementName=itemsList}"/>
                        </i:EventTrigger>
                    </i:Interaction.Triggers>
                                                    
                </local:MaroisHyperlink>
                                                
    
        </TextBlock>
    
        </DataTemplate>
    </ListBox.ItemTemplate>
    

    </ListBox>

    Code behind

    private ICommand _ItemClickedCommand;
    public ICommand ItemClickedCommand
    {
    get
    {
    if (_ItemClickedCommand == null)
    _ItemClickedCommand = new RelayCommand<NavigationEntity>(p => ItemClickedExecuted(p), p => ItemClickedCanExecute(p));
    return _ItemClickedCommand;
    }
    }

    private bool ItemClickedCanExecute(NavigationEntity args)
    {
    return true;
    }

    private void ItemClickedExecuted(NavigationEntity args)
    {
    RaiseItemClickedEvent(args.Id, args.ItemType);
    }

    The problem is that if I first don't click the list item, but just the link, then the link is sent as the command parameter. If I first click the list item, THEN click the link, then entity is passed, which is what I want. How can I force the list item to be selected when its link is clicked?

    Richard DeemingR 1 Reply Last reply
    0
    • K Kevin Marois

      I have a listbox bound to an entity called NavigationItemEntity which displays hyperlinks as the list items: XAML

      <ListBox x:Name="itemsList"
      ItemsSource="{Binding Items, RelativeSource={RelativeSource TemplatedParent}}"
      ScrollViewer.HorizontalScrollBarVisibility="Auto"
      ScrollViewer.VerticalScrollBarVisibility="Auto"
      Margin="2"
      BorderBrush="Transparent"
      BorderThickness="0">

      <ListBox.ItemTemplate>
          <DataTemplate>
      
              <TextBlock>
      
                  <local:MaroisHyperlink LinkText="{Binding Caption}">
                                                      
                      <i:Interaction.Triggers>
                          <i:EventTrigger EventName="LinkClicked">
                              <i:InvokeCommandAction Command="{Binding RelativeSource={RelativeSource FindAncestor, 
                                                                          AncestorType={x:Type local:MaroisNavigationPane}}, 
                                                                          Path=ItemClickedCommand}"
                                                      CommandParameter="{Binding Path=SelectedItem,ElementName=itemsList}"/>
                          </i:EventTrigger>
                      </i:Interaction.Triggers>
                                                      
                  </local:MaroisHyperlink>
                                                  
      
          </TextBlock>
      
          </DataTemplate>
      </ListBox.ItemTemplate>
      

      </ListBox>

      Code behind

      private ICommand _ItemClickedCommand;
      public ICommand ItemClickedCommand
      {
      get
      {
      if (_ItemClickedCommand == null)
      _ItemClickedCommand = new RelayCommand<NavigationEntity>(p => ItemClickedExecuted(p), p => ItemClickedCanExecute(p));
      return _ItemClickedCommand;
      }
      }

      private bool ItemClickedCanExecute(NavigationEntity args)
      {
      return true;
      }

      private void ItemClickedExecuted(NavigationEntity args)
      {
      RaiseItemClickedEvent(args.Id, args.ItemType);
      }

      The problem is that if I first don't click the list item, but just the link, then the link is sent as the command parameter. If I first click the list item, THEN click the link, then entity is passed, which is what I want. How can I force the list item to be selected when its link is clicked?

      Richard DeemingR Offline
      Richard DeemingR Offline
      Richard Deeming
      wrote on last edited by
      #2

      When you click the link, it doesn't select the item first, so the SelectedItem won't be what you're expecting. Try using:

      CommandParameter="{Binding}"


      "These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer

      "These people looked deep within my soul and assigned me a number based on the order in which I joined" - Homer

      K 1 Reply Last reply
      0
      • Richard DeemingR Richard Deeming

        When you click the link, it doesn't select the item first, so the SelectedItem won't be what you're expecting. Try using:

        CommandParameter="{Binding}"


        "These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer

        K Offline
        K Offline
        Kevin Marois
        wrote on last edited by
        #3

        That did it. Thanks

        If it's not broken, fix it until it is. Everything makes sense in someone's mind. Ya can't fix stupid.

        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups