WPF ComboBox SelectedItem未更新 [英] WPF ComboBox SelectedItem not Updating

查看:362
本文介绍了WPF ComboBox SelectedItem未更新的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用时遇到问题WPF ComboBox 。我的情况是,我有一个ComboBox显示一些值。我将 ContentControl 添加到ComboBox的 Items 属性。我已经将这些ContentControl的 Content 绑定到某个数据源,以便我可以动态地更改内容。问题是如果所选项目的内容更改ComboBox下拉更新中的项目,则ComboBox SelectionChange中的项目保持不变。

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?

推荐答案

我用不同的方式解决了这个问题。

ComboBox SelectedItem不是它显示的,而是你选择的。当您选择一个项目comboBox需要ComboBox显示它在SelectionBox的模板不在SelectedItem,s模板。如果你将进入ComboBox的VisualTree,你会看到它有一个ContentPresenter包含一个TextBlock和这个TextBlock分配了选定的项目的文本。

所以我做了,在SelectionChanged事件处理程序i使用VisualTreeHelper
在ContentPresenter中查找TextBlock,然后将此TextBlock的Text属性绑定到ContentControl(SelectedItem)的Content属性。

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).

在ComboBox的SelectionChanged evetn处理程序中,我写道:

In the SelectionChanged evetn handler of ComboBox I wrote:

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

,此方法为:

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

这篇关于WPF ComboBox SelectedItem未更新的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆