Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

WPF ListBox - Getting UIElement instead of of SelectedItem

I created a ListBox that has a DataTemplate as Itemtemplate. However, is there an easy way to access the generated UIElement instead of the SelectedItem in codebehind?

When I access SelectedItem, I just get the selected object from my ItemsSource collection. Is there a way to access the UIElement (ie. the element generated from the DataTemplate together with the bound object)?

like image 401
Joachim Kerschbaumer Avatar asked Mar 04 '09 12:03

Joachim Kerschbaumer


People also ask

How to get selected item from WPF listbox?

Make a simple Listbox in your WPF form as below : SelectionChanged method will be called when the user changes and selects an item from our WPF Listbox. And for getting the selected item here is the code : I’m having a Countries class which I’m using to Bind my WPF Listbox from Database.

How to show selecteditem on the view using WPF textblock?

And the WPF ViewModel will be assign a String value to the WPF TextBlock to show the selectedItem on the View. ListBox ItemSource and SelectionChanged method is defined in the design code and a TextBlock is made to show the SelectedItem. Make two folders with name Model and ViewModel respectively.

What does the selectionmode property do in a list box?

Returns a collection of the currently selected items. The SelectionMode property is set to Single. The following example shows how to use the SelectedItems property to determine whether a list box has any selected items.

How to bind selecteditem to the original ViewModel?

To bind to the original data context (which is your ViewModel) you can write: UPDATE: regarding the SelectedItem property binding, it looks perfectly valid, I tried the same on my machine and it works fine. Here is my full test app:


2 Answers

You are looking for the ItemContainerGenerator property. Each ItemsSource has an ItemContainerGenerator instance. This class has the following method that might interest you: ContainerFromItem(object instance).

Once you have a handle to the ListBoxItem, you can go ahead and browse the logical and visual tree. Check out Logical Tree Helper and Visual Tree Helper.

Like Andy said in the comments, just because the item exists in your collection doesn't mean a container has been generated for it. Any kind of virtualizing panel scenario will raise this issue; UIElements will be reused across the different items. Be careful with that as well.

like image 188
Szymon Rozga Avatar answered Sep 23 '22 04:09

Szymon Rozga


siz, Andy and Bodeaker are absolutely right.

Here is how I was able to retrieve the textbox of the listbox's selected item using its handle.

var container = listboxSaveList.ItemContainerGenerator.ContainerFromItem(listboxSaveList.SelectedItem) as FrameworkElement;
if (container != null)
{
    ContentPresenter queueListBoxItemCP = VisualTreeWalker.FindVisualChild<ContentPresenter>(container);
    if (queueListBoxItemCP == null)
        return;

    DataTemplate dataTemplate = queueListBoxItemCP.ContentTemplate;

    TextBox tbxTitle = (TextBox)dataTemplate.FindName("tbxTitle", queueListBoxItemCP);
    tbxTitle.Focus();
}

(Note: Here, VisualTreeWalker is my own wrapper over VisualTreeHelper with various useful functions exposed)

like image 34
vamosrafa Avatar answered Sep 19 '22 04:09

vamosrafa