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

Nat*_*ium 2 c# wpf binding combobox

我在SO上发现了一些关于这个问题的项目,但他们并不满足我.他们谈论INotifyProperyChanged,但这对我的情况没有帮助.

我有一个Combobox.对于ItemsSource,我使用a MultiBinding和a Converter来创建ICollectionView.在ICollectionView被绑定到ItemsSource.

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

我怎样才能做到这一点?

Nat*_*ium 8

好吧,一位同事帮助了我.

这是解决方案:

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();
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

  • 我通过ItemsSource进行了设置,只需将其设置为null并返回原始集合即可刷新,没有明显的负面副作用. (2认同)