从后面的代码中添加comboBox项。 [WPF]

时间:2015-07-22 11:44:02

标签: c# wpf combobox

我从MSDN抓取了这段代码。我试图做的是类似的,但使用列表而不是三个不同的字符串。所以说

List<string> strList = new List<string>();
strList.Add("Created with C#");
strList.Add("Item 2");
strList.Add("Item 3");

  //MSDN CODE BELOW
    cbox = new ComboBox();
        cbox.Background = Brushes.LightBlue;
        cboxitem = new ComboBoxItem();
        cboxitem.Content = "Created with C#";
        cbox.Items.Add(cboxitem);
        cboxitem2 = new ComboBoxItem();
        cboxitem2.Content = "Item 2";
        cbox.Items.Add(cboxitem2);
        cboxitem3 = new ComboBoxItem();
        cboxitem3.Content = "Item 3";
        cbox.Items.Add(cboxitem3);

        cv2.Children.Add(cbox);

试图做cbox.Items.Add(strList);还尝试了一个forloop循环遍历每个元素,但这也不起作用。我有什么想法可以达到这个目的吗?

enter image description here

XAML:

          <Grid x:Name="grid44" DataContext="{StaticResource tBLPERMITSViewSource}" HorizontalAlignment="Left" VerticalAlignment="Top" Width="409">
                            <Grid.ColumnDefinitions>
                                <ColumnDefinition Width="Auto"/>
                                <ColumnDefinition Width="Auto"/>
                            </Grid.ColumnDefinitions>
                            <Grid.RowDefinitions>
                                <RowDefinition Height="Auto"/>
                            </Grid.RowDefinitions>
                            <Label Content="SPR PACKET ASSIGMENT" Grid.Column="0" HorizontalAlignment="Left" Margin="3" Grid.Row="0" VerticalAlignment="Center" FontWeight="Bold"/>
                            <ComboBox x:Name="sPR_ASSIGNEDComboBox" Grid.Column="1" DisplayMemberPath="SPR_ASSIGNED" HorizontalAlignment="Left" Height="Auto" Text="{Binding SPR_ASSIGNED}" ItemsSource="{Binding Items}" Margin="3,5,-114.35,5" Grid.Row="0" VerticalAlignment="Center" Width="238.35" Background="White" IsReadOnly="True" IsEditable="True" >

                            </ComboBox>
                        </Grid>

4 个答案:

答案 0 :(得分:5)

以编程方式设置项目:

代码隐藏:

    private void PopulateComboBox()
    {
        cBox.ItemsSource = new List<string> { "Item1", "Item2", "Item3"};
    }

XAML:

<ComboBox Width="200" Height="30"  x:Name="cBox" />

绑定到一系列项目:

    public class DummyClass
    {
        public int Value { get; set; }
        public string DisplayValue { get; set;}
    }

    public ObservableCollection<DummyClass> DummyClassCollection
    {
        get
        {
            return new ObservableCollection<DummyClass>
            {
                new DummyClass{DisplayValue = "Item1", Value = 1},
                new DummyClass{DisplayValue = "Item2", Value = 2},
                new DummyClass{DisplayValue = "Item3", Value = 3},
                new DummyClass{DisplayValue = "Item4", Value = 4},
            };
        }
    }

XAML:

<ComboBox Width="200" Height="30"  x:Name="cBox" ItemsSource="{Binding DummyClassCollection}" DisplayMemberPath="DisplayValue" />

答案 1 :(得分:1)

如果您不想按照绑定/ mvvm模式执行此操作,只需执行以下操作就很简单:

Dog

稍后可以访问以下内容:

foreach (var item in items)
{
    _comboBox.Items.Add(item);
    _comboBox.SelectedValuePath = "ID";
    _comboBox.DisplayMemberPath = "Name";
}

答案 2 :(得分:0)

您可以使用数据绑定。数据绑定允许您将列表中的动态数据绑定到组合框,并生成并填充您正在传递的内容。

Binding WPF Combobox to a Custom List

答案 3 :(得分:0)

我通过将字典 Ienumerable Keys 与 ComboBox ItemsSource 属性同步,然后在添加或删除项目时创建 Items 属性的 Refresh() 解决了从代码背后添加和删除 ComboBox 项目的问题:

Dictionary<string,Object> dico=new Dictionary<string,Object>();
dico.Add("obj1",obj1);
dico.Add("obj2",obj2);
dico.Add("obj3",obj3);
...
ComboBox combobox=new ComboBox();
combobox.ItemsSource=dico.Keys;

string key=a_key;
Object obj=an_object;
// add //
if (dico.ContainsKey(key)==false)
{
    dico.Add(key,obj);
    combobox.Items.Refresh();
    combobox.SelectedItem=key;
}
// remove //
if (dico.ContainsKey(key)==true)
{
    dico.Remove(key);
    combobox.Items.Refresh();
}