Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4556556
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T17:23:01+00:00 2026-05-21T17:23:01+00:00

I need to find the ComboBox that a ComboBoxItem resides in. In codebehind I

  • 0

I need to find the ComboBox that a ComboBoxItem resides in.

In codebehind I catch an event when a ComboBoxItem is clicked, but I don’t know which one of several ComboBoxes that the specific ComboBoxItem belongs to. How do I find the ComboBox?

Normally you can use LogicalTreeHelper.GetParent() and traverse up the logical tree from the ComboBoxItem to find the ComboBox. But this only works if the ComboBoxItems are added to the ComboBox manually, not when the items are applied to the ComboBox with databinding. When using databinding, the ComboBoxItems do not have the ComboBox as a logical parent (I don’t understand why).

Any ideas?

More info:

Below is some code reconstructing my problem (not my actual code). If I would change from databinding the ComboBoxItems to setting them manually (in the XAML), the variable “comboBox” would be set to the correct ComboBox. Now comboBox is only null.

XAML:

<ComboBox Name="MyComboBox" ItemsSource="{Binding Path=ComboBoxItems, Mode=OneTime}" />

CodeBehind:

public MainWindow()
{
    InitializeComponent();

    MyComboBox.DataContext = this;
    this.PreviewMouseDown += MainWindow_MouseDown;
}

public BindingList<string> ComboBoxItems
{
    get
    {
        BindingList<string> items = new BindingList<string>();
        items.Add("Item E");
        items.Add("Item F");
        items.Add("Item G");
        items.Add("Item H");
        return items;
    }
}

private void MainWindow_MouseDown(object sender, MouseButtonEventArgs e)
{
    DependencyObject clickedObject = e.OriginalSource as DependencyObject;
    ComboBoxItem comboBoxItem = FindVisualParent<ComboBoxItem>(clickedObject);
    if (comboBoxItem != null)
    {
        ComboBox comboBox = FindLogicalParent<ComboBox>(comboBoxItem);
    }
}

//Tries to find visual parent of the specified type.
private static T FindVisualParent<T>(DependencyObject childElement) where T : DependencyObject
{
    DependencyObject parent = VisualTreeHelper.GetParent(childElement);
    T parentAsT = parent as T;
    if (parent == null)
    {
        return null;
    }
    else if (parentAsT != null)
    {
        return parentAsT;
    }
    return FindVisualParent<T>(parent);
}

//Tries to find logical parent of the specified type.
private static T FindLogicalParent<T>(DependencyObject childElement) where T : DependencyObject
{
    DependencyObject parent = LogicalTreeHelper.GetParent(childElement);
    T parentAsT = parent as T;
    if (parent == null)
    {
        return null;
    }
    else if(parentAsT != null)
    {
        return parentAsT;
    }
    return FindLogicalParent<T>(parent);
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-21T17:23:01+00:00Added an answer on May 21, 2026 at 5:23 pm

    This is probably what you are looking for:

    var comboBox = ItemsControl.ItemsControlFromItemContainer(comboBoxItem) as ComboBox;
    

    I love how descriptive that method-name is.


    On a side-note, there are some other useful methods which can be found in the property ItemsControl.ItemContainerGenerator which let you get the container associated with the templated data and vice versa.

    On another side-note, you usually should not be using any of them and instead use data-binding actually.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

i know that have alot of question like this one, but i dont know
I need to find/create a library that can load hdr images in many formats
I need to find the encoding of all files that are placed in a
I'm trying to create a custom ComboBox that behaves like the one in here:
I need to write a custom WPF control that should look like a ComboBox
Need to find the timestamp for the first minute of the first day of
I need to find a way to spin off a thread from a static
I need to find a way to call a vb.net function in my aspx
I need to find an open source alternative to VSS where people will share
i need to find away to parse html and css layout to be able

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.