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的值
}
相关推荐
CoderIsArt5 分钟前
WPF的UI元素类型详解
ui·wpf
zxfgdjfjfjflfllf2 小时前
Mapreduce初使用
大数据·wpf·mapreduce
炯哈哈16 小时前
【上位机——WPF】Window标签常用属性
开发语言·c#·wpf·上位机
绿龙术士2 天前
构建现代化WPF应用:数据驱动开发与高级特性解析
c#·wpf
wangnaisheng4 天前
【WPF】Opacity 属性的使用
wpf
姬激薄4 天前
配置Hadoop集群-集群配置
wpf
python算法(魔法师版)4 天前
.NET 在鸿蒙系统上的适配现状
华为od·华为·华为云·.net·wpf·harmonyos
大道随心5 天前
【wpf】11 在WPF中实现父窗口蒙版效果:原理详解与进阶优化
wpf
zizisuo5 天前
9.1.领域驱动设计
wpf
大道随心5 天前
【wpf】10 C#树形控件高效实现:递归构建与路径查找优化详解
开发语言·c#·wpf