简体   繁体   English

如何在WPF中有效地在组合框中添加项目

[英]How to Add Items in Combobox Efficiently in WPF

I am a C++ developer and have recently started learning WPF. 我是C ++开发人员,最近开始学习WPF。 I am working on a wpf app where I am using MVVM. 我正在使用MVVM的wpf应用程序上工作。 I have comboboxes and I need to add items in it. 我有组合框,需要在其中添加项目。 Although I generally use ComboboxPropertyName.Add("") to add items in it, I am looking for an efficient method which adds the items without much code length. 尽管我通常使用ComboboxPropertyName.Add(“”)在其中添加项目,但我正在寻找一种有效的方法来添加没有太多代码长度的项目。 Here is the code: 这是代码:

XAML: XAML:

<ComboBox Height="23" ItemsSource="{Binding BoardBoxList}" SelectedItem="{Binding SelectedBoardBoxList, Mode=TwoWay}" SelectedIndex="0" Name="comboBox2" />

ViewModel Class: ViewModel类:

public ObservableCollection<string> BoardBoxList
    {
        get { return _BoardBoxList; }
        set
        {
            _BoardBoxList = value;
            OnPropertyChanged("BoardBoxList");
        }
    }

    /// <summary>
    /// _SelectedBoardBoxList
    /// </summary>
    private string _SelectedBoardBoxList;
    public string SelectedBoardBoxList
    {
        get { return _SelectedBoardBoxList; }
        set
        {
            _SelectedBoardBoxList = value;
            OnPropertyChanged("SelectedBoardBoxList");
        }
    }

Here Is how I had added items in my combobox in C++: 这是我在C ++的组合框中添加项目的方式:

static const signed char boards[][9] = {
{},                                           // left blank to indicate no selection
{ 'S', '1', '0', '1', '0', '0', '1', '2', 0 },   // redhook
{ 'S', '1', '0', '1', '0', '0', '1', '8', 0 },   // bavaria
{ 'S', '1', '0', '1', '0', '0', '2', '0', 0 },   // flying dog
};

m_boardBox = new ComboBox(String::empty);
for(int i = 1; i < 4; i++)
    m_boardBox->addItem(String((char*)(boards[i])), i); 
m_boardBox->setSelectedId(2); // select Bavaria by default
addAndMakeVisible(m_boardBox);

If you notice above, you will find the loop adding items easily. 如果您在上面注意到,您会发现轻松添加项目的循环。 This is how i wanna add items to my combobox. 这就是我想向组合框添加项目的方式。

If I use _BoardBoxList.Add("...."); 如果我使用_BoardBoxList.Add("...."); I will have to use many .Adds. 我将不得不使用许多.Adds。 Is their an efficient way where I can store the items in a list/collection and add them into the combobox in the form of for loop just like above? 它们是一种有效的方式,可以像上面那样将项目存储在列表/集合中并将它们以for loop的形式添加到组合框中吗?

Please help :) 请帮忙 :)

You can use a constructor of ObservableCollection that can consume an enumerable as a start set. 您可以使用ObservableCollection的构造函数,该构造函数可以使用枚举作为开始集。

new ObservableCollection<string>(boards);

Boards would have to be an collection of strings not chars. 木板必须是字符串而不是字符的集合。

Edit: 编辑:

var boards = new[]{ "S1010012" ,   // redhook
                   "S1010018",   // bavaria
                   "S1010020"    // flying dog
                  } 

There should be nothing stopping you from using the very same loop in C# to add items to the observable collection, the ComboBox will update automatically. 没有什么可以阻止您在C#中使用相同的循环将项目添加到可观察的集合中, ComboBox将自动更新。 Unless you are more specific about your problem there is nothing else to say. 除非您对问题更具体,否则没有其他话可说。

The goal of the ItemsSource property is that you do not imperatively add items to the ComboBox itself via a loop but just specify a collection and the control will take care of the rest, how the items get into the collection if your business. ItemsSource属性的目标是您不必强制通过循环将项目添加到ComboBox本身,而只是指定一个集合,控件将负责其余的事务,以及如果您的业务项目如何进入集合。

If i understand you correctly you can use AddRange Method of the List class 如果我理解正确,则可以使用List类的AddRange方法

you can create list (add use AddRange - for adding, for loop, etc.), and than create Observable Collection based on that list. 您可以创建列表(使用AddRange添加-用于添加,循环等),然后根据该列表创建Observable Collection。

ObservableCollection<string> collection 
                 = new ObservableCollection<string>(myList);

And than you can bind observable to ItemSource , if you need it (updating), otherwise you can bind list. 然后,如果需要,您可以将ItemSource绑定到ItemSource (更新),否则可以绑定list。

Use the ComboBox 's ItemsSource property. 使用ComboBoxItemsSource属性。 If you use an ObservableCollection as the source, you can forget updating the items manually and just work with the list in the view model. 如果使用ObservableCollection作为源,则可以忘记手动更新项目,而只需要在视图模型中使用列表即可。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM