WPF —— ComboBox控件详解

1 ConboBox简介

表示带有下拉列表的选择控件,通过单击控件上的箭头可显示或隐藏下拉列表。

ComboBox允许用户从下拉列表中选择项,或根据需要在控件的文本框中输入新文本。

ComboBox 是 ItemsControl ,这意味着它可以包含任何类型的对象的集合 (例如字符串、图像或面板) 。

ItemsSource="{Binding}" 使用dataContext进行数据绑定时候 ,需要加上 也就是第二中添加下拉项的写法

关于ComboBox的实例

cs 复制代码
    <Grid.Resources>
        
    </Grid.Resources>
    <ComboBox Name="c1"
              Text="学校班级" 
              IsEditable="True"
              IsReadOnly="true"
              Width="120"
              Height="40"
              VerticalAlignment="Top"
              >
              <!--ItemsSource="{Binding}" 使用dataContext进行数据绑定时候 ,需要加上 也就是第二中添加下拉项的写法-->
        
    </ComboBox>
    <Button Click="Button_Click"
            Width="100"
            Height="40"
            Content="点击"></Button>
</Grid>

// 窗口加载完毕时候触发

// 大部分加ed都是过去式,代表...完成了,loaded:加载完成

// 大部分加s或者加es都是负数一般可以找一个数组结构进行赋值

// 大部分加un的 一般都是否定意思,unchecked:未选中

添加属性的三种方法

1 shiyongitems属性进行添加

this.c1.Items.Add("贺钓帝");

this.c1.Items.Add("二脚踢");

this.c1.Items.Add("窜天猴");

2 使用datacontext进行绑定

cs 复制代码
 List<StudentClass> values = new List<StudentClass>();
 values.Add(new StudentClass()
 {
     StudentId = 1,
     ClassName = "软件1班"
 });
 values.Add(new StudentClass()
 {
     StudentId = 2,
     ClassName = "软件2班"
 });
 this.c1.DataContext = values; // 设置数据源
 this.c1.DisplayMemberPath = "ClassName"; // 设置展示成员
 this.c1.SelectedValuePath = "StudentId";// 获取SelectedValue值,获取的是SelectedValuePath的值

3 使用ItemSource进行绑定

cs 复制代码
    List<StudentClass> values = new List<StudentClass>();
    values.Add(new StudentClass()
    {
        StudentId = 1,
        ClassName = "软件1班"
    });
    values.Add(new StudentClass()
    {
        StudentId = 2,
        ClassName = "软件2班"
    });
    this.c1.ItemsSource = values; // 设置数据源
    this.c1.DisplayMemberPath = "ClassName"; // 设置展示成员
    this.c1.SelectedValuePath = "StudentId";// 获取SelectedValue值,获取的是SelectedValuePath的值
}
相关推荐
Poetinthedusk1 天前
WPF动画制作分享
wpf·动画
张人玉1 天前
WPF HTTPS 通信示例使用说明
数据库·网络协议·http·c#·wpf
张人玉1 天前
WPF HTTPS 通信示例代码分析笔记
笔记·https·wpf
廋到被风吹走1 天前
【Spring】ThreadLocal详解 线程隔离的魔法与陷阱
java·spring·wpf
熊猫钓鱼>_>1 天前
多智能体协作:构建下一代高智能应用的技术范式
人工智能·ai·去中心化·wpf·agent·多智能体·multiagent
要记得喝水1 天前
某公司WPF面试题(含答案和解析)--1
wpf
源之缘-专注WPF开发2 天前
全栈开发实战:WPF+FFmpeg+GIS,打造工业级雷达探测终端
ffmpeg·wpf·gis
张人玉2 天前
C# WPF 折线图制作(可以连接数据库)
数据库·c#·wpf·sugar
闲人编程2 天前
OpenTelemetry分布式追踪
分布式·wpf·trace·追踪·open telemetry·codecapsule
张人玉2 天前
C# WPF 折线图制作笔记(LiveCharts 库)
笔记·c#·wpf·折线图·linechart