WPF数据绑定组合框到列表< string> [英] WPF Databinding combobox to a list<string>

查看:112
本文介绍了WPF数据绑定组合框到列表< string>的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

 公开我的财产是一个困难的时间,试图通过XAML绑定我的属性类型列表到我的组合框。列表< string> MyProperty {get;组; 

以下XAML绑定不起作用:

 < ComboBox Name =cboDomainNamesItemsSource ={Binding Path = MyProperty}/> 

但以下作业:

  cboDomainNames.ItemsSource = MyProperty; 

功能完美。我在这里错过什么?

解决方案

发表我的评论回来标记答案。



我的DataContext被设置,但是它被设置在InitializeComponent()之后。我以为这可能是问题。然后我意识到,当我通过xaml绑定时,当加载视图时,绑定就会发生在空的属性上。



当视图准备就绪时,属性被填充之后加载(即在_presenter.OnViewReady())。由于它不是可观察的集合,所以没有添加到组合框中。从我的代码中指定它可以工作,因为当时的数据存在于属性中。


I am having a difficult time trying to bind my property which is of type List to my combobox through XAML.

public List<string> MyProperty  { get; set; }

The following XAML binding does not work:

<ComboBox Name="cboDomainNames" ItemsSource="{Binding Path=MyProperty}"/> 

But the following assignment:

cboDomainNames.ItemsSource = MyProperty;

works perfectly. What i am missing here?

解决方案

Posting my comment back to mark the answer.

My DataContext was set, BUT it was set after InitializeComponent(). I thought that could be the problem. Then I realized that as I am binding through xaml, when the view loads, the binding happens to the property which is empty.

The property gets populated when the view is ready after its loaded (i.e on _presenter.OnViewReady()). Since it's not an observable collection nothing gets added to the combobox. Specifying it from my code behind works, because at that time the data exists in the property.

这篇关于WPF数据绑定组合框到列表&lt; string&gt;的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆