【问题标题】:How to refresh a binding of the itemssource of a combobox programmatically?如何以编程方式刷新组合框的 itemssource 的绑定?
【发布时间】:2010-11-11 08:57:32
【问题描述】:

我在 SO 上找到了一些关于这个问题的项目,但它们并不能让我满意。他们谈论 INotifyProperyChanged,但这对我没有帮助。

我有一个Combobox。 对于ItemsSource,我使用MultiBindingConverter 创建ICollectionViewICollectionView 绑定到 ItemsSource

GotFocus-事件中,这个绑定需要被刷新,所以转换器会再次被触发。

我该怎么做?

【问题讨论】:

    标签: c# wpf binding combobox


    【解决方案1】:

    如果您可以在后面的代码中访问您的 ICollectionView,您可能想尝试 Refresh 方法...

    希望这会有所帮助..

    【讨论】:

    • 这没有帮助。这会刷新 ICollectionView,但 ICollectionView 需要由 Converter 重新创建。所以转换器需要重新启动。
    【解决方案2】:

    好的,一位同事帮了我。

    这是解决方案:

    private void theComboBox_OnGotFocus(object sender, RoutedEventArgs e)
    {
        ComboBox theComboBox = sender as ComboBox;
    
        if (theComboBox != null)
        {
            MultiBindingExpression binding = BindingOperations.GetMultiBindingExpression(theComboBox, ComboBox.ItemsSourceProperty);
            if (binding != null)
            {
                binding.UpdateTarget();
            }
        }
    }
    

    【讨论】:

    • 每次我将绑定设为 null 时...我已经给出了 itemsource...
    • 我通过 ItemsSource 设置了我的设置,只需将其设置为 null 并返回到原始集合即可刷新,没有明显的负面影响。
    • 如果绑定为空,您可以尝试使用BindingOperations.GetBindingExpression
    猜你喜欢
    • 2012-12-03
    • 1970-01-01
    • 2011-12-16
    • 2010-10-22
    • 2016-02-25
    • 2014-09-05
    • 2022-10-04
    • 1970-01-01
    • 2015-07-02
    相关资源
    最近更新 更多