WPF之列表控件(ListBox、ComboBox)

WPF之列表控件

<Window x:Class="_06_05_列表控件.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:_06_05_列表控件"
        mc:Ignorable="d"
        Title="MainWindow" Height="550" Width="400">
    <Grid Margin="10">
        <StackPanel Orientation="Vertical">
            <ListBox Height="200" SelectionChanged="ListBox_SelectionChanged">
                <ListBoxItem>
                    <TextBlock Text="1"/>
                </ListBoxItem>
                <ListBoxItem>
                    <Label Content="2"/>
                </ListBoxItem>
                <ListBoxItem>
                    <Button Content="3"/>
                </ListBoxItem>
                <ListBoxItem>
                    <StackPanel Orientation="Horizontal">
                        <Label Content="xxxx : "/>
                    </StackPanel>
                </ListBoxItem>
                <CheckBox Content="Is A"></CheckBox>
            </ListBox>
            <ListBox Height="200" x:Name="listBox"> </ListBox>
            <ComboBox SelectedIndex="0" IsEditable="True" DropDownOpened="ComboBox_DropDownOpened">
                <Label>123</Label>
                <Label>456</Label>
                <Label>789</Label>
            </ComboBox>
        </StackPanel>
    </Grid>
</Window>

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace _06_05_列表控件
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();


            //初始化数据
            List<string> list = new List<string>();
            for(int i = 0; i < 10; i++)
            {
                list.Add($"ListBoxItem:{i}");
            }

            //将list内容加到listbox
            //listBox.ItemsSource = list;
            for (int i = 0; i < list.Count; i++)
            {
                listBox.Items.Add(list[i]); //传统的操作
            }

            listBox.Items.RemoveAt(0);

        }

        private void ListBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            ListBox listBox = (ListBox)sender;
            object obj = listBox.SelectedItem;
            if(obj is ListBoxItem)
            {
                ListBoxItem item = (ListBoxItem)obj;
                object content= item.Content;
            }
        }

        private void ComboBox_DropDownOpened(object sender, EventArgs e)
        {
            //当Combox被展开的时候,触发,某些时候比较有用,比如刷新操作
        }
    }
}

在这里插入图片描述

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值