Add comboBox items from the code behind. [WPF]

I took this code from MSDN . What I'm trying to do is similar, but uses a list instead of three different strings. so tell me

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

      

Tried doing cbox.Items.Add (strList); Also tried looping a loop through each element, but that doesn't work either. Any ideas how I can achieve this?

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>

      

+3


source to share


2 answers


Install programmatically:

Code for:

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

      

XAML:

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

      



Bind to a set of elements:

    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" />

      

+5


source


You can use data binding. Data binding allows you to associate dynamic data from your list with a dropdown and create one and populate the content you go through.



Binding WPF Combobox to Custom List

0


source







All Articles