question

EduardoGomez-1870 avatar image
0 Votes"
EduardoGomez-1870 asked HuiLiu-MSFT edited

Combo box does not update

Hello

I am building a wpf app, using MVVM (I already imported MVVM.Helpers)

I am trying to fill a list with 5000 numbers, and put in in a commbo box, and for some reason is not working

my app: https://github.com/eduardoagr/RsidentsManager

I have a BaseViewModel, and every ViewModel I have inherit from that base

 <ComboBox
                 materialDesign:HintAssist.Hint="Rooms" materialDesign:HintAssist.IsFloating="True" ItemsSource="{Binding Rooms}"
                 IsReadOnly="True" />

VM

     public class PrpertiesWindowViewModel : BaseViewModel {
    
         public List<int> Rooms { get; set; }
    
         public PrpertiesWindowViewModel() {
    
             Rooms = new List<int>();
             FillList();
    
         }
    
         private void FillList() {
    
             for (int i = 1; i < 5000; i++) {
                 Rooms.Add(i);
             }
         }
     }

if you run you do not see anything is like the list dosent get filled



windows-wpfdotnet-wpf-xaml
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

1 Answer

HuiLiu-MSFT avatar image
0 Votes"
HuiLiu-MSFT answered HuiLiu-MSFT edited

Hi,@EduardoGomez-1870. Welcome Microsoft Q&A.

The complete code required is shown below.
Xaml:

  <Window.Resources>
         <Thickness x:Key="tbMargin">0,10,0,0</Thickness>
     </Window.Resources>
    
     <Window.DataContext>
         <local:PrpertiesWindowViewModel />
     </Window.DataContext>
    
     <Border Padding="20" Width="300">
         <StackPanel>
             <TextBox materialDesign:HintAssist.Hint="Name of the building" materialDesign:HintAssist.IsFloating="True" />
    
             <TextBox  materialDesign:HintAssist.Hint="Description"  materialDesign:HintAssist.IsFloating="True" />
    
             <ComboBox  materialDesign:HintAssist.Hint="Rooms" materialDesign:HintAssist.IsFloating="True"
                        ItemsSource="{Binding Rooms}" IsReadOnly="True" />
    
             <TextBox    materialDesign:HintAssist.Hint="Address"   materialDesign:HintAssist.IsFloating="True" />
    
             <ListBox Visibility='Collapsed' />
    
         </StackPanel>
     </Border>

Codebehind:

 using System;
 using System.Collections.Generic;
 using System.ComponentModel;
 using System.Net.Http;
 using System.Runtime.CompilerServices;
 using System.Threading.Tasks;
 using System.Windows;
 using Newtonsoft.Json;
    
 namespace SuggestionDemo
 {
   public partial class MainWindow : Window
   {
     public MainWindow()
     {
       InitializeComponent();
     }
   }
   public class PrpertiesWindowViewModel : BaseViewModel
   {
    
     public List<int> Rooms { get; set; }
    
     public PrpertiesWindowViewModel()
     {
    
       Rooms = new List<int>();
       FillList();
    
     }
    
     private void FillList()
     {
    
       for (int i = 1; i < 50; i++)
       {
         Rooms.Add(i);
       }
     }
   }
   public class BingSuggestions
   {
     private static readonly string subscriptionKey = "89d284162a144c0d887cf725a5cffecf";
     private static readonly string baseUri = "https://api.bing.microsoft.com/v7.0/suggestions";
     private const string QUERY_PARAMETER = "q?=";
     private const string MKT_PARAMETER = "&mkt=en-us";
    
     public static async Task<List<string>> CallApi(string query)
     {
    
       using (var client = new HttpClient())
       {
    
         var list = new List<string>();
    
         client.DefaultRequestHeaders.Add("Ocp-Apim-Subscription-Key", subscriptionKey);
    
         var content = await client.GetAsync($"{baseUri}?{QUERY_PARAMETER}{query}{MKT_PARAMETER}");
    
         if (content.IsSuccessStatusCode)
         {
    
           string jsonContent = await content.Content.ReadAsStringAsync();
           SearchSuggestion response = JsonConvert.DeserializeObject<SearchSuggestion>(jsonContent);
    
           if (response.Query != null)
           {
             list.Add(response.Query);
           }
         }
    
         return list;
       }
     }
   }
    
   public class QueryContext
   {
    
     [JsonProperty("originalQuery")]
     public string? OriginalQuery { get; set; }
   }
    
   public class SearchSuggestion
   {
    
     [JsonProperty("url")]
     public string? Url { get; set; }
    
     [JsonProperty("displayText")]
     public string? DisplayText { get; set; }
    
     [JsonProperty("query")]
     public string? Query { get; set; }
    
     [JsonProperty("searchKind")]
     public string? SearchKind { get; set; }
   }
    
   public class SuggestionGroup
   {
    
     [JsonProperty("name")]
     public string? Name { get; set; }
    
     [JsonProperty("searchSuggestions")]
     public IList<SearchSuggestion>? SearchSuggestions { get; set; }
   }
    
   public class AutoSuggest
   {
    
     [JsonProperty("_type")]
     public string? Type { get; set; }
    
     [JsonProperty("queryContext")]
     public QueryContext? QueryContext { get; set; }
    
     [JsonProperty("suggestionGroups")]
     public IList<SuggestionGroup>? SuggestionGroups { get; set; }
   }
      public class BaseViewModel : INotifyPropertyChanged
   {
     private string? title = string.Empty;
    
     private string? subtitle = string.Empty;
    
     private string? icon = string.Empty;
    
     private bool isBusy;
    
     private bool isNotBusy = true;
    
     private bool canLoadMore = true;
    
     private string? header = string.Empty;
    
     private string? footer = string.Empty;
    
     public string? Title
     {
       get
       {
         return title;
       }
       set
       {
          
         SetProperty(ref title, value, "Title");
       }
     }
    
     public string? Subtitle
     {
       get
       {
         return subtitle;
       }
       set
       {
          
         SetProperty(ref subtitle, value, "Subtitle");
       }
     }
    
     public string? Icon
     {
       get
       {
         return icon;
       }
       set
       {
            
         SetProperty(ref icon, value, "Icon");
       }
     }
    
     public bool IsBusy
     {
       get
       {
         return isBusy;
       }
       set
       {
         
         if (SetProperty(ref isBusy, value, "IsBusy"))
         {
           IsNotBusy = !isBusy;
         }
       }
     }
    
    
     public bool IsNotBusy
     {
       get
       {
         return isNotBusy;
       }
       set
       {
        
         if (SetProperty(ref isNotBusy, value, "IsNotBusy"))
         {
              
           IsBusy = !isNotBusy;
         }
       }
     }
    
     
     public bool CanLoadMore
     {
       get
       {
         return canLoadMore;
       }
       set
       {
      
         SetProperty(ref canLoadMore, value, "CanLoadMore");
       }
     }
    
     public string? Header
     {
       get
       {
         return header;
       }
       set
       {
            
         SetProperty(ref header, value, "Header");
       }
     }
    
     public string? Footer
     {
       get
       {
         return footer;
       }
       set
       {
         
         SetProperty(ref footer, value, "Footer");
       }
     }
     protected virtual bool SetProperty<T>(ref T backingStore, T value, [CallerMemberName] string propertyName = "", Action? onChanged = null, Func<T, T, bool>? validateValue = null)
     {
       if (EqualityComparer<T>.Default.Equals(backingStore, value))
       {
         return false;
       }
    
       if (validateValue != null && !validateValue!(backingStore, value))
       {
         return false;
       }
    
       backingStore = value;
       onChanged?.Invoke();
       OnPropertyChanged(propertyName);
       return true;
     }
     protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = "")
     {
       this.PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
     }
     public event PropertyChangedEventHandler? PropertyChanged;
   }

The result:
214129-image.png


If the response is helpful, please click "Accept Answer" and upvote it.
Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.



image.png (7.6 KiB)
· 2
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

I can see here, that you are using code-behind.

I am not usind code behind, s that is why I inherit from my ViewModelBase in my PropertiesviewModel


 public List<int> Rooms { get; set; }
    
         public PrpertiesWindowViewModel() {
    
             Rooms = new List<int>();
             FillList();
    
         }
    
         private void FillList() {
    
             for (int i = 1; i < 50001; i++) {
                 Rooms.Add(i);
             }
         }

This viewModel already implement theIpropertychange


0 Votes 0 ·
HuiLiu-MSFT avatar image HuiLiu-MSFT EduardoGomez-1870 ·

Hi,@EduardoGomez-1870 . By code-behind do you mean writing the code in MaiWindow.xaml.cs or using the ObservableObject class code from GitHub? If it's the latter, would you mind writing the code from the ObservableObject in the BaseViewModel ? I updated my answer, you could check it out.

0 Votes 0 ·