WPF LookUpEdit控件无法填充数据问题求助
Hey there, let's get your LookUpEdit displaying data properly. I've spotted two key issues in your code that are preventing the control from populating correctly:
1. ItemsSource Needs a Collection (Not a Single Object)
The main problem is you're assigning a single Product instance to lookUpEdit1.ItemsSource, but LookUpEdit expects a collection type (like List<T>, ObservableCollection<T>, etc.) to display items—even if you only have one item to show. A single object can't be enumerated by the control, so it won't render any data.
2. (Optional) Use ObservableCollection for Dynamic Updates
If you plan to add/remove products later and want the UI to automatically reflect those changes, swap List<Product> with ObservableCollection<Product>. It implements INotifyCollectionChanged, which WPF uses to sync UI updates with data changes.
Corrected Backend Code
private void MainWindow_Loaded(object sender, RoutedEventArgs e) { // Assign a collection instead of a single Product instance lookUpEdit1.ItemsSource = new List<Product> { new Product("ad", "1", 5, 100), // Add more products here if needed new Product("Wireless Headphones", "2", 149.99, 35), new Product("Portable Charger", "3", 29.99, 80) }; // For dynamic updates (uncomment below): // lookUpEdit1.ItemsSource = new ObservableCollection<Product> // { // new Product("ad", "1", 5, 100) // }; } class Product { public string ProductName { get; set; } public string ID { get; set; } public double UnitPrice { get; set; } public int Quantity { get; set; } public Product(string productName, string id, double unitPrice, int quantity) { ProductName = productName; ID = id; UnitPrice = unitPrice; Quantity = quantity; } }
Quick Note on Your XAML
Your XAML setup is actually spot-on:
- The
PART_GridControlnaming is correct (DevExpress relies on this to link the GridControl to the LookUpEdit) DisplayMemberandValueMemberare properly mapped to yourProductproperties- Popup template columns are correctly bound to the
Productfields
After making this change, your LookUpEdit should display the product data in both the input field and the popup grid.
内容的提问来源于stack exchange,提问作者B.Dikmenn

