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