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
How to refresh a binding of the itemssource of a combobox programmatically?
提问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 MultiBinding
and a Converter
to create an ICollectionView
. The ICollectionView
gets 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();
}
}
}