Hi,@Mesh Ka.Welcome to Microsoft Q&A.
I made the following modifications based on your code. You could modify it according to your namespace.
<Window.DataContext> <local:MainWindowViewModel /> </Window.DataContext>
<Window x:Class="ComboboxDemo.MainWindow"
...
xmlns:local="clr-namespace:ComboboxDemo"
mc:Ignorable="d"
Title="MainWindow" Height="450" Width="800">
<Window.DataContext>
<local:MainWindowViewModel />
</Window.DataContext>
<Grid >
<StackPanel>
<DataGrid AutoGenerateColumns="False" ItemsSource="{Binding PlacesOfInterest}">
<DataGrid.Columns>
<DataGridTemplateColumn Header="Country">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<ComboBox
Width="120"
DisplayMemberPath="CountryName"
ItemsSource="{Binding DataContext.Countries, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type Window}}}"
SelectedItem="{Binding DataContext.SelectedCountry, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type Window}}, Mode=TwoWay}"
/>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
<DataGridTemplateColumn Header="Province">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<ComboBox
Width="120"
DisplayMemberPath="ProvinceName"
ItemsSource="{Binding DataContext.Provinces, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type Window}}}"
SelectedItem="{Binding DataContext.SelectedProvince, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type Window}}, Mode=TwoWay}"
/>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
<DataGridTemplateColumn Header="District">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<ComboBox
Width="120"
DisplayMemberPath="DistrictName"
ItemsSource="{Binding DataContext.Districts, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type Window}}}"
SelectedItem="{Binding DataContext.SelectedDistrict, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type Window}}, Mode=TwoWay}"
/>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
</DataGrid.Columns>
</DataGrid>
</StackPanel>
</Grid>
</Window>
public partial class MainWindowViewModel : ObservableObject
{
[ObservableProperty]
ObservableCollection<PlacesOfInterest> placesOfInterest;
[ObservableProperty]
ObservableCollection<CountriesModel> countries;
[ObservableProperty]
ObservableCollection<ProvincesModel> provinces;
[ObservableProperty]
ObservableCollection<DistrictsModel> districts;
public MainWindowViewModel()
{
this.PlacesOfInterest = new ObservableCollection<PlacesOfInterest>(DAL.LoadPlacesOfInterest());
this.Countries = new ObservableCollection<CountriesModel>(DAL.LoadCountries());
this.Provinces = new ObservableCollection<ProvincesModel>(DAL.LoadProvinces());
this.Districts = new ObservableCollection<DistrictsModel>(DAL.LoadDistricts());
}
private CountriesModel selectedCountry;
public CountriesModel SelectedCountry
{
get { return selectedCountry; }
set
{
selectedCountry = value;
Provinces = GetProvincesForCountry(selectedCountry);
SelectedProvince = null;
SelectedDistrict = null;
OnPropertyChanged(nameof(SelectedCountry));
}
}
private ProvincesModel selectedProvince;
public ProvincesModel SelectedProvince
{
get { return selectedProvince; }
set
{
selectedProvince = value;
Districts = GetDistrictsForProvince(selectedProvince);
SelectedDistrict = null;
OnPropertyChanged(nameof(SelectedProvince));
}
}
private DistrictsModel selectedDistrict;
public DistrictsModel SelectedDistrict
{
get { return selectedDistrict; }
set
{
selectedDistrict = value;
OnPropertyChanged(nameof(SelectedDistrict));
}
}
private ObservableCollection<ProvincesModel> GetProvincesForCountry(CountriesModel country)
{
return new ObservableCollection<ProvincesModel>(DAL.LoadProvincesForCountry(country));
}
private ObservableCollection<DistrictsModel> GetDistrictsForProvince(ProvincesModel province)
{
return new ObservableCollection<DistrictsModel>(DAL.LoadDistrictsForProvince(province));
}
}
public class DAL
{
private static readonly string ConnString = "...";
public static List<PlacesOfInterest> LoadPlacesOfInterest()
{
using (IDbConnection conn = new SqlConnection(ConnString))
{
if (conn.State == ConnectionState.Closed) conn.Open();
return conn.Query<PlacesOfInterest>("SELECT * FROM PlacesOfInterest").ToList();
}
}
public static List<CountriesModel> LoadCountries()
{
using (IDbConnection conn = new SqlConnection(ConnString))
{
if (conn.State == ConnectionState.Closed) conn.Open();
return conn.Query<CountriesModel>("SELECT * FROM Countries").ToList();
}
}
public static List<ProvincesModel> LoadProvinces()
{
using (IDbConnection conn = new SqlConnection(ConnString))
{
if (conn.State == ConnectionState.Closed) conn.Open();
return conn.Query<ProvincesModel>("SELECT * FROM Provinces").ToList();
}
}
public static List<ProvincesModel> LoadProvincesForCountry(CountriesModel country)
{
using (IDbConnection conn = new SqlConnection(ConnString))
{
if (conn.State == ConnectionState.Closed) conn.Open();
string query = "SELECT * FROM Provinces WHERE CountryName = @CountryName";
return conn.Query<ProvincesModel>(query, new { CountryName = country.CountryName }).ToList();
}
}
public static List<DistrictsModel> LoadDistricts()
{
using (IDbConnection conn = new SqlConnection(ConnString))
{
if (conn.State == ConnectionState.Closed) conn.Open();
return conn.Query<DistrictsModel>("SELECT * FROM Districts").ToList();
}
}
public static List<DistrictsModel> LoadDistrictsForProvince(ProvincesModel province)
{
using (IDbConnection conn = new SqlConnection(ConnString))
{
if (conn.State == ConnectionState.Closed) conn.Open();
string query;
if (province==null)
{
return conn.Query<DistrictsModel>("SELECT * FROM Districts").ToList();
}
else
{
query = "SELECT * FROM Districts WHERE ProvinceID = @ProvinceID";
return conn.Query<DistrictsModel>(query, new { ProvinceID = province.ProvinceID }).ToList();
}
}
}
}
The result:
If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
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.