控件combox


一. combox显示

    首先combox有两个属性来存储数据:DisplayMember(显示成员),ValueMember(值成员) ,DisplayMember是我们在combox界面上看到的,ValueMember是隐藏的数据。一般来说我们只需要设置DisplayMember属性的值即可。
  1. 循环赋值 ,通过combox.Items.Add方法
  2. 绑定数据,给combox.DataSource绑定数据源
两者的区别在于:
循环赋值代码没有绑定数据代码简练;
循环赋值方法combox默认没有选择值即 selectedIndex=-1,而绑定数据方式则默认是第一个数据。

注意:combox.DataSource数据源不一定,只要是集合数据即可(该集合类型必须继承自IList或IListSource)

二. 取数据

1. SelectedValue,SelectedItem,SelectedText,Text区别
combox取得值类型很多,基本有以上几种,这里讲下他们的区别,及用法
SelectedValue:选中的数据值;即ValueMember属性中存储的值。
SelectedItem:combox当前选中项的值;(如果设置了displayMember,与valueMember的值,则会显示着两个的集合)
SelectedText:鼠标选中的combox中显示值
Text:当前combox的值;可用来取combox的值。

注意:前三个值都是当combox选中某个值才有效,如果combox没有选中任何一个值,但是在程序中取得这些值为null,使用会报异常错误。(未实例化)而Text则是为空("")。

三.实例

1. UI:

2. 说明
groupbox1:给combox赋值。分别采用循环,绑定数据库获取值赋给DataTable,绑定Dictionary值类型
groupbox2:取combox值,这里选用的第三个combox DT绑定。分别查看combox各个属性的值
注意:这里只写取这些属性值,是为了区分他们的不同

3. 代码:
  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
             
             
using MySql.Data.MySqlClient ;
private void Form1_Load ( object sender , EventArgs e )
{
//循环绑定
SetCmbWithCycle ();
//绑定数据库
SetCmbWithBindingDB ();
//绑定dictionary
SetCmbWithDT ();
}
/// <summary>
/// 动态获取值
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void timer1_Tick ( object sender , EventArgs e )
{
if ( cmbDT . SelectedIndex < 0 )
{
lblError . Text = "combox没有选择值!" ;
return ;
}
//获取combox值
GetComboxData ();
}
/// <summary>
/// 通过循环来赋值
/// </summary>
private void SetCmbWithCycle ()
{
DataTable dt = GetDbData ();
for ( int i = 0 ; i < dt . Rows . Count ; i ++)
{
cmbItem . Items . Add ( dt . Rows [ i ][ "Name" ]);
}
}
/// <summary>
/// 通过绑定方式来赋值
/// </summary>
private void SetCmbWithBindingDB ()
{
DataTable dt = GetDbData ();
cmbBind . DataSource = dt ;
cmbBind . DisplayMember = "Name" ;
}
/// <summary>
/// 通过dictiona绑定
/// </summary>
private void SetCmbWithDT ()
{
Dictionary < int , string > dt = new Dictionary < int , string >();
dt . Add ( 1 , "A" );
dt . Add ( 2 , "B" );
BindingSource bs = new BindingSource ();
bs . DataSource = dt ;
//Dictionary类型数据不可直接绑定到控件上
cmbDT . DataSource = bs ;
cmbDT . ValueMember = "Key" ;
cmbDT . DisplayMember = "Value" ;
}
/// <summary>
/// 获取combox的属性值
/// </summary>
private void GetComboxData ()
{
lblSelVal . Text = cmbDT . SelectedValue . ToString ();
lblSelItem . Text = cmbDT . SelectedItem . ToString ();
lblSelTxt . Text = cmbDT . SelectedText ;
lblTxt . Text = cmbDT . Text ;
}
/// <summary>
/// 数据库中获取数据
/// </summary>
/// <returns></returns>
private DataTable GetDbData ()
{
string connStr = @"Server=localhost;Database=stu;Uid=root;Pwd=123456" ;
MySqlConnection conn = new MySqlConnection ( connStr );
string sql = @"Select *from user" ;
if ( conn . State == ConnectionState . Open )
{
conn . Close ();
}
conn . Open ();
MySqlCommand cmb = new MySqlCommand ( sql , conn );
MySqlDataAdapter da = new MySqlDataAdapter ( cmb );
DataTable dt = new DataTable ();
da . Fill ( dt );
conn . Close ();
return dt ;
}
  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
您好,关于您的问题,可以通过以下步骤实现: 1. 在 XAML 中定义一个 ComboBox 控件和一个 ListBox 控件,并将 ComboBox 绑定到一个 List<string>。 2. 在 ListBox 中使用 DataTemplate 定义 CheckBox 的显示方式,并将其绑定到 ListBox 的 ItemsSource 属性。 3. 在 ComboBox 的 SelectionChanged 事件中,获取选中的项,并将其在 ListBox 中对应的 CheckBox 设置为不可选中。 具体实现可以参考以下代码: XAML 代码: <Window x:Class="WpfApp1.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Title="MainWindow" Height="450" Width="800"> <Grid> <ComboBox x:Name="comboBox" ItemsSource="{Binding Items}" SelectionChanged="ComboBox_SelectionChanged"/> <ListBox x:Name="listBox"> <ListBox.ItemTemplate> <DataTemplate> <CheckBox Content="{Binding}" IsChecked="{Binding IsSelected}"/> </DataTemplate> </ListBox.ItemTemplate> </ListBox> </Grid> </Window> C# 代码: public partial class MainWindow : Window { public List<string> Items { get; set; } public MainWindow() { InitializeComponent(); // 初始化 ComboBox 绑定的数据源 Items = new List<string> { "Item1", "Item2", "Item3", "Item4" }; DataContext = this; } private void ComboBox_SelectionChanged(object sender, SelectionChangedEventArgs e) { // 获取选中的项 string selectedItem = comboBox.SelectedItem as string; // 将 ListBox 中对应的 CheckBox 设置为不可选中 foreach (var item in listBox.Items) { if (item.ToString() == selectedItem) { (item as CheckBox).IsEnabled = false; } else { (item as CheckBox).IsEnabled = true; } } } }

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值