WPFComBox的操作方式获取的方式

1 数据绑定

csharp 复制代码
public class City 
    {
        public int ID { get; set; }
        public string Name { get; set; }
    }
 private void Window_Loaded(object sender, RoutedEventArgs e)
 {
        List<City> list = new List<City>();
        list.Add(new City { ID = 1, Name = "上海" });
        list.Add(new City { ID = 2, Name = "北京" });
        list.Add(new City { ID =3, Name = "天津" });
        cmb_list.ItemsSource = list;
           
 } 

前台写入绑定的属性(显示名称,绑定Id)

csharp 复制代码
 <ComboBox Name="cmb_list" Grid.Row="1" Grid.Column="1" Height="23"
                  DisplayMemberPath="Name" SelectedValuePath="ID" SelectionChanged="cmb1_SelectionChanged"></ComboBox> 
                        

另一种选法:

csharp 复制代码
List<string> countries = new List<string> { "USA", "UK", "Germany", "France", "Italy" };
            comboBox1.ItemsSource = countries;
            comboBox1.DisplayMemberPath = "Text";
            comboBox1.SelectedValuePath = "Value"; 

原文链接:https://blog.csdn.net/qq_35320456/article/details/137250525

获取的方法是:

WPF ComboBox控件有以下几个常用的属性:

ItemsSource:指定ComboBox控件的选项数据源,可以是任何集合,如List、ObservableCollection等。

DisplayMemberPath:指定显示在ComboBox中的数据成员路径,即列表中每个项目的显示文本。

SelectedValuePath:指定ComboBox控件中选定项目的数据成员路径,即列表中每个项目绑定的实际值。

IsEditable:指定ComboBox控件是否可编辑,如果设置为true,用户可以在下拉列表之外输入新值。

Watermark:指定ComboBox控件的提示文本,当没有选中任何选项时显示。

IsSelected:默认选中状态

2.手动指定 两中方法:

csharp 复制代码
<ComboBox Name="jiexianFangshi" Grid.Column="1" HorizontalAlignment="Left"  Height="30" Margin="3,16,0,0" Grid.Row="3" VerticalAlignment="Top" Width="235" Grid.ColumnSpan="2" FontSize="14">
                        <ComboBoxItem>裸线/瓷板</ComboBoxItem>
                        <ComboBoxItem IsSelected="True">两线制变送器</ComboBoxItem>
                        <ComboBoxItem>裸线/瓷板/端子</ComboBoxItem>
  </ComboBox>
相关推荐
humors22113 分钟前
Windows运维工具批处理版
运维·windows·计算机·电脑·笔记本·维护·台式机
hnlucky1 小时前
Windows 上安装下载并配置 Apache Maven
java·hadoop·windows·学习·maven·apache
洛阳泰山2 小时前
Windows系统部署MongoDB数据库图文教程
数据库·windows·mongodb
阿斯顿法国红酒快3 小时前
Windows系统安全加固
网络·windows·安全·网络安全·系统安全·ddos
C++ 老炮儿的技术栈4 小时前
自定义CString类与MFC CString类接口对比
c语言·c++·windows·qt·mfc
不讲废话的小白5 小时前
Windows系统永久暂停更新操作步骤
windows
正经教主7 小时前
【基础】Windows开发设置入门4:Windows、Python、Linux和Node.js包管理器的作用和区别(AI整理)
linux·windows·python·包管理器
明月与玄武7 小时前
Pywinauto:轻松实现Windows桌面自动化实战
windows·pywinauto·windows桌面自动化
渴望技术的猿8 小时前
Windows 本地部署MinerU详细教程
java·windows·python·mineru
Mast Sail1 天前
windows下authas调试tomcat
java·windows·tomcat·authas