C# ComboBox下拉框运用实例

  • Post author:
  • Post category:其他


  • 在Form窗体添加一个ComboBox控件
 <ComboBox x:Name="Process" HorizontalAlignment="Left" DisplayMemberPath="Value" SelectedValuePath="ID" SelectedIndex="0" Text="{Binding ComPara1}" Margin="103,8,0,0" VerticalAlignment="Top" Width="104" Height="30" FontSize="14" Grid.Column="1"/>

可以在全局变量中定义下拉框的值

public static string[] ComboBoxValue = { "A", "B", "C", "D"};

在Form的CS文件中写一个将定义好的值添加到ComboBox下拉框中的方法,并在Form 加载Load函数中引用

 private void LoadProcessCombox()
        {
            List<ComPara> ProcessComboxList = new List<ComPara>();
            for (int i = 0; i < SysC.SysFunction.ComboBoxValue.Count(); i++)
            {
                ProcessComboxList.Add(new ComPara { ID = (i + 1).ToString(), Value = SysC.SysFunction.ComboBoxValue[i] });//循环添加、注意ComPara以键值对的结构存放数据
            }

            Process.ItemsSource = ProcessComboxList;//完成传值

        }
 private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            LoadProcessCombox();
       
            Time.Text = DateTime.Now.ToString("yyyy-MM-dd");
        }

将选中的值传递到后台

 string SearchText = "";
               
                if (Process.SelectedIndex != -1) //选中不为空判断
                {
                    SearchText = string.Format(" E_No = {0} ", Process.SelectedValue.ToString());
                }
  • 根据需要在SQL 语句中将SearchText作为筛选条件 即可完成对选中数据的操作

     string Startsql = string.Format(@" select E_NO,E_Value 
                                     FROM TableName
                                     where 2>1  {0} ", SearchText);



版权声明:本文为qq_53821027原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。