Ticket T983669
Visible to All Users

WPF ComboboxEdit changing ItemssSource when opened

created 4 years ago

I have a CombBoxEdit with data which can change a lot, that's why I am each time the popup is being loaded reload the data. Currently my code is:

XAML
<dxe:ComboBoxEdit ItemsSource="{Binding Suppliers}" ValueMember="Id" DisplayMember="Name" ValidateOnTextInput="False" AutoComplete="True" IncrementalFiltering="True" FilterCondition="Contains" ImmediatePopup="True" SelectedItem="{Binding Supplier,ValidatesOnDataErrors=True,UpdateSourceTrigger=PropertyChanged}" PopupOpening="ComboBoxEdit_PopupOpening"/>
C#
private void ComboBoxEdit_PopupOpening(object sender, DevExpress.Xpf.Editors.OpenPopupEventArgs e) { if (DataContext is LotEditorViewModel viewmodel && viewmodel.ReloadSuppliersCommand.CanExecute(null)) { viewmodel.ReloadSuppliersCommand.Execute(null); } }

I then wanted to test whether it rerenders the data as I want to be certain about it and did the following in my viewmodel.

C#
private async void OnReloadSuppliersCommand(object obj) { try { Suppliers = Enumerable.Empty<Supplier>(); await Task.Delay(250); Suppliers = await _supplierService.GetAllByLocation(locationId); } catch (Exception ex) { } }

The code of Suppliers in my viewmodel.

C#
private IEnumerable<Supplier> _suppliers; public IEnumerable<Supplier> Suppliers { get => _suppliers?.OrderBy((supplier) => supplier.Name); set { _suppliers = value; RaisePropertyChanged(nameof(Suppliers)); // Using Prism.MVVM } }

What's happening is my comboboxedit renders an empty list, while I do load the Suppliers a bit later.

Now I'm wondering how I'm able to make the ComboboxEdit detect itemssource changes and renders the newest items put in the variable?

Show previous comments (2)
DevExpress Support Team 4 years ago

    Hello Tom,

    The use of the PopupOpening event is a correct solution if you wish to refresh data every time the popup is opened. Regarding the issue you described, AutoSuggestEdit does not filter data on its own, so most likely an extra filter is applied in your code. Please check your logic to populate the ItemsSource collection.

    In the meantime, I tested your original code snippet and see that the editor's popup is never opened because Suppliers is set to an empty collection. If that's the issue you meant, you can set ShowPopupIfItemsSourceEmpty to true. I demonstrated this in my sample.

      Hi Ivan

      Thanks, that solves my problem indeed. What now is occurring is often the popup shows no items, while there are items in the ItemsSource though.

      Clipboard-File-1.png

      Clipboard-File-2.png

      Or is it because of the usage of async/ await?

      DevExpress Support Team 4 years ago

        With the code you sent initially, Suppliers is temporarily set to an empty collection. So yes, the popup will appear empty until Suppliers is set to another collection asynchronously. If you wish to avoid this behavior, cancel opening the popup and show it again when data is loaded. For example:

        C#
        bool isDataLoaded; private async void OnPopupOpening(object sender, OpenPopupEventArgs e) { if (isDataLoaded) return; if (DataContext is LotEditorViewModel viewmodel) { e.Cancel = true; await viewmodel.ReloadSuppliers(); isDataLoaded = true; ((PopupBaseEdit)sender).ShowPopup(); isDataLoaded = false; } }

        Disclaimer: The information provided on DevExpress.com and affiliated web properties (including the DevExpress Support Center) is provided "as is" without warranty of any kind. Developer Express Inc disclaims all warranties, either express or implied, including the warranties of merchantability and fitness for a particular purpose. Please refer to the DevExpress.com Website Terms of Use for more information in this regard.

        Confidential Information: Developer Express Inc does not wish to receive, will not act to procure, nor will it solicit, confidential or proprietary materials and information from you through the DevExpress Support Center or its web properties. Any and all materials or information divulged during chats, email communications, online discussions, Support Center tickets, or made available to Developer Express Inc in any manner will be deemed NOT to be confidential by Developer Express Inc. Please refer to the DevExpress.com Website Terms of Use for more information in this regard.