0
votes

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>
1
Put them in Models.StateTaxParmModel? Also, you shouldn't INPC an observable property, there isn't any need.user1228
Thank you Will. That's actually were they are but I knew as soon as I posted this question the answer would slap me in the face. It turns out everything is correct but I wasn't populating the list of possible values in the right place at the right time so the list of values was always empty. This works fine as long as the list of values gets populated before the binding occurs.Darrell
Simple solution. Go ahead and add a bit of code in an answer and you can close this out.user1228

1 Answers

1
votes

Will had the right idea and in fact, that's where the list of values was located. As it turns out, everything laid out in my question is correct, I just wasn't populating the list of possible values at the right time. Lists work much better when there's something in the list! You'd think I'd know that after 18 years of doing this! ;-)