I have a WPF user control bound to EmployeeDeductionViewModel with a DataGrid bound to an ObservableCollection of state tax parameters:
public ObservableCollection<Models.StateTaxParmModel> StateTaxSettings
{
get { return _stateTaxSettings; }
set
{
if (_stateTaxSettings != value)
{
_stateTaxSettings = value;
OnPropertyChanged();
}
}
}
Here is the binding on the datagrid (shortened to make it easier to read):
<DataGrid ItemsSource="{Binding Path=StateTaxSettings}" SelectedItem="{Binding Path=StateTaxSetting, Mode=TwoWay}"...
Inside each StateTaxParamModel is a list of possible values the ComboBox needs to bind to:
public ObservableCollection<ParamValue> Values
{
get { return _values; }
set
{
if (_values != value)
{
_values = value;
OnPropertyChanged();
}
}
}
The ParamValue class is really simple:
public class ParamValue
{
public int ValueID { get; set; }
public string ValueText { get; set; }
}
However, the list of possible Values (List of ParamValue) varies with each row. Therein lies the problem. I can bind a ComboBox inside of a DataGrid as long as the list is part of the UserControl DataContext but because the list varies with each row, I can't bind it to the main DataContext, I have to bind it to the ObservableCollection of Values (List of ParamValue) that's unique to each row. Can anyone please help me understand how this is accomplished?
Here is my DataGridTemplateColumn where I'm trying to bind to the row:
<DataGridTemplateColumn Header="Value" MinWidth="60">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<TextBlock Text="{Binding Path=Value, Mode=OneWay}" ToolTip="The value of the state tax parameter for the employee."/>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
<DataGridTemplateColumn.CellEditingTemplate>
<DataTemplate>
<ComboBox ItemsSource="{Binding Path=Values, Mode=OneWay}" DisplayMemberPath="ValueText" SelectedValuePath="ValueText" SelectedValue="{Binding Path=Value, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged, ValidatesOnNotifyDataErrors=True, NotifyOnValidationError=True}" />
</DataTemplate>
</DataGridTemplateColumn.CellEditingTemplate>
</DataGridTemplateColumn>