1
votes

I am facing a problem while working with a WPF ComboBox. My situation is that I have a ComboBox that is displaying some values. I am adding ContentControls to ComboBox' Items property. I have bound the Content of these ContentControl to some datasource so that I can change the Content dynamically. The problem is if the Content of item which is selected changes the item in ComboBox drop down updates but item in ComboBox SelectionChange remains same.

Any suggestions please?

3

3 Answers

1
votes

Instead of adding ContentControl directly inside the ComboBox, Use a DataTemplate(ItemsTemplate) or ItemContainerStyle. Because the automatically generated ComboBoxItem doesn't know your click because the ContentControl eats up the Mousedown and hide the ComboboxItem. ComboBox item is responsible to set the IsSelectedProperty and trigger SelectionChanged to happen.

1
votes

I have resolved this issue with a different way.
ComboBox SelectedItem is not what it displays but is what you select. When you select an Item comboBox takes the ComboBox displays it in SelectionBox's Template not in SelectedItem,s Template. If you will go into VisualTree of ComboBox, you will see that it has a ContentPresenter that contains a TextBlock and this TextBlock is assigned with the text of selected item.
So what i did, in SelectionChanged event handler i looked for the TextBlock inside that ContentPresenter using VisualTreeHelper and then i Binded the Text property of this TextBlock to the Content property of my ContentControl(SelectedItem).

Edit:

In the SelectionChanged evetn handler of ComboBox I wrote:

ModifyCombox(cmbAnimationBlocks, myComboBox.SelectedItem.As<ContentControl>());

and this method is:

    private static void ModifyCombox(DependencyObject comboBox, ContentControl obj)
    {
        if (VisualTreeHelper.GetChildrenCount(comboBox) > 0)
        {
            WalkThroughElement(comboBox, obj);
        }
    }

    private static void WalkThroughElement(DependencyObject element, ContentControl contentControl)
    {
        for (int i = 0; i < VisualTreeHelper.GetChildrenCount(element); i++)
        {
            if (element.GetType() == typeof(ContentPresenter))
            {
                ContentPresenter contentPresenter = element.As<ContentPresenter>();
                TextBlock textBlock = VisualTreeHelper.GetChild(contentPresenter, 0).As<TextBlock>();
                textBlock.SetBinding(TextBlock.TextProperty, new Binding("Content")
                {
                    Source = contentControl
                });
                contentPresenter.Content = textBlock;
            }
            else
            {
                DependencyObject child = VisualTreeHelper.GetChild(element, i).As<FrameworkElement>();
                WalkThroughElement(child, contentControl);
            }
        }

        if (VisualTreeHelper.GetChildrenCount(element) == 0 && element.GetType() == typeof(ContentPresenter))
        {
            ContentPresenter contentPresenter = element.As<ContentPresenter>();
            TextBlock textBlock = new TextBlock();
            textBlock.SetBinding(TextBlock.TextProperty, new Binding("Content")
            {
                Source = contentControl
            });
            contentPresenter.Content = textBlock;
        }
    }
0
votes
private void utc_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            var items = e.AddedItems;
            ComboBoxItem utcSel = (ComboBoxItem)items[0];
            string utcStr = utcSel.Content.ToString();
        }