性能:使用相同的DisplayMemberPath和SelectedValuePath提高绑定多个组合框的性能的最佳方法是什么

时间:2014-07-28 04:22:17

标签: c# wpf performance

我在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";
}

1 个答案:

答案 0 :(得分:1)

根据你的评论,我可能会这样做:

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);
}