0

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;     
        }
    }
Redaa
  • 1,540
  • 2
  • 17
  • 28

1 Answers1

1

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;
}`
Michael
  • 198
  • 10
  • @redaa Oh sorry I misunderstood, once you have the listviewitem you can just get the content and cast it. – Michael Aug 13 '14 at 15:08