C# 如何以编程方式刷新组合框的 itemssource 的绑定?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1137538/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 09:06:48  来源:igfitidea点击:

How to refresh a binding of the itemssource of a combobox programmatically?

c#wpfbindingcombobox

提问by Natrium

I found some items regarding this questions on SO, but they do not satisfy me. They talk about INotifyProperyChanged, but that does not help in my case.

我在 SO 上找到了一些关于这个问题的项目,但它们并不让我满意。他们谈论 INotifyProperyChanged,但这对我来说无济于事。

I have a Combobox. For the ItemsSource, I use a MultiBindingand a Converterto create an ICollectionView. The ICollectionViewgets bound to the ItemsSource.

我有一个Combobox. 对于ItemsSource,我使用 aMultiBinding和 aConverter创建一个ICollectionView. 在ICollectionView被绑定到ItemsSource

On the GotFocus-event, this binding needs to be refreshed, so the converter gets fired again.

GotFocus-event 上,需要刷新此绑定,因此转换器再次被触发。

How can I do this?

我怎样才能做到这一点?

采纳答案by Natrium

Ok, a collegue helped me out.

好的,一位同事帮我解决了。

This is the solution:

这是解决方案:

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

回答by Arcturus

If you can access your ICollectionView in your code behind, you might want to try the Refreshmethod...

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

Hope this helps..

希望这可以帮助..