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

WPF ListBox

Scheduled Pinned Locked Moved WPF
csharpwpftutorialquestion
5 Posts 4 Posters 0 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.
  • M Offline
    M Offline
    michaelbarb
    wrote on last edited by
    #1

    ListBox is basically a list of ListBoxItem. How can you access this list in C#? You can access parts of ListBoxItem threw SelectedItem but no the full item. For example you cannot get to background color of an item with SelectedItem.

    So many years of programming I have forgotten more languages than I know.

    Richard DeemingR L 2 Replies Last reply
    0
    • M michaelbarb

      ListBox is basically a list of ListBoxItem. How can you access this list in C#? You can access parts of ListBoxItem threw SelectedItem but no the full item. For example you cannot get to background color of an item with SelectedItem.

      So many years of programming I have forgotten more languages than I know.

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

      You need to use the ItemContainerGenerator[^] to get the ListBoxItem for a specific index: How to: Get a ListBoxItem - WPF | Microsoft Docs[^] NB: This can return null if the item has not been shown yet. If you're trying to modify the style of the selected item, you should consider using declarative WPF styles instead. ItemsControl.ItemContainerStyle Property (System.Windows.Controls) | Microsoft Docs[^]


      "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

      M 1 Reply Last reply
      0
      • M michaelbarb

        ListBox is basically a list of ListBoxItem. How can you access this list in C#? You can access parts of ListBoxItem threw SelectedItem but no the full item. For example you cannot get to background color of an item with SelectedItem.

        So many years of programming I have forgotten more languages than I know.

        L Offline
        L Offline
        Lost User
        wrote on last edited by
        #3

        Depends on your "templating". The "contents" are the things you load (.Items). The item template, default or custom, deals with the visuals. The default "data" is via ToString(). [.net - Default ItemTemplate of Controls like ListBox/ListView - Stack Overflow](https://stackoverflow.com/questions/23848488/default-itemtemplate-of-controls-like-listbox-listview)

        It was only in wine that he laid down no limit for himself, but he did not allow himself to be confused by it. ― Confucian Analects: Rules of Confucius about his food

        1 Reply Last reply
        0
        • Richard DeemingR Richard Deeming

          You need to use the ItemContainerGenerator[^] to get the ListBoxItem for a specific index: How to: Get a ListBoxItem - WPF | Microsoft Docs[^] NB: This can return null if the item has not been shown yet. If you're trying to modify the style of the selected item, you should consider using declarative WPF styles instead. ItemsControl.ItemContainerStyle Property (System.Windows.Controls) | Microsoft Docs[^]


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

          M Offline
          M Offline
          michaelbarb
          wrote on last edited by
          #4

          This seems to be one way. It can get a ListBoxItem but not return it to the ListBox after it is edited. To be more exact about what I want to do. Given a ListBox and search key, I want to highlight or change the color of items that contain the search key. I do not want to edit the text in the list. I want to keep the list browsable and still contain all items. Change the search key: a)highlight different items in the ListBox b)clearing those that are no longer contain the search key.

          So many years of programming I have forgotten more languages than I know.

          M 1 Reply Last reply
          0
          • M michaelbarb

            This seems to be one way. It can get a ListBoxItem but not return it to the ListBox after it is edited. To be more exact about what I want to do. Given a ListBox and search key, I want to highlight or change the color of items that contain the search key. I do not want to edit the text in the list. I want to keep the list browsable and still contain all items. Change the search key: a)highlight different items in the ListBox b)clearing those that are no longer contain the search key.

            So many years of programming I have forgotten more languages than I know.

            M Offline
            M Offline
            Mycroft Holmes
            wrote on last edited by
            #5

            Try Add a property to your listbox data source - IsMatched (probably a string with the colour name) bind the back colour of your ListBoxItemTemplate to the IsMatched property of your data item During the search event change the IsMatched value Reset event should clear the IsMatched value back to the default, ptobably Transparent

            Never underestimate the power of human stupidity - RAH I'm old. I know stuff - JSOP

            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