0
votes

i'm working on a Project where i need to display a ComboBox into a ListView, the ComboBox is Bound using TwoWay Mode. I need to fire an event whenever the Combobox selection changes and get the selected item of the selected ComboBox from the listview.

enter image description here

I need to select this item, whenever the combobox selection change event is fired, so i can get the selected item.

EDIT: this is the event code.

private void ProductTypeComboBox_OnSelectionChanged(object sender, SelectionChangedEventArgs e)
    {
        ComboBox combo = e.OriginalSource as ComboBox;
        ComboBoxItem cbItem = (ComboBoxItem) combo.SelectedItem;
        string selected = cbItem.Content.ToString();


        switch (selected)
        {
            case "Vente" :
                var pro = this.ProductsToAddListView.SelectedItem;

                break;

            default:

                MessageBox.Show("Error", "Error", MessageBoxButton.OK, MessageBoxImage.Error);
                break;     
        }
    }
1
Please add your code what you have done so far - Flat Eric
Could be a possible duplicate of stackoverflow.com/questions/2961118/… - Kulasangar
@Kulasangar this has nothing to do with my situation - Redaa

1 Answers

1
votes

What you want to do is go up through the combobox's ancestors until you find the one you want. The following function is a generalized version, what you want to do is use ListViewItem as the type T and your combobox as the parameter.

private static T FindUIElementParent<T>(UIElement element) where T : UIElement
{
    UIElement parent = element;
    while (parent != null)
    {
        T correctlyTyped = parent as T;
        if (correctlyTyped != null)
        {
            return correctlyTyped;
        }

        parent = VisualTreeHelper.GetParent(parent) as UIElement;
    }
    return null;
}`