MainWindow.xaml:
<Grid>
<ComboBox ItemsSource="{Binding Path=CountryEntries}" Width="200" Height="60"
DisplayMemberPath="Country"
SelectedValuePath="Country"
SelectedItem="{Binding Path=SelectedCountry, Mode=TwoWay}"/>
</Grid>
MainWindow.xaml.cs:
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Runtime.CompilerServices;
using System.Windows;
using System.Windows.Data;
namespace ComboboxFilter
{
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
DataContext=new ViewModelClassB();
}
}
public class ViewModelClassB : INotifyPropertyChanged
{
private ObservableCollection<ClassA> listOfCountry;
public ObservableCollection<ClassA> ListOfCountry
{
get
{
return listOfCountry;
}
set { listOfCountry=value; OnPropertyChanged("ListOfCountry");}
}
private readonly ICollectionView countryEntries ;
public ICollectionView CountryEntries
{
get
{
return countryEntries;
}
}
public ViewModelClassB()
{
listOfCountry=new ObservableCollection<ClassA>();
listOfCountry.Add(new ClassA() { City="Beijing", Country="China"});
listOfCountry.Add(new ClassA() { City= "New York", Country= "United States" });
listOfCountry.Add(new ClassA() { City= "Paris ", Country= "France" });
countryEntries=CollectionViewSource.GetDefaultView(listOfCountry);
}
private ClassB selectedCountry;
public ClassB SelectedCountry
{
get => selectedCountry;
set
{
this.selectedCountry = value;
OnPropertyChanged("SelectedCountry");
//OnPropertyChanged(nameof(CityView));
}
}
public event PropertyChangedEventHandler PropertyChanged;
internal void OnPropertyChanged([CallerMemberName] string propName = "") => PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propName));
}
public class ClassA
{
public string Country { get; set; }
public string City { get; set; }
}
public class ClassB
{
public string Pro1 { get; set; }
public string Pro2 { get; set; }
public string CountryValue { get; set; }
}
}
The result:
If the response is helpful, please click "Accept Answer" and upvote it.
Note: Please follow the steps in our [documentation][5] to enable e-mail notifications if you want to receive the related email notification for this thread.
[5]: https://learn.microsoft.com/en-us/answers/articles/67444/email-notifications.html