1
votes

Having an issue getting the selected Value of a Combobox. The error I get is and I cannot seem to fix it, have tried various combinations of binding, selectedvaluepath etc hopefully someone can point me in the right directions, thanks!

ConvertBack cannot convert value 'ClientRatesWPF.Model.ChargeUnit' (type 'ChargeUnit'). BindingExpression:Path=SelectedChargeUnitListValueId; DataItem='ClientRatesViewModel' (HashCode=33434731); target element is 'ComboBox' (Name='cmbChargeUnit'); target property is 'SelectedValue' (type 'Object') NotSupportedException:'System.NotSupportedException: Int32Converter cannot convert from ClientRatesWPF.Model.ChargeUnit.

<av:ComboBox x:Name="cmbChargeUnit" HorizontalAlignment="Left" Margin="548,15,0,0" Width="187" ItemsSource="{av:Binding ChargeUnits}" DisplayMemberPath="ChargeUnitDescription" SelectedValue="{Binding SelectedChargeUnitListValueId}" VerticalAlignment="Top" Background="{av:DynamicResource {x:Static av:SystemColors.ControlDarkDarkBrushKey}}" Height="20" IsSynchronizedWithCurrentItem="True"/>

VM:

public ObservableCollection<ChargeUnit> ChargeUnits
{
    get { return _chargeUnitsCollection; }
    set { _chargeUnitsCollection = value; }
}

public IList<ChargeUnit> ChargeUnitDescription
{
    get { return _chargeUnitDescription; }
    set
    {
        _chargeUnitDescription = value;
        OnPropertyChanged("ChargeUnitDescription");
    }
}

public IList<ChargeUnit> ChargeUnitListValueId
{
    get { return _chargeUnitListValueId; }
    set
    {
        _chargeUnitListValueId = value;
        OnPropertyChanged("ChargeUnitListValueId");
    }
}

public int SelectedChargeUnitListValueId
{
    get { return _SelectedChargeUnitListValueId; }
    set
    {
        _SelectedChargeUnitListValueId = value;
        OnPropertyChanged("SelectedChargeUnitListValueId");
    }
}

Where I am populating the Observable Collection (code extracted from method)

while (reader.Read())
{
    _chargeUnitsCollection.Add(new ChargeUnit
    {
        ChargeUnitListValueId = (int)reader["ListValueId"],
        ChargeUnitDescription = reader["ValueName"].ToString()
    });
}
1

1 Answers

1
votes

so you have a combo box populated with a list of ChargeUnit's and you are trying to set the SelectedValue to an int that will be your problem. You need to change to the selected ChargeUnit

public int SelectedChargeUnitListValueId
{
    get { return _SelectedChargeUnitListValueId; }
    set
    {
        _SelectedChargeUnitListValueId = value;
        var unitsWhere = ChargeUnits.Where(x => x.id == _SelectedChargeUnitListValueId);
        if (unitsWhere.Count() > 0)
        {
           SelectedChargeUnit = unitsWhere.First();
        }
        OnPropertyChanged("SelectedChargeUnitListValueId");
    }
}

public ChargeUnit SelectedChargeUnit
{
    get { return _SelectedChargeUnit; }
    set
    {
        _SelectedChargeUnit = value;
        OnPropertyChanged("SelectedChargeUnit");
    }
}

and in your binding:

SelectedValue="{Binding SelectedChargeUnit}"