I'm attempting to set the column combobox values based on the row's (object) individual collection value so that each row has a different list of options based on another value.
For example.
Column 1: Country combobox - e.g. UK selected from { "UK", "USA", "France" }
Column 2: City combobox - e.g. LA selected { "New York", "LA", "Texas" } (Based on USA selection of previous)
Below is my example data object
public class ExampleData
{
public List<string> countries { get; set; }
public List<string> cities { get; set; }
public string country { get; set; }
public string city { get; set; }
public ExampleData(string country)
{
this.country = country;
countries = new List<string>() { "UK", "USA", "France" };
}
// Example update method to change dependant options
public void UpdateOptions()
{
if (country == "UK")
{
cities = new List<string>() { "London", "Bristol", "Birmingham" };
}
else if (country == "USA")
{
cities = new List<string>() { "New York", "LA", "Texas" };
}
else if (country == "France")
{
cities = new List<string>() { "Paris", "Lyon", "Nice" };
}
else
{
cities = new List<string>();
}
}
}
I would then create an example collection of the data, to be displayed within the datagrid
public void TestScenario()
{
ExampleDataCollection = new List<ExampleData>();
exampleDataList.Add(new ExampleData("UK"));
exampleDataList.Add(new ExampleData("USA"));
exampleDataList.Add(new ExampleData("France"));
DataGrid.ItemsSource = ExampleDataCollection ;
}
Then within the xaml, I would have the object collection bound to the datagrid as the ItemsSource, while each column combobox would be bound to the objects own collection which will be independent per row (object). This would be something like ExampleData.countries, with ExampleData.Country being the selected value.
<DataGrid
Name="ExampleDataGrid"
AutoGenerateColumns="False"
ItemsSource="{Binding ExampleDataCollection}"
>
<DataGrid.Columns>
<DataGridComboBoxColumn
Header="country"
ItemsSource="{Binding ExampleData.countries}"
SelectedValueBinding="{Binding ExampleData.country}"
/>
<DataGridComboBoxColumn
Header="city"
ItemsSource="{Binding ExampleData.cities}"
SelectedValueBinding="{Binding ExampleData.city}"
/>
</DataGrid.Columns>
</DataGrid>
Is this actually feasible to achieve, or should an alternative method be applied to this situation? Logically it is not that complex of a process, however I have been unable to implement a solution.