Share via

Combo box does not update

Eduardo Gomez 4,316 Reputation points
2022-06-22T19:30:58.93+00:00

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

Developer technologies | Windows Presentation Foundation
Developer technologies | XAML
Developer technologies | XAML

A language based on Extensible Markup Language (XML) that enables developers to specify a hierarchy of objects with a set of properties and logic.

0 comments No comments

Answer accepted by question author

Hui Liu-MSFT 48,711 Reputation points Microsoft External Staff
2022-06-23T07:01:15.117+00:00

Hi,@Eduardo Gomez . 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.

Was this answer helpful?


0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.