翻譯|使用教程|編輯:龔雪|2023-08-02 11:20:45.117|閱讀 128 次
概述:本文主要介紹DevExpress WinForms的查找編輯器在v23.1中引入的多項選擇功能,歡迎立即下載組件體驗哦~
# 界面/圖表報表/文檔/IDE等千款熱門軟控件火熱銷售中 >>
在最新發布的v23.1版本中,DevExpress WinForms Lookup Editor(查找編輯器)引入了一個非常受歡迎的功能——多項選擇。在這種新的選擇模式下,Lookup Editor顯示一個帶有復選框的列,用戶可以使用鼠標或鍵盤選擇查找項。
DevExpress WinForms 擁有180+組件和UI庫,能為Windows Forms平臺創建具有影響力的業務解決方案。DevExpress WinForms能完美構建流暢、美觀且易于使用的應用程序,無論是Office風格的界面,還是分析處理大批量的業務數據,它都能輕松勝任!
DevExpress技術交流群8:523159565 歡迎一起進群討論
使用EditValueType屬性來啟用多項選擇,此屬性指定Lookup如何在其EditValue屬性中存儲所選值。可用的選項包括:
下面的示例演示如何激活多項選擇,將查找的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 } }; } }
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”數據字段。
如果將EditValueType屬性設置為LookUpEditValueType.ValueList,則可以將查找的EditValue屬性綁定到只讀集合類型屬性,這個操作是由 設置控制的(默認情況下是啟用的)。
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控件不支持此功能。
本站文章除注明轉載外,均為本站原創或翻譯。歡迎任何形式的轉載,但請務必注明出處、不得修改原文相關鏈接,如果存在內容上的異議請郵件反饋至chenjj@fc6vip.cn
文章轉載自:慧都網