如何用代码动态添加Items到Listbox

        <!--ContentPanel - place additional content here-->

        <Grid x:Name="ContentGrid" Grid.Row="1">

            <ListBox Height="451" HorizontalAlignment="Left" Margin="9,28,0,0" Name="listBox1" VerticalAlignment="Top" Width="460" Background="#FFAAA6A6" SelectionChanged="ListBox1SelChg"></ListBox>

            <Button Content="Button" Height="72" HorizontalAlignment="Left" Margin="150,515,0,0" Name="button1" VerticalAlignment="Top" Width="160" Click="Button1Clk" />

        </Grid>

 

Add this to the .cs code behind file in the main page class;

 

        // Constructor

        public MainPage()

        {

            InitializeComponent();

            LoadListBox1();

        }

 

        Dictionary<string, CheckBox> ckBoxDictionary = new Dictionary<string, CheckBox>();

        void LoadListBox1()

        {

            for (int ctr = 0; ctr < 10; ctr++)

            {

                //stack panels

                StackPanel stk = new StackPanel();

                stk.Name = "stack" + ctr.ToString();

                stk.Orientation = System.Windows.Controls.Orientation.Horizontal;

                stk.HorizontalAlignment = System.Windows.HorizontalAlignment.Left;

 

                // text blocks

                TextBlock txtBlk = new TextBlock();

                txtBlk.Name = "txtBlk" + ctr.ToString();

                txtBlk.Text = "txtBlk " + ctr.ToString();

                txtBlk.FontSize = 30;

                txtBlk.VerticalAlignment = System.Windows.VerticalAlignment.Center;

 

                // checkboxes

                CheckBox ckBox = new CheckBox();

                ckBox.Name = "ckBox" + ctr.ToString();

                ckBox.VerticalAlignment = System.Windows.VerticalAlignment.Center;

                ckBox.IsChecked = true;

                ckBox.Visibility = System.Windows.Visibility.Collapsed;

                ckBoxDictionary.Add(ckBox.Name, ckBox); // save them

 

                //Add child elements to stack panel order important, last will be on right

                stk.Children.Add(txtBlk);  // index 0

                stk.Children.Add(ckBox);  // index 1

 

                //Add to the Listitem collection

                listBox1.Items.Add(stk);

 

            }

        }

 

        private void ListBox1SelChg(object sender, SelectionChangedEventArgs e)

        {

            StackPanel stackP = (StackPanel)listBox1.SelectedItem;

            CheckBox checkB = (CheckBox)stackP.Children.ElementAt(1);   // checkmark the one selected

            checkB.Visibility = System.Windows.Visibility.Visible;

        }

 

        private void Button1Clk(object sender, RoutedEventArgs e)

        {

            CheckBox checkB1 = (CheckBox)ckBoxDictionary.ElementAt(4).Value;    // checkmark using an index

            checkB1.Visibility = System.Windows.Visibility.Visible;

            CheckBox checkB2 = (CheckBox)ckBoxDictionary["ckBox3"]; // checkmark using the name

            checkB2.Visibility = System.Windows.Visibility.Visible;

        }

你可能感兴趣的:(listbox)