Question: Most code samples on the DataGridComboBox seem to use a static resource as the ItemsSource. In my use case, I'd like to provide different ItemsSources with each bound object. Can this be done?
Background: I'm trying to bind a collection of Question class objects to a WPF DataGrid, using a DataGridComboBoxColumn control. The Answer string provides the SelectedValue. I'd like the AnswerDomain list to provide the ItemsSource for each ComboBox. The AnswerDomain differs from Question to Question.
Class
public class Question
{
string Answer {get; set;}
List<string> AnswerDomain {get; set;}
//...other stuff
}
XAML
<DataGrid ItemsSource="{Binding Path=InspectionItems}" AutoGenerateColumns="False" Name="dataGrid1" >
<DataGrid.Columns>
<DataGridComboBoxColumn Header="Answer Domain"
DisplayMemberPath="Answer"
SelectedValuePath="Answer"
ItemsSource="{Binding Path=AnswerDomain}"
>
</DataGridComboBoxColumn>
</DataGrid.Columns>
</DataGrid>
Problem: There are a couple problems. The key issue right now is that the ComboBoxes in each DataGrid Row aren't displaying the AnswerDomain strings. I've tried a series of XAML combinations without success. Help me Stack Overflow.
UPDATE: The selected solution below worked. After some further fumbling and by adding UpdateSourceTrigger=PropertyChanged
to the SelectedItem, user changes in the combobox were then reflected back in the underlying custom object.
<DataGridTemplateColumn Header="Answer">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<ComboBox
ItemsSource="{Binding AnswerDomain}"
SelectedItem="{Binding Answer, UpdateSourceTrigger=PropertyChanged}" />
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>