如何用代码动态添加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;

        }

转载于:https://www.cnblogs.com/nio-nio/archive/2011/05/11/2043015.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值