下面是我的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中。
发布于 2012-09-07 23:54:46
我已经尝试了很多方法来做到这一点,然而,似乎什么都不起作用。
相反,我将在保存.xaml文件时序列化绑定。这似乎工作得很完美。绑定将不再需要在代码中设置。
发布于 2012-09-07 02:29:31
你可以试试(替换这个)
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);发布于 2012-09-07 02:29:32
您需要设置包含属性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); (发布自手机版)
https://stackoverflow.com/questions/12306092
复制相似问题