WPF ComboBox SelectedItem not Updating

£可爱£侵袭症+ 提交于 2019-12-04 05:19:20

问题


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?


回答1:


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.




回答2:


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;
        }
    }


来源:https://stackoverflow.com/questions/1605939/wpf-combobox-selecteditem-not-updating

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!