0
votes

I have tried for what seems a lifetime to get this issue sorted but unfortunately to no avail.. even after searching stackoverflow and other sites.

Ill try and keep my issue short but to the point.

Using WPF and the MVVM methodology I have : A View and a ViewModel.

From within the view XAML I am binding a comboBox to a list of values in a key/value dictionary object. This works fine - I am able to see the values drop down when I click on the combo box.

Note: Each record can have more than 1 instalment in a gridview where the combobox is displayed.

<dxg:GridColumn Header="Instalments" 
        Width="100" 
        AllowSorting="False" 
        AllowAutoFilter="False">
<dxg:GridColumn.CellTemplate>
<DataTemplate>
    <ComboBox IsEnabled="{Binding Data.IsIncludedInDD}"
              Name="cbInstalmentValues" 
              Width="200"
              ItemsSource="{Binding Data.D_InstalmentValues}"
              SelectedIndex="{Binding Data.Instalments, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" 
              DisplayMemberPath="Value" 
              SelectedValuePath="Key"
              SelectedValue="{Binding SelectedInstalmentValue, Mode=TwoWay, ValidatesOnDataErrors=True, NotifyOnValidationError=True, UpdateSourceTrigger=PropertyChanged}"
              VerticalAlignment="Center" />

Code:

private static readonly Dictionary<int, string> D_instalmentValues = new Dictionary<int, string>
{
  {0, "None"}, 
  {1, "1 Instalment"}, 
  {2, "2 Instalments"}, 
  {3, "3 Instalments"}, 
  {4, "4 Instalments"},
  {5, "5 Instalments"}, 
  {6, "6 Instalments"}, 
  {7, "7 Instalments"}, 
  {8, "8 Instalments"}, 
  {9, "9 Instalments"},
  {10, "10 Instalments"}, 
  {11, "11 Instalments"}, 
  {12, "12 Instalments"}
};

public Dictionary<int, string> D_InstalmentValues 
{
  get
  {
    return _instalmentValues;
  }
  set
  {
    _instalmentValues = value;
  }
}

So..when I load up my application I want the comboBox to read the value from the 'Instalments' property in the viewmodel and bind to the same index of the value in the dictionary and display the number of instalments.. ie: None, 1 Instalment etc..

public int Instalments
{
    get
    {
        return _chargeTemplate.Instalments;
    }

    set
    {
    if (value == -1)
    {
        value = value + 1;
        _chargeTemplate.Instalments = _chargeTemplate.Instalments;
    }
    else
    {
        if (_chargeTemplate.Instalments == value)
        {
            return;
        }
        else
        {
            _chargeTemplate.Instalments = value;
        }
     }
     //NotifyNotRegister("Instalments");
     RaisePropertyChanged("Instalments");
    }
}

Now the '_chargeTemplate.Instalments' value holds the value from the Instalments column from the record in the database.

The combobox does not seem to bind if the value in the Instalments property is 0. The box is blank.

It does get populated after I select a record which has a value greater than 0.

When I tried populating all the instalment fields in the sql table with values from 1 to 12 the selected index did not get set properly for the very first record displayed in my application but if I selected the next record it was displayed ok... then if I reselected the first record again it was also displayed correctly.

Another strange part of the behaviour was for the first record, if I had 3 instalments, and in the sql table instalment 1 = 2, instalment 2 = 4 and instalment 3 = 0... the first 2 instalments for that record had their combo box index set but the last record was empty.

In summary - I need to know how, when the application loads - how to make the selected index bind from a viewmodel to the view without the combo box being blank for the first record or for values where the selected index is 0.

I hope there is enough information here...

Many Thanks

UPDATE :

I made a small change to the XAML and the property to which I am binding the combobox selectedindex. This allows the 0 value to display 'None' in the combobox where the value is 0 for a persons instalment value in the sql database.

However the very first person displayed in the list of records does not have the selectd index set to the index of the dictionary value unless I select another person and reselect the first person again... strange

ViewModel

public int Instalments { get { return _chargeTemplate.Instalments; } }

XAML

SelectedIndex="{Binding Data.Instalments, Mode=OneWay}"

1

1 Answers

3
votes

I found the answer eventually after staring at the screen for a bit and realising I had left out 1 WORD!!!!

I removed the 'SelectedIndex' property from the combobox control completely and used the 'SelectedValue' instead. Also the word missing in front was 'Data.'

With a few adjustments on the properties in the viewmodel this worked a treat displaying the correct value in the combobox from the database on loading the application

See below:)

Thanks!

DisplayMemberPath="Value" SelectedValuePath="Key" SelectedValue="{Binding Data.SelectedInstalmentValue, Mode=TwoWay, ValidatesOnDataErrors=True, NotifyOnValidationError=True, UpdateSourceTrigger=PropertyChanged}" Style="{StaticResource ValidationStyle}" VerticalAlignment="Center" />