[英]How to refresh a binding of the itemssource of a combobox programmatically?
[英]How do you set binding of ComboBox ItemsSource in UserControl?
这是我的UserControl中的组合框:
<Combobox ItemsSource="{Binding ComboItemsProperty}" />
我努力了:
Binding bind = new Binding();
bind.Mode = BindingMode.OneWay;
bind.Source = this;
bind.Path = new PropertyPath("ComboItemsProperty");
this.SetBinding(ComboBox.ItemsSourceProperty, bind);
但是,这不起作用。 我觉得我做的bind.Source错了,但我不知道如何设置源头。 这段代码在我的UserControl.xaml.cs中。
您可以尝试(替换此)
Binding bind = new Binding();
bind.Mode = BindingMode.OneWay;
bind.Source = yourCollectionSourceOrClass; //<--Replace with your collection
bind.Path = new PropertyPath("ComboItemsProperty");
this.SetBinding(ComboBox.ItemsSourceProperty, bind);
您需要包含属性ComboItemsProperty的实例的设置上下文。 因此,应将其设置为“ this.DataContext”或其他包含您已定义的ItemSource属性的类对象实例,而不是“ this”。
尝试这个,
Binding bind = new Binding();
bind.Mode = BindingMode.OneWay;
bind.Source = this.DataContext;
bind.Path = new PropertyPath("ComboItemsProperty");
this.SetBinding( ComboBox. ItemsSource Property, bind);
(发布frm手机)
我尝试了很多方法来执行此操作,但是似乎没有任何效果。
相反,当保存.xaml文件时,我将序列化绑定。 这似乎工作得很好。 绑定将不再需要在代码中设置。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.