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