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

本文关键字:组合 高性能 方法 最佳 绑定 DisplayMemberPath 性能 SelectedValuePath | 更新日期: 2023-09-27 18:15:13

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

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

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

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