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的值
}
相关推荐
Pasregret1 小时前
缓存与数据库一致性深度解析与解决方案
数据库·缓存·wpf
Java林间2 天前
Zookeeper是什么?基于zookeeper实现分布式锁
分布式·zookeeper·wpf
zizisuo2 天前
1.微服务拆分与通信模式
微服务·wpf
程序员秘密基地2 天前
基于c#,wpf,ef框架,sql server数据库,音乐播放器
sql·sqlserver·c#·.net·wpf
Zhen (Evan) Wang2 天前
.NET 6 WPF 利用CefSharp.Wpf.NETCore显示PDF文件
.net·wpf·.netcore
冰茶_3 天前
WPF特性分析
学习·microsoft·c#·wpf
qq_196055874 天前
最快打包WPF 应用程序
wpf
baivfhpwxf20235 天前
wpf ScaleTransform
wpf
C#_西哥5 天前
wpf stylet框架 关于View与viewmodel自动关联绑定的问题
wpf
wqq10275 天前
WPF 图标原地旋转
wpf