Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

To refer to UserControls within a ListBox from another source, you can use the FindName method of the ListBoxItem class.

First, give each UserControl a unique name by setting the x:Name attribute. Then, you can iterate through the items in the ListBox and use the FindName method to locate the UserControl with the desired name. For example:

foreach (ListBoxItem listBoxItem in MyListBox.Items)
{
    MyUserControl userControl = (MyUserControl)listBoxItem.Content;
    if (userControl.Name == "MyControlName")
    {
        // do something with the UserControl
    }
}

Alternatively, you can bind the ListBox's ItemsSource property to a collection of objects that contain references to the UserControls. Then, you can access the UserControls directly from the objects in the collection.