彩票走势图

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

翻译|使用教程|编辑:龚雪|2023-08-02 11:20:45.117|阅读 51 次

概述:本文主要介绍DevExpress WinForms的查找编辑器在v23.1中引入的多项选择功能,欢迎立即下载组件体验哦~

# 慧都年终大促·界面/图表报表/文档/IDE等千款热门软控件火热促销中 >>

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

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

获取DevExpress v23.1正式版下载

DevExpress技术交流群8:523159565      欢迎一起进群讨论

启用多项选择

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

  • LookUpEditValueType.ValueList —Lookup将选择的值存储为对象(List<object>)。
  • 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<object>() {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<ProductA> GetList() {
return new List<ProductA>() {
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组件中文教程 - 如何实现多项目选择?
将选定状态绑定到数据

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

如何绑定到布尔数据字段

下面的示例演示了如何将选择绑定到“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<object>() {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<ProductB> GetProductList() {
return new List<ProductB>() {
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组件中文教程 - 如何实现多项目选择?
如何绑定到集合属性

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

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

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<Item> orderItems;
List<Order> orders;
public Form1() {
InitializeComponent();
orders = new List<Order>(){
new Order(10295),
new Order(10296),
new Order(10297)
};
teOrder.DataBindings.Add(new Binding("EditValue", orders, "ID"));
dataNavigator1.DataSource = orders;

orderItems = new List<Item>() {
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<int>();
}
[Display(ShortName = "Order ID")]
public int ID { get { return id; } }
public HashSet<int> 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<ProductB> 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企业定制服务

标签:

本站文章除注明转载外,均为本站原创或翻译。欢迎任何形式的转载,但请务必注明出处、不得修改原文相关链接,如果存在内容上的异议请邮件反馈至chenjj@capbkgr.cn

文章转载自:慧都网

为你推荐

  • 推荐视频
  • 推荐活动
  • 推荐产品
  • 推荐文章
  • 慧都慧问
扫码咨询


添加微信 立即咨询

电话咨询

客服热线
023-68661681

TOP