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的值
}
相关推荐
玉面小君1 天前
从 WPF 到 Avalonia 的迁移系列实战篇6:Trigger、MultiTrigger、DataTrigger 的迁移
wpf·avalonia
招风的黑耳2 天前
Java生态圈核心组件深度解析:Spring技术栈与分布式系统实战
java·spring·wpf
lfw20192 天前
WPF 数据绑定模式详解(TwoWay、OneWay、OneTime、OneWayToSource、Default)
wpf
Magnum Lehar2 天前
3d wpf游戏引擎的导入文件功能c++的.h实现
3d·游戏引擎·wpf
FuckPatience3 天前
WPF Telerik.Windows.Controls.Data.PropertyGrid 自定义属性编辑器
wpf
almighty273 天前
C#WPF控制USB摄像头参数:曝光、白平衡等高级设置完全指南
开发语言·c#·wpf·usb相机·参数设置
军训猫猫头4 天前
12.NModbus4在C#上的部署与使用 C#例子 WPF例子
开发语言·c#·wpf
我要打打代码4 天前
在WPF项目中使用阿里图标库iconfont
wpf
拾忆,想起5 天前
Redisson 分布式锁的实现原理
java·开发语言·分布式·后端·性能优化·wpf
weixin_464078075 天前
wpf依赖注入驱动的 MVVM实现(含免费源代码demo)
wpf