界面控件DevExpress WinForms Lookup中文教程 - 如何实现多项目选择?

在最新发布的v23.1版本中,DevExpress WinForms Lookup Editor(查找编辑器)引入了一个非常受欢迎的功能——多项选择。在这种新的选择模式下,Lookup Editor显示一个带有复选框的列,用户可以使用鼠标或键盘选择查找项。

DevExpress WinForms 拥有180+组件和UI库,能为Windows Forms平台创建具有影响力的业务解决方案。DevExpress WinForms能完美构建流畅、美观且易于使用的应用程序,无论是Office风格的界面,还是分析处理大批量的业务数据,它都能轻松胜任!

获取DevExpress v23.1正式版下载(Q技术交流:523159565)

启用多项选择

使用EditValueType属性来启用多项选择,此属性指定Lookup如何在其EditValue属性中存储所选值。可用的选项包括:

  • LookUpEditValueType.ValueList —Lookup将选择的值存储为对象(List)。
  • LookUpEditValueType.CSVString — Lookup将选定的值存储为带有逗号分隔值的字符串。
  • 下面的示例演示如何激活多项选择,将查找的EditValue属性设置为一个带有id的列表,以便在应用程序启动时选择相应的产品。

    using System.Windows.Forms;
    using System.Collections.Generic;
    using System.ComponentModel.DataAnnotations;
    using DevExpress.XtraEditors;
    using DevExpress.XtraEditors.Repository;
    
    public partial class Form1 : XtraForm {
    public Form1() {
    InitializeComponent();
    lookUpEdit1.Properties.DataSource = ProductA.GetList();
    lookUpEdit1.Properties.DisplayMember = "Name";
    lookUpEdit1.Properties.ValueMember = "ID";
    lookUpEdit1.Properties.EditValueType = LookUpEditValueType.ValueList;
    lookUpEdit1.EditValue = new List() {0, 1, 4};
    }
    }
    public class ProductA {
    int id;
    public ProductA(int id) {
    this.id = id;
    }
    [Display(Order = -1)]
    public int ID {
    get { return id; }
    }
    [Display(ShortName = "Bakery & Desserts")]
    public string Name { get; set; }
    [DisplayFormat(DataFormatString = "c2")]
    public double Price { get; set; }
    public static List GetList() {
    return new List() {
    new ProductA(0){ Name = "Butter Cake", Price = 56.99 },
    new ProductA(1){ Name = "Chocolate Pie ", Price = 89.99 },
    new ProductA(2){ Name = "Frozen Cookie Dough", Price = 54.99 },
    new ProductA(3){ Name = "Truffie Cake", Price = 59.99 },
    new ProductA(4){ Name = "Original Apple Pie", Price = 59.99 }
    };
    }
    }
    

    界面控件DevExpress WinForms Lookup中文教程 - 如何实现多项目选择?_第1张图片

    将选定状态绑定到数据

    Lookup Editor(查找编辑器)可以保存有关数据源字段中所选项的信息,使用CheckBoxSelectorName属性指定包含所选项信息的字段名。如果此字段包含非布尔值类型,您还应该处理以下事件来根据需要 "convert" 字段值:

    • QueryCheckBoxSelectorMemberIsSelected
    • SetCheckBoxSelectorMemberValue
    如何绑定到布尔数据字段

    下面的示例演示了如何将选择绑定到“Selected”字段(带有布尔值):

    public partial class Form1 : XtraForm {
    public Form1() {
    InitializeComponent();
    lookUpEdit2.Properties.DataSource = ProductB.GetList();
    lookUpEdit2.Properties.DisplayMember = "Name";
    lookUpEdit2.Properties.ValueMember = "ID";
    lookUpEdit2.Properties.CheckBoxSelectorMember = "Selected";
    lookUpEdit2.Properties.EditValueType = LookUpEditValueType.ValueList;
    lookUpEdit2.EditValue = new List() {0, 1};
    }
    }
    public class ProductB {
    int id;
    public ProductB(int id) {
    this.id = id;
    }
    [Display(Order = -1)]
    public int ID {
    get { return id; }
    }
    [Display(ShortName = "Bakery & Desserts")]
    public string Name { get; set; }
    [DisplayFormat(DataFormatString = "c2")]
    public double Price { get; set; }
    public int InStock { get; set; }
    [Display(ShortName = "Add to Order")]
    public bool Selected { get; set; }
    public static List GetProductList() {
    return new List() {
    new ProductB(0){ Name = "Butter Cake", Price = 56.99, InStock = 50 },
    new ProductB(1){ Name = "Chocolate Pie ", Price = 89.99, InStock = 32 },
    new ProductB(2){ Name = "Frozen Cookie Dough", Price = 54.99, InStock = 0 },
    new ProductB(3){ Name = "Truffie Cake", Price = 59.99, InStock = 42 },
    new ProductB(4){ Name = "Original Apple Pie", Price = 59.99, InStock = 0}
    };
    }
    }
    

    从下面的截图中可以看到,lookup创建了一个选择器列(Add to Order),并将其绑定到“Selected”数据字段。

    界面控件DevExpress WinForms Lookup中文教程 - 如何实现多项目选择?_第2张图片

    如何绑定到集合属性

    如果将EditValueType属性设置为LookUpEditValueType.ValueList,则可以将查找的EditValue属性绑定到只读集合类型属性,这个操作是由EnableEditValueCollectionEditing 设置控制的(默认情况下是启用的)。

    界面控件DevExpress WinForms Lookup中文教程 - 如何实现多项目选择?_第3张图片

    using System.Windows.Forms;
    using System.Collections.Generic;
    using System.ComponentModel.DataAnnotations;
    using DevExpress.XtraEditors;
    using DevExpress.XtraEditors.Repository;
    
    public partial class Form1 : XtraForm {
    List orderItems;
    List orders;
    public Form1() {
    InitializeComponent();
    orders = new List(){
    new Order(10295),
    new Order(10296),
    new Order(10297)
    };
    teOrder.DataBindings.Add(new Binding("EditValue", orders, "ID"));
    dataNavigator1.DataSource = orders;
    
    orderItems = new List() {
    new Item(){ ID = 0, ItemName = "Butter Cake", Price = 56.99 },
    new Item(){ ID = 1, ItemName = "Chocolate Pie", Price = 89.99},
    new Item(){ ID = 2, ItemName = "Frozen Cookie Dough", Price = 54.99},
    new Item(){ ID = 3, ItemName = "Truffie Cake", Price = 59.99 },
    new Item(){ ID = 4, ItemName = "Original Apple Pie", Price = 59.99 }
    };
    lookupOrderItems.Properties.DataSource = orderItems;
    lookupOrderItems.Properties.DisplayMember = "ItemName";
    lookupOrderItems.Properties.ValueMember = "ID";
    lookupOrderItems.Properties.EditValueType = LookUpEditValueType.ValueList;
    lookupOrderItems.DataBindings.Add(new Binding("Editvalue", orders, "Items"));
    }
    }
    
    public class Order {
    int id;
    public Order(int id) {
    this.id = id;
    Items = new HashSet();
    }
    [Display(ShortName = "Order ID")]
    public int ID { get { return id; } }
    public HashSet Items { get; private set; }
    }
    public class Item {
    public int ID { get; set; }
    public string ItemName { get; set; }
    public double Price { get; set; }
    }
    有条件的选择

    使用此选项,您可以阻止用户根据特定条件选择项目,处理SelectionChanging事件并将e.Cancel参数设置为true来取消项目选择。

    using DevExpress.XtraEditors.Controls;
    
    public partial class Form1 : XtraForm {
    List products;
    public Form1() {
    InitializeComponent();
    //...
    lookUpEdit1.Properties.SelectionChanging += Properties_SelectionChanging;
    }
    private void Properties_SelectionChanging(object sender, PopupSelectionChangingEventArgs e) {
    e.Cancel = products[e.RecordIndex].InStock == 0;
    }
    }

    重要提示:WinForms LookUpEdit控件可以选择多个项目,GridLookUpEdit、TreeListLookUpEdit和SearchLookUpEdit控件不支持此功能。

    你可能感兴趣的:(devexpress,UI开发,界面控件,winfrom,c#)