首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在UserControl中设置ComboBox ItemsSource的绑定?

问如何在UserControl中设置ComboBox ItemsSource的绑定?
EN

Stack Overflow用户
提问于 2012-09-07 02:20:38
回答 3查看 3.4K关注 0票数 0

下面是我的UserControl中的组合框:

代码语言:javascript
复制
<Combobox ItemsSource="{Binding ComboItemsProperty}" />

我试过了:

代码语言:javascript
复制
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中。

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2012-09-07 23:54:46

我已经尝试了很多方法来做到这一点,然而,似乎什么都不起作用。

相反,我将在保存.xaml文件时序列化绑定。这似乎工作得很完美。绑定将不再需要在代码中设置。

票数 0
EN

Stack Overflow用户

发布于 2012-09-07 02:29:31

你可以试试(替换这个)

代码语言:javascript
复制
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);
票数 0
EN

Stack Overflow用户

发布于 2012-09-07 02:29:32

您需要设置包含属性ComboItemsProperty.的实例的上下文因此,您应该将其设置为“this.DataContext”或包含已定义的ItemSource属性的其他类对象实例,而不是“this”。

尝尝这个,

代码语言:javascript
复制
Binding bind = new Binding();  
bind.Mode = BindingMode.OneWay;  
bind.Source = this.DataContext;  
bind.Path = new PropertyPath("ComboItemsProperty");  
this.SetBinding( ComboBox. ItemsSource Property, bind);  

(发布自手机版)

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/12306092

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档