![](/img/trans.png)
[英]ComboBox binding with DisplayMemberPath and SelectedValuePath does not work as expected
[英]Performance : What is the best way to increase the performance of binding multiple comboboxes with same DisplayMemberPath and SelectedValuePath
我在WPF應用程序中有5個組合框。
下面是綁定所有組合框的代碼
private void BindYears()
{
cbBeginYear.ItemsSource = listOfYears;
cbBeginYear.DisplayMemberPath = "YearID";
cbBeginYear.SelectedValuePath = "YearID";
cbEnd_Year.ItemsSource = listOfYears;
cbEnd_Year.DisplayMemberPath = "YearID";
cbEnd_Year.SelectedValuePath = "YearID";
cbExactYear.ItemsSource = listOfYears;
cbExactYear.DisplayMemberPath = "YearID";
cbExactYear.SelectedValuePath = "YearID";
cbStart_Year.ItemsSource = listOfYears;
cbStart_Year.DisplayMemberPath = "YearID";
cbStart_Year.SelectedValuePath = "YearID";
cbEndYear.ItemsSource = listOfYears;
cbEndYear.DisplayMemberPath = "YearID";
cbEndYear.SelectedValuePath = "YearID";
}
我可以用這種方式做同樣的事情嗎,下面的這種方法將有助於提高任何一種性能。
private void BindYears()
{
cbEndYear.ItemsSource = cbStart_Year.ItemsSource = cbExactYear.ItemsSource = cbEnd_Year.ItemsSource = cbBeginYear.ItemsSource = listOfYears;
cbEndYear.DisplayMemberPath = cbStart_Year.DisplayMemberPath = cbExactYear.DisplayMemberPath = cbEnd_Year.DisplayMemberPath = cbBeginYear.DisplayMemberPath = "YearID";
cbEndYear.SelectedValuePath = cbStart_Year.SelectedValuePath = cbExactYear.SelectedValuePath = cbEnd_Year.SelectedValuePath = cbBeginYear.SelectedValuePath = "YearID";
}
根據您的評論,我可能會這樣:
private void BindYears(Combobox box)
{
box.ItemsSource = listOfYears;
box.DisplayMemberPath = "YearID";
box.SelectedValuePath = "YearID";
}
private void BindBoxes()
{
BindYears(cbBeginYear);
BindYears(cbEnd_Year);
BindYears(cbExactYear);
BindYears(cbStart_Year);
BindYears(cbEndYear);
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.