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的值
}
相关推荐
hqwest19 小时前
C#WPF实战出真汁06--【系统设置】--餐桌类型设置
c#·.net·wpf·布局·分页·命令·viewmodel
Vae_Mars21 小时前
WPF中使用InputBindings进行快捷键绑定
wpf
hqwest1 天前
C#WPF实战出真汁05--左侧导航
开发语言·c#·wpf·主界面·窗体设计·视图viewmodel
hqwest1 天前
C#WPF实战出真汁01--项目介绍
开发语言·c#·wpf
wuty0072 天前
WPF 实现支持动态调整高度的文本显示控件
wpf·scrollviewer·extentheight·自动高度控件·动态调整高度
范纹杉想快点毕业5 天前
C 语言主控开发与显控开发能力体系及技术栈详解,STM32、QT、嵌入式、边缘系统显示
stm32·单片机·tcp/ip·microsoft·fpga开发·51单片机·wpf
weixin_447103585 天前
WPF之绑定!
c#·wpf
DataIntel5 天前
wpf问题记录
wpf
蓝点lilac6 天前
C# WPF 内置解码器实现 GIF 动图控件
c#·.net·wpf·图像
@Jackasher6 天前
Redis如何实现一个分布式锁?
redis·分布式·wpf